I need to have a div box in a static position meaning that when someone scrolls down the page, the div stays in the same po开发者_JS百科sition.
I have googled a lot and I found some solutions, but they were all using defined positions like top left, top right etc.. and I need a solution that will work regardless of the place the div is in. So basically the script needs to either take the current position and set that to the fixed position, or not work with fixed X/Y position..
Any ideas?
Thanks,
You need to use position:fixed
for your element.
Example:
#dv{
position:fixed; /* this is important for you */
width:200px;
height:200px;
background:blue;
}
Check out the example
Notice that div remains there even if you scroll :)
http://jsfiddle.net/Shaz/Fqr4t/
I'm not entirely sure what you mean by 'regardless of the place the div is in', however, if you can't use position: fixed, there is an onscroll event in Javascript that you can hook into. You can update the position of the div there. Do note that this is usually isn't fast enough to look fluent.
精彩评论