jQuery $("html").animate({ scrollTop: $(document).height() }, "slow"); 如果它在底部不要滚动它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5417080/
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
$("html").animate({ scrollTop: $(document).height() }, "slow"); if it's at the bottom don't scroll it
提问by Tara Irvine
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.
我有三个隐藏 div 的按钮,当我单击一个按钮时,我希望内容滚动到底部,然后当我单击另一个按钮时,我希望页面保持原样但加载新内容。
I've tried variables and if statements
我试过变量和 if 语句
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/5wshere, 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...
编辑:对不起,我真的不认为我已经解释过自己, http://ephemurl.com/4w/5ws这里是我目前所拥有的,最后 6 个部分弹跳到文档底部,但我希望这只发生一次,然后接下来的 5 次点击不要动画,因为你已经在那里了......
回答by Quincy
just use $(body) instead of $(html)
只需使用 $(body) 而不是 $(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
您还可以在事件触发器中使用 unbind 使其仅运行一次
$('#link-slide13').click(function(){
$("body").animate({ scrollTop: $(document).height() }, "slow");
$(this).unbind("click");
});
回答by Prakash
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
祝你好运
回答by Curlas
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")).
我们无法查看您的 html,但我认为您只将该功能应用于一键女巫 id="link-slide13"。如果它必须在三个按钮中起作用,请更改您的 JQuery 选择器。(向所有按钮添加一个类,您可以使用 $(".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.
您可以在函数末尾添加一个“return false”语句。这可以防止链接默认工作并且导航器转到文档顶部。
But, really...what's the problem?
但是,真的……有什么问题吗?