jquery - 单击正文滚动到下一个 div

标签 jquery onclick next scrollto css

我有很多 div,它们都带有“.slide”类,还有一个滚动功能,当单击一个 .slide div 时,它会滚动浏览它们。

看这里: http://codepen.io/thomasjwpayne/pen/KFotg

但是,我试图在单击正文而不是幻灯片时使该功能正常工作。此外,由于幻灯片显示为内联 block ,我需要在下一张幻灯片已完全显示在视口(viewport)中后滚动到幻灯片。

有人对此有任何指示吗?

最佳答案

你必须在某处保留一个跟踪器,在我的示例中 - 我使用 body 来保留一个 data-* 属性,该属性存储当前的 jQuery 对象当前在 View 中。

var currentSlide = $('body').data( 'current-slide', $('div.slide').eq(1) );

然后将 $.click() 处理程序从 $('div.slide') 切换到 $('body'):

$('body').click(function(e){
   e.preventDefault();
   //get the current slide index from the body tag.
   $this = currentSlide.data( 'current-slide' );
   $next = $this.next('div.slide');

   if($next.length) {
        $next.scrollTo($next.attr('id'));
        //Save the next slide as the current.
        $('body').data( 'current-slide', $next );
   } else {
        //Throw us back to the top.
        $('div.slide:first').scrollTo($('div.slide:first').attr('id'));
        //Save the first slide as the first slide, which 
        //Cycles us back to the top.
        $('body').data( 'current-slide', $('div.slide:first'));
   }

   $(".slide a").click(function(e) {
       e.stopPropagation();
   });
});

CodePen 演示: http://codepen.io/anon/pen/tBqhJ

关于jquery - 单击正文滚动到下一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21366375/

相关文章:

javascript - Jquery apply bg for just for one <td> not the whole column

javascript - JavaScript 或 Jquery 中的 onchange()

javascript - React Leaflet 弹出窗口在移动设备上不起作用

arrays - Angular4 - 通过点击显示数组中的下 5 个项目

javascript - fullcalendar.io - 在内存中保存事件

javascript - 插入 DOM 时图像闪烁

javascript - addEventListener:点击移除标签

lua - 有没有办法告诉 `next` 从特定键开始?

mysql - 在 MySQL 中查找 AUTO_INCREMENT 列的下一个值

javascript - 如何在 jquery 中使用 css 方法设置位置?