如何让元素在页面上随你滚动?

发布于 2024-09-24 21:13:52 字数 304 浏览 0 评论 0原文

我想在网站上创建一个当您向下滚动窗口时滚动的元素。在大多数情况下,我知道这可以通过使用固定定位的一些基本 CSS 轻松解决。问题是,我不希望它始终位于屏幕上的同一空间。我希望它成为一个侧边栏元素,从其余内容开始,然后在您向下滚动页面时随您移动。有关示例,请参阅 http://www.demonoid.com/(请参阅右侧的广告)页面)。我知道可能有一个相当简单的 css 或 jquery 解决方案,但我一直想不出一个。我很感激你的帮助。

谢谢!

I would like to create an element on a website that scrolls as you scroll down the window. In most cases, I know this could easily be solved with some basic css using fixed positioning. The issue is, I would not like it to be in the same space on the screen at all times. I would like it to be a sidebar element that starts with the rest of the content, but then moves with you as you scroll down the page. See http://www.demonoid.com/ for an example (see the ad on the right side of the page). I know there is probably a fairly easy css or jquery solution, but I have not been able to think of one. I appreciate the help.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天涯沦落人 2024-10-01 21:13:52

这是一个使用 jQuery 创建固定浮动元素的非常棒的教程:

固定浮动元素 |面向设计师的 jQuery

Here's a really great tutorial to create fixed-floating elements using jQuery:

Fixed Floating Elements | jQuery for Designers

叶落知秋 2024-10-01 21:13:52

也许只是一个

   if($(window).scrollTop>300) {

        $("#image").position().top($(window).scrollTop()-$(window).height());

   }

Maybe just a

   if($(window).scrollTop>300) {

        $("#image").position().top($(window).scrollTop()-$(window).height());

   }
属性 2024-10-01 21:13:52

这是一种制作方法。

使用 position:sticky 使元素浮动。

这里是文章的解释。

http://updates.html5rocks.com /2012/08/Stick-your-landings-position-sticky-lands-in-WebKit

粘性位置演示

Here is a way to make it.

use position:sticky to get element floating.

Here is the article explained.

http://updates.html5rocks.com/2012/08/Stick-your-landings-position-sticky-lands-in-WebKit

sticky position demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文