简体   繁体   English

jQuery cycle2:滚动浏览幻灯片

[英]jQuery cycle2: move through slides on scroll

I'm currently using the Cycle2 plugin to create a slideshow ( http://jquery.malsup.com/cycle2/ ), instead of triggering the slide change on click I want to trigger it when you scroll. 我目前正在使用Cycle2插件来创建幻灯片( http://jquery.malsup.com/cycle2/ ),而不是在单击时触发幻灯片更改,而我想在滚动时触发它。 Essentially I'm trying replicate something like this: http://loris-cormoreche.com but with the cycle2 plugin. 本质上,我正在尝试复制以下内容: http ://loris-cormoreche.com,但带有cycle2插件。

I have a codepen set up to demonstrate the problem: http://codepen.io/neal_fletcher/pen/NrRdmP If you scroll you'll see the problem I'm having, the images keep flickering and it cycles through more than one image at a time as well, in general just quite buggy. 我设置了一个代码笔来演示该问题: http ://codepen.io/neal_fletcher/pen/NrRdmP如果滚动,您将看到我所遇到的问题,图像不断闪烁,并且循环显示多个图像一次也是如此,一般来说,这是相当容易的问题。 The code I currently have is as follows: 我目前拥有的代码如下:

$('.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;
});

Any suggestions or ways to improve on this would be greatly appreciated! 任何建议或改进方法,将不胜感激!

The reason why it's buggy is because the event is triggered on every "scroll-click" or whatever you want to call it. 之所以会出现错误,是因为该事件是在每次“滚动点击”或您要调用的任何事件上触发的。 So when you scroll you are triggering it several times which makes it choppy like that. 因此,当您滚动时,您会触发几次,从而使它变得断断续续。

I fixed it in this codepen: http://codepen.io/LinusAronsson/pen/EygWpd 我在以下Codepen中修复了该问题: http ://codepen.io/LinusAronsson/pen/EygWpd

What I did was to change the jQuery to the following: 我所做的是将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);

});

I basically turn of the mousewheel event for 1200 milliseconds after it has been used, which is enough of a delay for it to not be triggered before the next picture has come into the view. 在使用完鼠标滚轮事件后,我基本上将其打开了1200毫秒,这足以使它在下一张图片进入视图之前不会被触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM