幻灯片/轮播

基于GSAP全屏背景图片jQuery幻灯片插件

阿里云

Wavescroll 是一款基于 GSAP 的响应式全屏背景图片 jQuery 幻灯片插件。该幻灯片插件支持鼠标拖动、鼠标滚动和移动触摸事件,可以制作出炫酷的波浪滚动切换效果。该幻灯片插件主要使用的技术有:jQuery, CSS3, requestAnimationFrame 和 GSAP 的 TweenMax.js。

HTML 结构:

  1. <div class="ws-pages">
  2.   <div class="ws-bgs">
  3.     <div class="ws-bg"></div>
  4.     <div class="ws-bg"></div>
  5.     <div class="ws-bg"></div>
  6.     <div class="ws-bg"></div>
  7.     <div class="ws-bg"></div>
  8.   </div>
  9.   <div class="ws-text">
  10.     <h2 class="ws-text__heading">
  11.       <span>This is Slide 1</span>
  12.     </h2>
  13.     <h2 class="ws-text__heading">
  14.       <span>This is Slide 2</span>
  15.     </h2>
  16.     <h2 class="ws-text__heading">
  17.       <span>This is Slide 3</span>
  18.     </h2>
  19.     <h2 class="ws-text__heading">
  20.       <span>This is Slide 4</span>
  21.     </h2>
  22.     <h2 class="ws-text__heading">
  23.       <span>This is Slide 5</span>
  24.       <span>The end</span>
  25.     </h2>
  26.   </div>
  27. </div>
也想出现在这里?联系我们
创客主机

CSS 样式:

该幻灯片的主要 CSS 样式如下:所有幻灯片都通过 background-size: cover;来设置为全屏模式。.ws-bg__part 是制作百叶窗效果的每一个窗叶。每个幻灯片 slide 分为 24 个窗叶,分别使用使用 ws-bg__part-n:after 伪元素来制作。

  1. .ws-pages {
  2.   overflow: hidden;
  3.   position: relative;
  4.   height: 100%;
  5. }
  6.  
  7. .ws-bgs {
  8.   position: relative;
  9.   height: 100%;
  10. }
  11.  
  12. .ws-bg {
  13.   height: 100%;
  14.   background-size: cover;
  15.   background-position: center center;
  16. }
  17.  
  18. .ws-pages.s--ready .ws-bg { background: none !important; }
  19.  
  20. .ws-bg:after {
  21.   content: "";
  22.   display: table;
  23.   clear: both;
  24. }
  25.  
  26. .ws-bg__part {
  27.   overflow: hidden;
  28.   position: relative;
  29.   float: left;
  30.   width: 4.16667%;
  31.   height: 100%;
  32.   background-size: cover;
  33.   background-position: center center;
  34.   cursor: -webkit-grab;
  35.   cursor: grab;
  36.   -webkit-user-select: none;
  37.   -moz-user-select: none;
  38.   -ms-user-select: none;
  39.   user-select: none;
  40. }
  41.  
  42. .ws-bg__part:after {
  43.   content: "";
  44.   position: absolute;
  45.   top: 0;
  46.   width: 100vw;
  47.   height: 100%;
  48.   background: inherit;
  49. }
  50.  
  51. .ws-bg__part-1:after { left: 0vw; }
  52.  
  53. .ws-bg__part-2:after { left: -4.16667vw; }
  54.  
  55. .ws-bg__part-3:after { left: -8.33333vw; }
  56.  
  57. .ws-bg__part-4:after { left: -12.5vw; }
  58.  
  59. .ws-bg__part-5:after { left: -16.66667vw; }
  60.  
  61. .ws-bg__part-6:after { left: -20.83333vw; }
  62.  
  63. .ws-bg__part-7:after { left: -25vw; }
  64.  
  65. .ws-bg__part-8:after { left: -29.16667vw; }
  66.  
  67. .ws-bg__part-9:after { left: -33.33333vw; }
  68.  
  69. .ws-bg__part-10:after { left: -37.5vw; }
  70.  
  71. .ws-bg__part-11:after { left: -41.66667vw; }
  72.  
  73. .ws-bg__part-12:after { left: -45.83333vw; }
  74.  
  75. .ws-bg__part-13:after { left: -50vw; }
  76.  
  77. .ws-bg__part-14:after { left: -54.16667vw; }
  78.  
  79. .ws-bg__part-15:after { left: -58.33333vw; }
  80.  
  81. .ws-bg__part-16:after { left: -62.5vw; }
  82.  
  83. .ws-bg__part-17:after { left: -66.66667vw; }
  84.  
  85. .ws-bg__part-18:after { left: -70.83333vw; }
  86.  
  87. .ws-bg__part-19:after { left: -75vw; }
  88.  
  89. .ws-bg__part-20:after { left: -79.16667vw; }
  90.  
  91. .ws-bg__part-21:after { left: -83.33333vw; }
  92.  
  93. .ws-bg__part-22:after { left: -87.5vw; }
  94.  
  95. .ws-bg__part-23:after { left: -91.66667vw; }
  96.  
  97. .ws-bg__part-24:after { left: -95.83333vw; }

