$(“html”).animate({ scrollTop: $(document).height() }, “slow”); if it's at the bottom don't scroll it

偶尔善良 提交于 2019-12-19 04:38:11

问题


I have three buttons which have hidden divs, when i click on one i want the content to scroll to the bottom, then when i click another I want the page to stay where it is but load the new content.

I've tried variables and if statements

var i=0;
$('#link-slide13').click(function(){
        if (i==0){//nothing's been scrolled
            $("html").animate({ scrollTop: $(document).height() }, "slow");
            i=1;

        }else{
            //don't do anything
        }
});

Any ideas? Thanks for the answers!

Edit: Sorry i don't really think i've explained myself, http://ephemurl.com/4w/5ws here, is what I have at the minute, the last 6 sections bounce to scroll to the bottom of the document, but i want this to only happen once and then for the next 5 clicks don't animate because you're already there...


回答1:


just use $(body) instead of $(html)

demo http://jsfiddle.net/APebY/

$(function(){
var i=0;
$('#link-slide13').click(function(){
        if (i==0){//nothing's been scrolled
            $("body").animate({ scrollTop: $(document).height() }, "slow");
            i=1;

        }else{
            //don't do anything
        }
});
});

You can also use unbind within the event trigger to make it run only once

$('#link-slide13').click(function(){

            $("body").animate({ scrollTop: $(document).height() }, "slow");

            $(this).unbind("click");
});



回答2:


Don't Know what exactly you're trying to do, I guess this should help you,

$('#link-slide13').toggle(function(){
    $("html").animate({ scrollTop: $(document).height() }, "slow");
},function(){
    //don't do anything
})

Good Luck




回答3:


We can't watch your html, but I think that you only have applied the function to one button witch id="link-slide13". If it must works in the three buttons change your JQuery selector. (adding a class to all buttons you can select all easy whith $(".className")).

Then, what is a button? a link an input? If the button is a link

<a href="#">

you can add a "return false" sentence at the end of the function. This prevent that the link works as default and the navegator go to the top of the document.

But, really...what's the problem?



来源:https://stackoverflow.com/questions/5417080/html-animate-scrolltop-document-height-slow-if-its-at-the-b

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!