简体   繁体   English

jQuery平滑滚动

[英]jQuery smooth scroll

I'm in a bit of a bind. 我有点束缚。 I've been searching for a way to scroll smoothly through divs and found this fiddle: 我一直在寻找一种在div中平滑滚动的方法,发现了这个小提琴:

http://jsfiddle.net/Vk7gB/187/ http://jsfiddle.net/Vk7gB/187/

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://trevordavis.net/play/jquery-one-page-nav/jquery.scrollTo.js"></script>
<script type="text/javascript" src="http://trevordavis.net/play/jquery-one-page-nav/jquery.nav.min.js"></script>
<script type="text/javascript" src="http://mottie.github.io/Keyboard/js/jquery.mousewheel.js"></script>
<script type="text/javascript" src="scroll.js"></script>

The problem is that, while it works perfectly on the jsfiddle site, when i copy it exactly the same, without any changes, it stops working for some reason. 问题是,尽管它在jsfiddle站点上可以正常工作,但是当我完全相同地复制它而没有任何更改时,由于某种原因它会停止工作。

I've triple checked all external scripts and yet I can't find out what is the problem. 我已经对所有外部脚本进行了三重检查,但仍然找不到问题所在。

Here's the exact same code, copied directly from the fiddle and it does not work. 这是完全相同的代码,直接从小提琴复制而来,它不起作用。

http://www.zero-blade.com/work/test2/ http://www.zero-blade.com/work/test2/

If anyone can point me in the right direction, I would greatly appreciate it. 如果有人能指出正确的方向,我将不胜感激。

You refer in your code to DOM elements, but they are not ready yet. 您在代码中引用了DOM元素,但尚未准备好。 Put all your <script> tags just before closing </body> tag. 将所有<script>标记放在</body>标记之前。

The JavaScript in the fiddle is configured to execute when the DOM is ready. 提琴中的JavaScript配置为在DOM准备就绪时执行。 The JavaScript in your site (scroll.js) is being inserted and executed in the HEAD of your document, before the DOM elements exist, so no bindings are occurring. 在DOM元素存在之前,您网站中的JavaScript(scroll.js)已插入并在文档的HEAD中执行,因此不会发生绑定。

All of your JavaScript should be at the end of the body, and moving scroll.js to the end of the body will solve the issue. 您所有的JavaScript都应放在正文的末尾,将scroll.js移到正文的末尾将解决此问题。

If you can't move the link to scroll.js, you can use jQuery's document.ready() in scroll.js to trigger the bindings to occur AFTER the DOM is ready, as follows: 如果无法将链接移动到scroll.js,则可以在DOM准备就绪后使用Scroll.js中的jQuery的document.ready()触发绑定,如下所示:

scroll.js scroll.js

var $current, flag = false;

$(function() { 
    // This code will be executed when the DOM is ready.
    // This is a short version of $(document).ready(){...}

    $('#nav').onePageNav();

    $('body').mousewheel(function(event, delta) {
        if (flag) { return false; }
        $current = $('div.current');

        if (delta > 0) {
            $prev = $current.prev();

            if ($prev.length) {
                flag = true;
                $('body').scrollTo($prev, 1000, {
                    onAfter : function(){
                        flag = false;
                    }
                });
                $current.removeClass('current');
                $prev.addClass('current');
            }
        } else {
            $next = $current.next();

            if ($next.length) {
                flag = true;
                $('body').scrollTo($next, 1000, {
                    onAfter : function(){
                        flag = false;
                    }
                });
                $current.removeClass('current');
                $next.addClass('current');
            }
        }

        event.preventDefault();
    });

});

Because in jsFiddle, the code runs inside the DOMReady event ( look at the drop down under the jQuery version in the fiddle ). 因为在jsFiddle中,代码在DOMReady事件中运行( 请看小提琴中jQuery版本下的下拉列表 )。

Wrap all your code inside 将所有代码包装在内

$(function(){
  // you code here
});

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

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