簡體   English   中英

jQuery,單擊滾動窗口向下到div頂部

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

我試圖使我的瀏覽器窗口在單擊時向下滾動到div的頂部。 唯一的問題是其他所有東西都可以正常工作,但是,該窗口應該向下滾動到所單擊的div的頂部...

到目前為止,我有:

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

我做了一個jsfiddle,向您展示我的意思... http://jsfiddle.net/Jq4Vw/

只要窗口沒有最大化,這就是滾動到div頂部的方式:

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

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

我不確定滾動之前您要達到的目標

在這里看到j​​sFiddle

嘗試這個:

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

看到這個: 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 });  
   });
 });

我認為您正在嘗試實現這一目標: 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
   });
});
$('.work-showcase').click(function(){
    window.location = "#top";
});

確保存在頂部ID。

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

工作小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM