javascript - jQuery 周期2 : move through slides on scroll

标签 javascript jquery html jquery-cycle

我目前正在使用 Cycle2 插件创建幻灯片 ( http://jquery.malsup.com/cycle2/ ),而不是在单击时触发幻灯片更改,我想在滚动时触发它。本质上我正在尝试复制这样的东西:http://loris-cormoreche.com但使用cycle2插件。

我设置了一个 codepen 来演示该问题:http://codepen.io/neal_fletcher/pen/NrRdmP如果你滚动,你会看到我遇到的问题,图像不断闪烁,并且一次循环显示多个图像,一般来说非常有问题。 我目前的代码如下:

$('.cycle-slideshow').on( 'DOMMouseScroll mousewheel', function ( event ) {
    if( event.originalEvent.detail > 0 || event.originalEvent.wheelDelta < 0 ) { //alternative options for wheelData: wheelDeltaX & wheelDeltaY
        $('.cycle-slideshow').cycle('next');
        console.log('Down');
    } else {
       $('.cycle-slideshow').cycle('prev');
       console.log('Up');
    }
    //prevent page fom scrolling
    return false;
});

任何建议或改进方法将不胜感激!

最佳答案

它有问题的原因是因为每次“滚动单击”或任何您想要调用的事件都会触发该事件。因此,当您滚动时,您会多次触发它,这使得它变得不稳定。

我在这个代码笔中修复了它:http://codepen.io/LinusAronsson/pen/EygWpd

我所做的是将 jQuery 更改为以下内容:

var scrollDisabled = false;
$('.cycle-slideshow').on('DOMMouseScroll mousewheel', function(event) {

 if (scrollDisabled)
     return;

  if (event.originalEvent.detail > 0 || event.originalEvent.wheelDelta < 0) { //alternative options for wheelData: wheelDeltaX & wheelDeltaY
  $('.cycle-slideshow').cycle('next');
    console.log('Down');
  } else {
    $('.cycle-slideshow').cycle('prev');
  console.log('Up');
    }
   //prevent page fom scrolling

   scrollDisabled = true;
   setTimeout(function(){scrollDisabled = false;}, 1200);

});

我基本上在使用鼠标滚轮事件后 1200 毫秒将其关闭,这足以使其在下一张图片进入 View 之前不会被触发。

关于javascript - jQuery 周期2 : move through slides on scroll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37902047/

相关文章:

javascript - Ecma6 promise : how to use Ecma6 promise to rewrite jquery $. 什么时候?

jquery - 滚动时创建 "underline to circle"动画

php - 简单 SSL - 我需要做什么才能使我的 ajax 登录 SSL 安全

php - 提交表单后如何从(国际电话输入)插件获取国家代码?

html - 粘性页脚不是那么粘

javascript - 滚动到每个 span 元素

javascript - SQLite回调高效解决方案

javascript - 为什么创建一个新的繁琐连接会使我的程序无法完成?

javascript - 由于 background-url 样式,Internet Explorer 的安全消息

javascript - 在 IE8 中替换大下拉列表的最快方法