javascript 滚动时如何使DIV始终位于页面底部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8942719/
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
How to make a DIV always at the bottom of the page when scrolling
提问by Exception
I am using the below code to make a DIV always at the bottom of the page when scrolled. But this is not working and goes on increasing the Page height.
我正在使用以下代码在滚动时始终在页面底部创建一个 DIV。但这不起作用,并继续增加页面高度。
var LSscrollingDiv = $("#LightSwitchMenuIt");
$(window).scroll(function(){
LSscrollingDiv
.stop()
.animate({"marginTop": ($(window).scrollTop() + $(window).height()) + "px"}, "slow" );
});
Please help me on this.
请帮我解决这个问题。
回答by karim79
Why not use straight CSS?
为什么不直接使用 CSS?
div.foo {
position: fixed;
bottom: 0px;
}
See:
看:
回答by devnull69
This might be a simple CSS issue ... you can place a DIV to a fixed position at the bottom of the viewport and it will always be there when scrolling, without any Javascript
这可能是一个简单的 CSS 问题……您可以将 DIV 放置在视口底部的固定位置,滚动时它将始终在那里,无需任何 Javascript
position: fixed;
bottom: 0px;
回答by minikomi
You can use css
你可以使用 css
position: fixed;
bottom: 0;
to avoid having to do this in javascript if you like.
如果您愿意,可以避免在 javascript 中执行此操作。
http://jsfiddle.net/A8BGJ/is a simple demonstration.
http://jsfiddle.net/A8BGJ/是一个简单的演示。
回答by user2727099
It may help to have width set as inherit too.
将宽度设置为继承也可能会有所帮助。
#div {
position: fixed;
bottom: 0;
width: inherit;
}
回答by Shaoz
Use this rules:
使用这个规则:
div {
position: fixed;
bottom: 0;
}
You can also use this technique in any parent block element.
您也可以在任何父块元素中使用此技术。