幻灯片的图片使用背景图片来制作。

  1. .ws-bg:nth-child(1) { background-image: url(1.jpg); }
  2.  
  3. .ws-bg:nth-child(1) .ws-bg__part { 1.jpg); }
  4.  
  5. .ws-bg:nth-child(2) { background-image: url(2.jpg); }
  6.  
  7. .ws-bg:nth-child(2) .ws-bg__part { background-image: url(2.jpg); }
  8.  
  9. .ws-bg:nth-child(3) { background-image: url(3.jpg); }
  10.  
  11. .ws-bg:nth-child(3) .ws-bg__part { background-image: url(3.jpg); }
  12.  
  13. .ws-bg:nth-child(4) { background-image: url(4.jpg); }
  14.  
  15. .ws-bg:nth-child(4) .ws-bg__part { background-image: url(4.jpg); }
  16.  
  17. .ws-bg:nth-child(5) { background-image: url(5.jpg); }
  18.  
  19. .ws-bg:nth-child(5) .ws-bg__part { background-image: url(5.jpg); }

初始化插件:

Wavescroll 是基于 GSAP 来制作的,所以在使用时要在页面中引入 jQuery 和 TweenMax.js 文件。

  1. <script src="/path/to/jquery.min.js"></script> 
  2. <script src="/path/to/TweenMax.min.js"></script>

