【问题标题】:Animate scroll between sections - without a plugin部分之间的动画滚动 - 没有插件
【发布时间】:2014-03-12 12:34:38
【问题描述】:

我正在尝试创建一个脚本,该脚本可以在不使用插件的情况下为页面上的部分之间的滚动设置动画 - 只是纯 jquery。但是,当我尝试滚动页面时,似乎遇到了一些问题。谁能帮帮我?

这是我的jsFiddle 还有我的代码:

$(document).ready(function() {
        $('.main').bind('mousewheel', function(e){
            var mHeight=$(document).height()/8;
            console.log(mHeight);
            if(e.originalEvent.wheelDelta /120 > 0) {
                // alert('up');
                $('html, body').animate({
                    scrollTop: mHeight
                }, 1000);
            }
            else{
                // alert('down');
                $('html body').animate({
                    scrollTop: -mHeight
                }, 1000);
            }
        });
    });

【问题讨论】:

  • @AnoopJoshi 我想在部分之间设置动画和滚动,而不是在类似 alvarotrigo.com/fullPage 但没有插件的部分之间滚动

标签: javascript jquery scroll jquery-animate


【解决方案1】:

检查一下

$('body').bind('mousewheel', function (e) {
        e.stopPropagation();
        e.preventDefault();
        var mHeight = $(document).height() / 8;
        console.log(mHeight);
        if (e.originalEvent.wheelDelta / 120 > 0) {
            // alert('up');
            $('html, body').stop().animate({
                scrollTop: -mHeight
            }, 1000);
        }
        else {
            // alert('down');
            $('html body').stop().animate({
                scrollTop: mHeight
            }, 1000);
        }
    });

Demo

编辑

 var index = 1;
    $('body').bind('mousewheel', function (e) {
        e.stopPropagation();
        e.preventDefault();
        var mHeight = $(document).height() / 8;
        console.log(mHeight);
        if (e.originalEvent.wheelDelta / 120 > 0) {
            // alert('up');
            index--;
            mHeight = mHeight * index;
            $('html, body').stop().animate({
                scrollTop: mHeight
            }, 1000);
        }
        else {
            // alert('down');
            index++;
            mHeight = mHeight * index;
            $('html body').stop().animate({
                scrollTop: mHeight
            }, 1000);
        }
    });

Updated Fiddle

【讨论】:

  • 知道如何检测轮子/每次只执行一次功能 - 所以只更改一个部分?
  • 我认为,动画的完成事件可以帮助你。对此进行研究
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多