scrollTo 函数 jQuery 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18754050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:25:52  来源:igfitidea点击:

scrollTo function jQuery is not working

jqueryscrollto

提问by Matt Andrzejczuk

I need Jquery's scrollTo function to work for this website: http://cinicraft.com/Silverman/index.html

我需要 Jquery 的 scrollTo 功能来为这个网站工作:http: //cinicraft.com/Silverman/index.html

I've tried the following

我试过以下

$(document).ready(function()
{
    $("div.btnLp3").click(function(){
        $('body,html').scrollTo('#target-examples', 800, {easing:'elasout'});
    });
});

And I've tried this too:

我也试过这个:

$(document).ready(function()
{
    $("div.btnLp3").click(function(){
        $('html, body').animate({ scrollTop: $(window.location.hash).offset().top}, 1000);
    });
});

I can't seem to get scrollToworking at all. Does anyone have any ideas? Here's what I have imported:

我似乎根本无法scrollTo工作。有没有人有任何想法?这是我导入的内容:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">  </script>    
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js">  </script>

回答by Khawer Zeshan

Try this

尝试这个

$("#clickme").click(function() {
    $('html, body').animate({
        scrollTop: $("#wrap2").offset().top
    }, 2000);
    return false;
});

FIDDLE

小提琴

回答by CountZero

/*
* ScrollToElement 1.0
* Copyright (c) 2009 Lauri Huovila, Neovica Oy
*  [email protected]
*  http://www.neovica.fi
*  
* Dual licensed under the MIT and GPL licenses.
*/

(function($) {
    $.scrollToElement = function($element, speed) {

        speed = speed || 750;

        $("html, body").animate({
            scrollTop: $element.offset().top,
            scrollLeft: $element.offset().left
        }, speed);
        return $element;
    };

    $.fn.scrollTo = function(speed) {
        speed = speed || "normal";
        return $.scrollToElement(this, speed);
    };
})(jQuery);

回答by grecio beline

 $("#your-div")[0].scrollTo(0, Number.MAX_SAFE_INTEGER);