开发者

How can I keep a fixed element contained inside a relative element?

开发者 https://www.devze.com 2023-01-21 04:05 出处:网络
I have a fixed element (div) that I want开发者_Go百科 to scroll with my page. However, when the fixed element reaches the end of the container (div) I want it to stop scrolling with the page.

I have a fixed element (div) that I want开发者_Go百科 to scroll with my page. However, when the fixed element reaches the end of the container (div) I want it to stop scrolling with the page.

I'm trying to do this with CSS, or is jquery my best option?


Elements with position:fixed are positioned relative to the body, not the containing element. You probably won't be able to do this without using JavaScript.


You could add an event with $(window).scroll() and compare the position of the div container and the fixed element.

When they are equal switch the position style from fixed to absolute and set left and top accordingly. When the user scolls back, do the opposite.

0

精彩评论

暂无评论...
验证码 换一张
取 消