JS 代码如下:

  1. window.requestAnimFrame = (function() {
  2.   return window.requestAnimationFrame ||
  3.     window.webkitRequestAnimationFrame ||
  4.     window.mozRequestAnimationFrame ||
  5.     function(callback){
  6.     window.setTimeout(callback, 1000 / 60);
  7.   };
  8. })();
  9.  
  10. function rafThrottle(fn) {
  11.   var busy = false;
  12.   return function() {
  13.     if (busy) return;
  14.     busy = true;
  15.     fn.apply(this, arguments);
  16.     requestAnimFrame(function() {
  17.       busy = false;
  18.     });
  19.   };
  20. };
  21.  
  22. $(document).ready(function() {
  23.  
  24.   var $wsPages = $(".ws-pages");
  25.   var $headings = $(".ws-text__heading");
  26.   var bgParts = 24;
  27.   var staggerVal = 65;
  28.   var staggerStep = 4;
  29.   var textH = $(".ws-text").height();
  30.   var winW = $(window).width();
  31.   var winH = $(window).height();
  32.   var curPage = 1;
  33.   var numOfPages = $(".ws-bg").length;
  34.   var changeAT = 0.5;
  35.   var waveStartDelay = 0.2;
  36.   var waveStagger = 0.013;
  37.   var waveBlocked = false;
  38.   var index = 1;
  39.   var startY = 0;
  40.   var deltaY = 0;
  41.   var headingsY = 0;
  42.   var $parts;
  43.  
  44.   function initBgs() {
  45.     var arr = [];
  46.     for (var i = 1; i <= bgParts; i++) {
  47.       var $part = $('<div class="ws-bg__part">');
  48.       $part.addClass("ws-bg__part-" + i);
  49.       arr.push($part);
  50.     }
  51.     $(".ws-bg").append(arr);
  52.     $wsPages.addClass("s--ready");
  53.     $parts = $(".ws-bg__part");
  54.     changePages();
  55.   };
  56.  
  57.   initBgs();
  58.  
  59.   function changePages() {
  60.     var y = (curPage - 1) * winH * -1;
  61.     var textY = textH * (curPage - 1) * -1;
  62.     var leftMax = index - 1;
  63.     var rightMin = index + 1;
  64.  
  65.     TweenMax.to($(".ws-bg__part-" + index), changeAT, {y: y});
  66.  
  67.     for (var i = leftMax; i > 0; i--) {
  68.       var d = (index - i) * waveStagger;
  69.       TweenMax.to($(".ws-bg__part-" + i), changeAT - d, {y: y, delay: d});
  70.     }
  71.  
  72.     for (var j = rightMin; j <= bgParts; j++) {
  73.       var d = (j - index) * waveStagger;
  74.       TweenMax.to($(".ws-bg__part-" + j), changeAT - d, {y: y, delay: d});
  75.     }
  76.  
  77.     TweenMax.to($headings, changeAT, {y: textY});
  78.   };
  79.  
  80.   function waveChange() {
  81.     waveBlocked = true;
  82.     var y = (curPage - 1) * winH * -1;
  83.     var textY = textH * (curPage - 1) * -1;
  84.     for (var i = 1; i <= bgParts; i++) {
  85.       var $part = $(".ws-bg__part-" + i);
  86.       var d = (i - 1) * waveStagger + waveStartDelay;
  87.       TweenMax.to($part, changeAT, {y: y, delay: d});
  88.     }
  89.  
  90.     TweenMax.to($headings, changeAT, {y: textY, delay: d});
  91.  
  92.     var delay = (changeAT + waveStagger * (bgParts - 1)) * 1000;
  93.     setTimeout(function() {
  94.       waveBlocked = false;
  95.     }, delay);
  96.   };
  97.  
  98.   function navigateUp() {
  99.     if (curPage > 1) curPage--;
  100.   };
  101.  
  102.   function navigateDown() {
  103.     if (curPage < numOfPages) curPage++;
  104.   };
  105.  
  106.   function navigateWaveUp() {
  107.     if (curPage === 1) return;
  108.     curPage--;
  109.     waveChange();
  110.   };
  111.  
  112.   function navigateWaveDown() {
  113.     if (curPage === numOfPages) return;
  114.     curPage++;
  115.     waveChange();
  116.   };
  117.  
  118.   function movePart($part, y) {
  119.     var y = y - (curPage - 1) * winH;
  120.     var headY = headingsY - (curPage - 1) * textH;
  121.     TweenMax.to($part, changeAT, {y: y, ease: Back.easeOut.config(4)});
  122.     TweenMax.to($headings, changeAT, {y: headY});
  123.   };
  124.  
  125.   function moveParts(y, index) {
  126.     var leftMax = index - 1;
  127.     var rightMin = index + 1;
  128.     var stagLeft = 0;
  129.     var stagRight = 0;
  130.     var stagStepL = -staggerStep;
  131.     var stagStepR = -staggerStep;
  132.     var sign = (y > 0) ? -1 : 1;
  133.  
  134.     movePart($(".ws-bg__part-" + index), y);
  135.  
  136.     for (var i = leftMax; i > 0; i--) {
  137.       var step = index - i;
  138.       stagStepL += (step <= 15) ? staggerStep : 1;
  139.       var sVal = staggerVal - stagStepL;
  140.       if (sVal < 0) sVal = 0;
  141.       stagLeft += sVal;
  142.       var nextY = y + stagLeft * sign;
  143.       if (Math.abs(y) < Math.abs(stagLeft)) nextY = 0;
  144.       movePart($(".ws-bg__part-" + i), nextY);
  145.     }
  146.  
  147.     for (var j = rightMin; j <= bgParts; j++) {
  148.       var step = j - index;
  149.       stagStepR += (step <= 15) ? staggerStep : 1;
  150.       var sVal = staggerVal - stagStepR;
  151.       if (sVal < 0) sVal = 0;
  152.       stagRight += sVal;
  153.       var nextY = y + stagRight * sign;
  154.       if (Math.abs(y) < Math.abs(stagRight)) nextY = 0;
  155.       movePart($(".ws-bg__part-" + j), nextY);
  156.     }
  157.   };
  158.  
  159.   var mousemoveHandler = rafThrottle(function(e) {
  160.     var y = e.pageY;
  161.     var x = e.pageX;
  162.     index = Math.ceil(x / winW * bgParts);
  163.  
  164.     deltaY = y - startY;
  165.     headingsY = textH * deltaY / winH;
  166.     moveParts(deltaY, index);
  167.   });
  168.  
  169.   var touchmoveHandler = rafThrottle(function(e) {
  170.     e.preventDefault();
  171.     var y = e.originalEvent.touches[0].pageY;
  172.     var x = e.originalEvent.touches[0].pageX;
  173.     index = Math.ceil(x / winW * bgParts);
  174.  
  175.     deltaY = y - startY;
  176.     headingsY = textH * deltaY / winH;
  177.     moveParts(deltaY, index);
  178.   });
  179.  
  180.   var swipeEndHandler = function() {
  181.     $(document).off("mousemove", mousemoveHandler);
  182.     $(document).off("touchmove", touchmoveHandler);
  183.     $(document).off("mouseup touchend", swipeEndHandler);
  184.  
  185.     if (!deltaY) return;
  186.  
  187.     if (deltaY / winH >= 0.5) navigateUp();
  188.     if (deltaY / winH <= -0.5) navigateDown();
  189.     changePages();
  190.   };
  191.  
  192.   $(document).on("mousedown touchstart", ".ws-bg__part", function(e) {
  193.     startY = e.pageY || e.originalEvent.touches[0].pageY;
  194.     deltaY = 0;
  195.  
  196.     $(document).on("mousemove", mousemoveHandler);
  197.     $(document).on("touchmove", touchmoveHandler);
  198.  
  199.     $(document).on("mouseup touchend", swipeEndHandler);
  200.   });
  201.  
  202.   $(document).on("mousewheel DOMMouseScroll", function(e) {
  203.     if (waveBlocked) return;
  204.     if (e.originalEvent.wheelDelta > 0 || e.originalEvent.detail < 0) {
  205.       navigateWaveUp();
  206.     } else { 
  207.       navigateWaveDown();
  208.     }
  209.   });
  210.  
  211.   $(document).on("keydown", function(e) {
  212.     if (waveBlocked) return;
  213.     if (e.which === 38) {
  214.       navigateWaveUp();
  215.     } else if (e.which === 40) {
  216.       navigateWaveDown();
  217.     }
  218.   });
  219.  
  220.   $(window).on("resize", function() {
  221.     winW = $(window).width();
  222.     winH = $(window).height();
  223.     changePages();
  224.   });
  225. });

基于 GSAP 全屏背景图片 jQuery 幻灯片插件

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

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

发表回复

热销模板

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

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