单击一个按钮滚动div?

时间:2014-09-12 00:53:52

标签: javascript jquery scrollto

点击#news .section-wrap时,我试图让页面滚动到.paging-navigation a。我尝试插入线(如下所示),但无法使其工作。我哪里错了?

$('#article-list').on('click', '.paging-navigation a', function(e){
    e.preventDefault();
    var link = $(this).attr('href');
    $('#article-list').scrollTo('#news .section-wrap');  // this is the line I added
    $('#article-list').fadeOut(500, function(){
        $(this).load(link + ' #article-list', function() {
            $(this).find('#article-list > *').unwrap().end().fadeIn(500);
        });
    });
});

3 个答案:

答案 0 :(得分:1)

您需要为htmlbody添加动画,并指向jQuery animate函数中的选择器。试试这个:

$('html, body').animate({
    scrollTop: $('#news .section-wrap').offset().top
}, 2000);

答案 1 :(得分:0)

尝试这样的事情:

$(".paging-navigation a").click(function(){
    $('html, body').animate({
        scrollTop: $("#news .section-wrap").offset().top
    }, 500);
});

您可能需要更改此代码中的某些内容,无论是计时还是某些错误,因为我目前无法对其进行测试。

希望它有用。

答案 2 :(得分:0)

scrollTo()不是本机jQuery方法。您可以使用第三方插件,例如http://lions-mark.com/jquery/scrollTo/http://demos.flesler.com/jquery/scrollTo/

正如jQuery scroll to element上的回答,你也可以让页面滚动到目标位置,如下所示:

$("#button").click(function() {
    $('html, body').animate({
        scrollTop: $("#elementtoScrollToID").offset().top
    }, 2000);
});
相关问题