图片/图形

滚动背景视觉差jQuery特效

阿里云


这是一款带混合模式的 jquery 滚动背景视觉差特效。该特效利用 css mix-blend-mode 来混合背景和文字,并使用 jquery 来制作滚动时的视觉差效果,效果非常炫酷,在页面中引入 jquery 文件。

  1. <script src=".js/jquery.min.js" type="text/javascript"></script>
也想出现在这里?联系我们
创客主机

HTML 结构

视觉差效果的 HTML 结果如下。

  1. <header>
  2.   <div class="overlay">
  3.     <div class="parallax">
  4.       <h1>Awesome<span>Parallax Scrolling</span></h1>
  5.       <div class="one"></div>
  6.       <div class="two"></div>
  7.       <div class="three"></div>
  8.       <div class="four"></div>
  9.       <div class="five"></div>
  10.     </div>
  11.   </div>
  12. </header>

CSS 样式

然后添加下面的 CSS 样式。

  1. *,
  2. *:before,
  3. *:after {
  4.   box-sizing: border-box;
  5. }
  6.  
  7. body {
  8.   margin: 0;
  9.   font-family: "Poppins", sans-serif;
  10.   font-size: 62.5%;
  11. }
  12.  
  13. ::-webkit-scrollbar {
  14.   width: 0;
  15. }
  16.  
  17. header {
  18.   position: relative;
  19.   height: 100vh;
  20.   overflow: hidden;
  21.   display: flex;
  22.   justify-content: center;
  23.   align-items: center;
  24.   background-color: #c6d9d3;
  25. }
  26. header .overlay {
  27.   position: absolute;
  28.   top: 0;
  29.   left: 0;
  30.   width: 100%;
  31.   height: 100%;
  32. }
  33. header .overlay h1 {
  34.   position: absolute;
  35.   top: 50%;
  36.   left: 50%;
  37.   transform: translate(-50%, -50%);
  38.   z-index: 3;
  39.   margin: 0;
  40.   text-transform: uppercase;
  41.   text-align: center;
  42.   font-size: 9rem;
  43.   text-shadow: 25px -15px rgba(25, 42, 46, 0.04);
  44.   color: #142124;
  45.   mix-blend-mode: overlay;
  46. }
  47. header .overlay h1 span {
  48.   margin-top: -2em;
  49.   padding-top: 0.5em;
  50.   display: block;
  51.   letter-spacing: 5px;
  52.   font-size: 1.25rem;
  53. }
  54. header .parallax {
  55.   position: relative;
  56.   width: 100%;
  57.   height: 100%;
  58. }
  59. header .parallax div {
  60.   position: absolute;
  61.   bottom: 0;
  62.   left: 0;
  63.   width: 100%;
  64.   height: 100%;
  65.   background-position: 0 50%;
  66.   background-repeat: no-repeat;
  67.   background-size: 100%;
  68. }
  69. header .parallax .one {
  70.   background-image: url("svg/mountains-1.svg");
  71.   z-index: 4;
  72. }
  73. header .parallax .two {
  74.   background-image: url("svg/mountains-2.svg");
  75.   z-index: 3;
  76. }
  77. header .parallax .three {
  78.   background-image: url("svg/mountains-3.svg");
  79.   z-index: 2;
  80. }
  81. header .parallax .four {
  82.   background-image: url("svg/mountains-4.svg");
  83.   z-index: 1;
  84. }
  85. header .parallax .five {
  86.   background-image: url("svg/mountains-5.svg");
  87.   z-index: 0;
  88. }

初始化插件

在页面 DOM 元素加载完毕之后,通过下面的方法来初始化该滚动视觉差效果。

  1. $(window).scroll(function () {
  2.   var a = $(this).scrollTop();
  3.   var b = 800;
  4.   $("h1").css({
  5.     backgroundPosition: "center " + a / 2 + "px" });
  6.  
  7.   $(".parallax").css({
  8.     top: a / 1.6 + "px",
  9.     opacity: 1 - a / b });
  10.  
  11. });
  12.  
  13. // parallax scrolling
  14.  
  15. document.addEventListener("scroll", () => {
  16.   var top = window.pageYOffset,
  17.   var one = document.querySelector(".one"),
  18.   var two = document.querySelector(".two"),
  19.   var three = document.querySelector(".three"),
  20.   var four = document.querySelector(".four"),
  21.   var five = document.querySelector(".five");
  22.  
  23.   one.style.bottom = -(top * 0.1) + "px";
  24.   two.style.bottom = -(top * 0.2) + "px";
  25.   three.style.bottom = -(top * 0.3) + "px";
  26.   four.style.bottom = -(top * 0.4) + "px";
  27.   five.style.bottom = -(top * 0.5) + "px";
  28. });

Codepen 网址:https://codepen.io/andrejsharapov/pen/aPrNxP

滚动背景视觉差 jQuery 特效

已有 395 人购买
查看演示升级 VIP立刻购买

演示地址 下载地址
收藏
(1)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!