首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向下翻页单击滚动div适合查看端口

向下翻页单击滚动div适合查看端口
EN

Stack Overflow用户
提问于 2016-09-23 16:30:02
回答 1查看 110关注 0票数 1

我有四个div,它们都适合查看端口。我想滚动下一个div适合查看页面向下按钮点击(键码:34) .Now我为鼠标滚动做了修改。我想通过page down/up键修改默认div 1在page Down键顶部按down键div 2在view port上,反之亦然

代码语言:javascript
复制
//Set each section's height equals to the window height
    $('.clildClass').height($(window).height());
    /*set the class 'active' to the first element 
     this will serve as our indicator*/
    $('.clildClass').first().addClass('active');

    /* handle the mousewheel event together with 
     DOMMouseScroll to work on cross browser */
    $(document).on('mousewheel DOMMouseScroll', function (e) {
        e.preventDefault();//prevent the default mousewheel scrolling
        var active = $('.active');
        //get the delta to determine the mousewheel scrol UP and DOWN
        var delta = e.originalEvent.detail < 0 || e.originalEvent.wheelDelta > 0 ? 1 : -1;
        
        //if the delta value is negative, the user is scrolling down
        if (delta < 0) {
            //mousewheel down handler
            next = active.next();
            //check if the next section exist and animate the anchoring
            if (next.length) {
               /*setTimeout is here to prevent the scrolling animation
                to jump to the topmost or bottom when 
                the user scrolled very fast.*/
                var timer = setTimeout(function () {
                    /* animate the scrollTop by passing 
                    the elements offset top value */
                    $('body, html').animate({
                        scrollTop: next.offset().top
                    }, 'slow');
                    
                    // move the indicator 'active' class
                    next.addClass('active')
                        .siblings().removeClass('active');
                    
                    clearTimeout(timer);
                }, 800);
            }

        } else {
            //mousewheel up handler
            /*similar logic to the mousewheel down handler 
            except that we are animate the anchoring 
            to the previous sibling element*/
            prev = active.prev();

            if (prev.length) {
                var timer = setTimeout(function () {
                    $('body, html').animate({
                        scrollTop: prev.offset().top
                    }, 'slow');

                    prev.addClass('active')
                        .siblings().removeClass('active');
                    
                    clearTimeout(timer);
                }, 800);
            }

        }
    });
代码语言:javascript
复制
.clildClass{
   min-height:100vh;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parentClass">
    <div class="clildClass" id="div1">
        div one
    </div>
    <div class="clildClass" id="div2">
       div two
    </div>
    <div class="clildClass" id="div3">
       div three
    </div>
    <div class="clildClass" id="div4">
       div four
    </div>
</div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-23 17:04:47

尝试以下操作:

代码语言:javascript
复制
//Set each section's height equals to the window height
$('.clildClass').height($(window).height());
/*set the class 'active' to the first element 
 this will serve as our indicator*/
$('.clildClass').first().addClass('active');

$(window).on('keydown', function(e) {//use the keydown event
 var active = $('.active');
  e.preventDefault(); //prevent the default mousewheel scrolling

console.log(2)
  if (e.keyCode == 40) {//test if the key is the down arrow

    next = active.next();
    //check if the next section exist and animate the anchoring
    if (next.length) {
      /*setTimeout is here to prevent the scrolling animation
       to jump to the topmost or bottom when 
       the user scrolled very fast.*/
      var timer = setTimeout(function() {
        /* animate the scrollTop by passing 
        the elements offset top value */
        $('body, html').animate({
          scrollTop: next.offset().top
        }, 'slow');

        // move the indicator 'active' class
        next.addClass('active')
          .siblings().removeClass('active');

        clearTimeout(timer);
      }, 800);
    }

  } else if (e.keyCode == 38) {//test if the key is the up arrow
    prev = active.prev();

    if (prev.length) {
      var timer = setTimeout(function() {
        $('body, html').animate({
          scrollTop: prev.offset().top
        }, 'slow');

        prev.addClass('active')
          .siblings().removeClass('active');

        clearTimeout(timer);
      }, 800);
    }

  }
});

演示:https://jsfiddle.net/rpxzv4ae/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39656214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档