javascript jQuery,单击滚动窗口向下滚动到 div 顶部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14213986/
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-10-26 21:01:13  来源:igfitidea点击:

jQuery, on click scroll window down to top of the div

javascriptjquery

提问by Liam

I'm trying to make my browser window scroll down to the top of a div when clicked. The only problem is everything else works but that, the window should scroll down to the top of the div clicked...

我试图让我的浏览器窗口在单击时向下滚动到 div 的顶部。唯一的问题是其他一切正常,但窗口应该向下滚动到单击的 div 的顶部...

So far I have:

到目前为止,我有:

$('.work-showcase').click(function(){
    $('.work-showcase').animate({height:'135px'}, 500);
    $(this).animate({height:'400px'}, 500);
    $(window).scrollTop;
});

I've made a jsfiddle to show you what I mean... http://jsfiddle.net/Jq4Vw/

我做了一个 jsfiddle 来向你展示我的意思...... http://jsfiddle.net/Jq4Vw/

回答by CR41G14

This is how you scroll to the top of the div as long as the window isn't maxed out:

只要窗口没有最大化,这就是滚动到 div 顶部的方式:

$('.work-showcase').click(function(){

    $('html,body').animate({
        scrollTop: $(this).offset().top},
        'slow');
});

I am unsure what you were trying to achieve before scrolling

我不确定你在滚动之前想要实现什么

See it here jsFiddle

在这里看到它jsFiddle

回答by pala?н

Try this:

试试这个:

$('.work-showcase').click(function(){
    $('.work-showcase').animate({height:'135px'}, 500);
    $(this).animate({height:'400px'}, 500);
    $("html, body").animate({ scrollTop: $(this).offset().top }, 500);
});

回答by Anujith

See this: http://jsfiddle.net/Jq4Vw/4/

看到这个:http: //jsfiddle.net/Jq4Vw/4/

$('.work-showcase').click(function(){
   $('.work-showcase').animate({height:'135px'}, 500);
  $(this).animate({height:'400px'}, 500,function() {  
  $("html, body").animate({ scrollTop: $(this).offset().top });  
   });
 });

回答by Jai

I think you are trying to achieve this: http://jsfiddle.net/Jq4Vw/7/

我认为您正在努力实现这一目标:http: //jsfiddle.net/Jq4Vw/7/

$('.work-showcase').click(function(){
   $('.work-showcase').animate({height:'135px'}, 500);
   $(this).animate({height:'400px'}, 500).promise().done(function(){
       $('html,body').animate({scrollTop: $(this).offset().top},500);
       $(this).addClass('current').unbind('click'); // just add this line
   });
});

回答by Zeal Murapa

$('.work-showcase').click(function(){
    window.location = "#top";
});

make sure top ID is present.

确保存在顶部 ID。

<div id="top">
I am at the top of the document.
</div>

Working Fiddle

工作小提琴