简体   繁体   中英

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

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...

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/

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

$('.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

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);
});

See this: 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 });  
   });
 });

I think you are trying to achieve this: 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";
});

make sure top ID is present.

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

Working Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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