如何使一个Div保持在它的位置? [英] How can I make a Div that stays on its position?
问题描述
我有一个困难的时候,如何正确描述我问的,但这里是最好的例子从 boingboing.net 。看看侧边栏,然后向下滚动到底部。注意最后一个小部件(我不知道是什么,对不起。)或广告停留在其地方,当用户向下滚动时?它也到达底部时停止。我怎么会这样做?
I 'm having a hard time how to properly describe what i'm asking but here's the best example from boingboing.net. Look at the sidebar then scroll down to the bottom. Notice how the last widget(i dont know what it is, sorry.) or Advertise stays at its place when the user scrolls down? It also stops when reaching the bottom. how am i gonna do that?
我知道css属性, position:fixed;
但我猜这个方法不同于boingboing.net。需要帮助这件事。任何帮助?非常感谢!
i know the css attributes, position:fixed;
but i guess that method is different from boingboing.net. need help on this matter. any help? Thank you very much!
推荐答案
在窗口滚动时绑定事件
,并根据窗口的
scrolltop
设置目标div的位置
c $ c>。
Bind an event on scroll of window
, and set the position
of target div, based on the scrolltop
of window
.
$(window).scroll(function () {
if($(this).scrollTop() >= 500){
$('targetdiv').css('position','fixed');
}
else {
$('targetdiv').css('position','relative'); // or any other position
}
});
或更好您可以添加类
窗口的特定顶部
其中有 position:fixed
or better you can add a class
on particular top of window
which has position:fixed
这篇关于如何使一个Div保持在它的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!