幻灯片/轮播

可拖动切换全屏jQuery幻灯片特效

阿里云


这是一款可以拖动切换的全屏 jQuery 幻灯片特效。该幻灯片特效特效可以使用鼠标来前后切换,带分页圆点按钮,可以自动播放,并且带有一点视觉差效果。

HTML 结构:

该幻灯片的 HTML 结构如下:

也想出现在这里?联系我们
创客主机
  1. <div class="demo-container">
  2.   <div class="slides">
  3.     <div class="slide slide-1">
  4.       <div class="bg bg-1">
  5.         <div class="overlay">Slide #1</div><span class="hint">图片描述...</span>
  6.       </div>
  7.     </div>
  8.     <div class="slide slide-2">
  9.       <div class="bg bg-2">
  10.         <div class="overlay">Slide #2</div><span class="hint">图片描述...</span>
  11.       </div>
  12.     </div>
  13.     <div class="slide slide-3">
  14.       <div class="bg bg-3">
  15.         <div class="overlay">Slide #3</div><span class="hint">图片描述...</span>
  16.       </div>
  17.     </div>
  18.     <div class="slide slide-4">
  19.       <div class="bg bg-4">
  20.         <div class="overlay">Slide #4  图片描述...  </div>
  21.       </div>
  22.     </div>
  23.     <div class="slide slide-5">
  24.       <div class="bg bg-5">
  25.         <div class="overlay">Slide #5. 图片描述...</div>
  26.       </div>
  27.     </div>
  28.   </div>
  29.   <div class="slide-indicator">
  30.     <div where="1" class="dot dot-1"></div>
  31.     <div where="2" class="dot dot-2"></div>
  32.     <div where="3" class="dot dot-3"></div>
  33.     <div where="4" class="dot dot-4"></div>
  34.     <div where="5" class="dot dot-5"></div>
  35.   </div>
  36.   <div class="property-hack"></div>
  37. </div>

CSS 样式:

在页面中引入幻灯片需要的 style.css 文件。幻灯片的图片以背景图片的方式进行引入。

  1. <link rel="stylesheet" href="css/style.css">
  2. <link rel="stylesheet" type="text/css" href="css/normalize.css" />

JavaScript:

在页面中引入 jquery。该幻灯片为文档绑定鼠标事件和移动触摸 touch 事件,时幻灯片可以进行鼠标拖动和在移动设备中使用手指来进行滑动切换。

  1. $(document).ready(function() {
  2.  var curX = 0;
  3.  var diff = 0;
  4.  var curSlide = 1;
  5.  var busy = false;
  6.  var flag = false;
  7.  var winW = parseInt($(".demo-container").css("width"));
  8.  var animationTime = parseFloat($(".property-hack").css("height"));
  9.  var k = parseFloat($(".property-hack").css("width")) / 100;
  10.  var numOfSlides = $(".slide").length;
  11.  var autoChangeTime = parseFloat($(".property-hack").css("margin-left"));
  12.  var autoChangeInterval = setInterval(autoChangeFunction, autoChangeTime);
  13.  $(".slide-1 .overlay").addClass("overlay-active");
  14.  $(window).on("keydown", function(event) {
  15.   if (!busy) {
  16.    if (event.which == 37 && curSlide >= 2) {
  17.     curSlide--;
  18.     animation();
  19.    }
  20.    if (event.which == 39 && curSlide < numOfSlides) {
  21.     curSlide++;
  22.     animation();
  23.    }
  24.   }
  25.  });
  26.  $(".dot").on("click", function() {
  27.   if (!busy) {   
  28.    curSlide = parseInt($(this).attr("where"));
  29.    animation(true);   
  30.   }
  31.  });
  32.  $(document).on("mousedown touchstart", ".slides", function(event) {
  33.   if (!busy) {
  34.    clearInterval(autoChangeInterval);
  35.    flag = true;
  36.    $(".demo-container").addClass("grabbing");
  37.    winW = parseInt($(".demo-container").css("width"));
  38.    if (event.originalEvent.touches) var stX = event.originalEvent.touches[0].pageX;
  39.    else var stX = event.pageX;
  40.    $(document).on("mousemove touchmove", function(event) {
  41.     if (event.originalEvent.touches) var msX = event.originalEvent.touches[0].pageX;
  42.     else var msX = event.pageX;
  43.     diff = (msX - stX) / winW * 100 / k;
  44.     if (diff > 110) diff = 110;
  45.     if (diff < -110) diff = -110;
  46.     if (curSlide == 1) {
  47.      if (diff > 0) diff /= 10;
  48.      if (diff > 5) diff = 5;
  49.     }
  50.     if (curSlide == numOfSlides) {
  51.      if (diff < 0) diff /= 10;
  52.      if (diff < -5) diff = -5;
  53.     }
  54.     $(".slides").css("transform", "translate3d(" + (diff + curX) + "vw, 0, 0)");
  55.     $(".bg-" + curSlide).css("transform", "translate3d(" + (-diff / 2) + "vw, 0, 0)");
  56.     $(".bg-" + (curSlide + 1)).css("transform", "translate3d(" + (-50 - diff / 2) + "vw, 0, 0)");
  57.     $(".bg-" + (curSlide - 1)).css("transform", "translate3d(" + (50 - diff / 2) + "vw, 0, 0)");
  58.    });
  59.   }
  60.  });
  61.  $(document).on("mouseup touchend", function(event) {
  62.   if (flag) {
  63.    if (diff * k < -30) curSlide++;
  64.    else if (diff * k > 30)
  65.     curSlide--;
  66.    animation();
  67.    diff = 0;
  68.    $(document).off("mousemove");
  69.    $(".demo-container").removeClass("grabbing");
  70.    flag = false;
  71.   }
  72.  });
  73.  
  74.  function animation(r) {
  75.   busy = true;
  76.   curX = (-curSlide + 1) * 100;
  77.   $(".overlay").removeClass("overlay-active");  
  78.   $(".slide-" + curSlide + " .overlay").addClass("overlay-active");
  79.   $(".dot").css("opacity", "0.3");
  80.   $(".dot-" + curSlide).css("opacity", "1");
  81.   $(".slides").addClass("animation");
  82.   if (!r)  $(".bg").addClass("animation");
  83.   else $(".bg").css("transform", "translate3d(0, 0, 0)");
  84.   $(".slides").css("transform", "translate3d(-" + (curSlide - 1) * 100 + "vw, 0, 0)");
  85.   $(".bg").css("transform", "translate3d(0, 0, 0)");
  86.   if (!r) {
  87.    $(".bg-" + (curSlide + 1) + ", .bg-" + (curSlide + 2)).css("transform", "translate3d(-50vw, 0, 0)");
  88.    $(".bg-" + (curSlide - 1) + ", .bg-" + (curSlide - 2)).css("transform", "translate3d(50vw, 0, 0)");
  89.   }
  90.   clearInterval(autoChangeInterval);
  91.   setTimeout(function() {
  92.    $(".slides, .bg").removeClass("animation");
  93.    busy = false;
  94.    $(".bg-" + (curSlide + 1)).css("transform", "translate3d(-50vw, 0, 0)");   
  95.    autoChangeInterval = setInterval(autoChangeFunction, autoChangeTime);
  96.   }, animationTime);
  97.  }
  98.  
  99.  function autoChangeFunction() {
  100.   if (curSlide != numOfSlides) {
  101.    curSlide++;
  102.    animation();
  103.   }
  104.   else {
  105.    curSlide = 1;
  106.    animation(true);
  107.   }
  108.  }
  109. });

可拖动切换全屏 jQuery 幻灯片特效

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

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

发表回复

热销模板

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

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