拥有“位置:固定”的最简单的 jQuery 方法是什么? (始终在顶部)div?

发布于 2024-07-07 22:28:18 字数 283 浏览 8 评论 0 原文

我对 jQuery 比较陌生,但到目前为止我所看到的都是我喜欢的。 我想要的是 div (或任何元素)位于页面顶部,就好像“position:fixed”在每个浏览器中都有效一样。

我不想要复杂的东西。 我不想要巨大的 CSS hack。 如果只使用 jQuery(版本 1.2.6)就足够了,我会更喜欢,但如果我需要 jQuery-UI-core,那也很好。

我试过 $("#topBar").scrollFollow(); <-- 但这进展缓慢......我希望某些东西看起来真正固定。

I'm relatively new to jQuery, but so far what I've seen I like. What I want is for a div (or any element) to be across the top of the page as if "position: fixed" worked in every browser.

I do not want something complicated. I do not want giant CSS hacks. I would prefer if just using jQuery (version 1.2.6) is good enough, but if I need jQuery-UI-core, then that's fine too.

I've tried $("#topBar").scrollFollow(); <-- but that goes slow... I want something to appear really fixed.

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

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

发布评论

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

评论(6

雨的味道风的声音 2024-07-14 22:28:18

使用此 HTML:

<div id="myElement" style="position: absolute">This stays at the top</div>

这是您要使用的 javascript。 它将一个事件附加到窗口的滚动,并将元素向下移动到您滚动的距离。

$(window).scroll(function() {
    $('#myElement').css('top', $(this).scrollTop() + "px");
});

正如下面的评论所指出的,不建议将事件附加到滚动事件 - 当用户滚动时,它会触发很多,并且可能会导致性能问题。 考虑将其与 Ben Alman 的 debounce/throttle 插件一起使用以减少开销。

Using this HTML:

<div id="myElement" style="position: absolute">This stays at the top</div>

This is the javascript you want to use. It attaches an event to the window's scroll and moves the element down as far as you've scrolled.

$(window).scroll(function() {
    $('#myElement').css('top', $(this).scrollTop() + "px");
});

As pointed out in the comments below, it's not recommended to attach events to the scroll event - as the user scrolls, it fires A LOT, and can cause performance issues. Consider using it with Ben Alman's debounce/throttle plugin to reduce overhead.

故人爱我别走 2024-07-14 22:28:18

对于那些支持“position:fixed”的浏览器,您可以简单地使用 javascript (jQuery) 在滚动时将位置更改为“fixed”。 这消除了使用此处列出的 $(window).scroll(function()) 解决方案滚动时的跳跃现象。

Ben Nadel 在他的教程中演示了这一点:
创建有时固定位置带有 jQ​​uery 的元素

For those browsers that do support "position: fixed" you can simply use javascript (jQuery) to change the position to "fixed" when scrolling. This eliminates the jumpiness when scrolling with the $(window).scroll(function()) solutions listed here.

Ben Nadel demonstrates this in his tutorial:
Creating A Sometimes-Fixed-Position Element With jQuery

耶耶耶 2024-07-14 22:28:18

美丽的! 你的解决方案是99%...而不是“this.scrollY”,我使用“$(window).scrollTop()”。 更好的是,这个解决方案只需要 jQuery1.2.6 库(不需要额外的库)。

我特别想要该版本的原因是因为这是目前 MVC 附带的版本。

这是代码:

$(document).ready(function() {
    $("#topBar").css("position", "absolute");
});

$(window).scroll(function() {
    $("#topBar").css("top", $(window).scrollTop() + "px");
});

Beautiful! Your solution was 99%... instead of "this.scrollY", I used "$(window).scrollTop()". What's even better is that this solution only requires the jQuery1.2.6 library (no additional libraries needed).

The reason I wanted that version in particular is because that's what ships with MVC currently.

Here's the code:

$(document).ready(function() {
    $("#topBar").css("position", "absolute");
});

$(window).scroll(function() {
    $("#topBar").css("top", $(window).scrollTop() + "px");
});
冬天的雪花 2024-07-14 22:28:18

HTML/CSS 方法

如果您正在寻找一个不需要太多 JavaScript 的选项(以及随之而来的所有问题,例如快速滚动事件调用),则可以获得相同的效果通过添加包装器

和一些样式来实现行为。 当我使用以下方法时,我注意到滚动更加平滑(没有元素滞后):

JS Fiddle

HTML

<div id="wrapper">
  <div id="fixed">
    [Fixed Content]
  </div><!-- /fixed -->
  <div id="scroller">
    [Scrolling Content]
  </div><!-- /scroller -->
</div><!-- /wrapper -->

CSS

#wrapper { position: relative; }
#fixed { position: fixed; top: 0; right: 0; }
#scroller { height: 100px; overflow: auto; }

JS

//Compensate for the scrollbar (otherwise #fixed will be positioned over it).
$(function() {
  //Determine the difference in widths between
  //the wrapper and the scroller. This value is
  //the width of the scroll bar (if any).
  var offset = $('#wrapper').width() - $('#scroller').get(0).clientWidth;

  //Set the right offset
  $('#fixed').css('right', offset + 'px');​
});

当然,可以修改此方法以用于在运行时获取/丢失内容的滚动区域(这将导致滚动条的添加/删除)。

HTML/CSS Approach

If you are looking for an option that does not require much JavaScript (and and all the problems that come with it, such as rapid scroll event calls), it is possible to gain the same behavior by adding a wrapper <div> and a couple of styles. I noticed much smoother scrolling (no elements lagging behind) when I used the following approach:

JS Fiddle

HTML

<div id="wrapper">
  <div id="fixed">
    [Fixed Content]
  </div><!-- /fixed -->
  <div id="scroller">
    [Scrolling Content]
  </div><!-- /scroller -->
</div><!-- /wrapper -->

CSS

#wrapper { position: relative; }
#fixed { position: fixed; top: 0; right: 0; }
#scroller { height: 100px; overflow: auto; }

JS

//Compensate for the scrollbar (otherwise #fixed will be positioned over it).
$(function() {
  //Determine the difference in widths between
  //the wrapper and the scroller. This value is
  //the width of the scroll bar (if any).
  var offset = $('#wrapper').width() - $('#scroller').get(0).clientWidth;

  //Set the right offset
  $('#fixed').css('right', offset + 'px');​
});

Of course, this approach could be modified for scrolling regions that gain/lose content during runtime (which would result in addition/removal of scrollbars).

青春如此纠结 2024-07-14 22:28:18

对于仍在 IE 6 中寻找简单解决方案的人。我创建了一个插件来处理 IE 6 位置:已修复问题并且非常易于使用:http://www.fixedie.com/

我编写它是为了模仿 belatedpng 的简单性,其中唯一需要的更改是添加脚本并调用它。

For anyone still looking for an easy solution in IE 6. I created a plugin that handles the IE 6 position: fixed problem and is very easy to use: http://www.fixedie.com/

I wrote it in an attempt to mimic the simplicity of belatedpng, where the only changes necessary are adding the script and invoking it.

最冷一天 2024-07-14 22:28:18

在一个项目中,我的客户希望在另一个 div 中有一个浮动框,因此我使用 margin-top CSS 属性而不是 top ,以便我的浮动框保留在其父级中。

In a project, my client would like a floating box in another div, so I use margin-top CSS property rather than top in order to my floating box stay in its parent.

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