简体   繁体   English

一旦它到达页面顶部(滚动时),如何将css添加到div?

[英]How to add css to a div once it hits the top of the page (when scrolling)?

I would like to make it so when user scrolls down and reaches a certain div, say #float, set that div to margin-top: 50px and position fixed, and if user scrolls back up undo those changes. 当用户向下滚动并到达某个div时,我想这样做,比如#float,将div设置为margin-top:50px并且位置固定,如果用户向后滚动则撤消这些更改。 It's hard to understand I know ))) If you go to this page and pay your attention to sidebar once scrolling up and down you will see what I mean. 很难理解我知道)))如果你进入这个页面 ,一旦上下滚动就注意侧边栏,你会看到我的意思。

As you scroll down 2nd advertisement scrolls with a page too. 当您向下滚动第二个广告滚动页面时也是如此。

How would I achieve same functionality with jQuery/CSS? 我如何使用jQuery / CSS实现相同的功能?

This is a way of doing it in jQuery. 这是在jQuery中实现它的一种方式。

This code is provided for example purposes only; 此代码仅用于示例目的; there are almost certainly a handful of regularly-maintained jQuery plugins that will do this thing for you - check GitHub or DailyJS . 几乎肯定有一些定期维护的jQuery插件会为你做这件事 - 检查GitHubDailyJS

$(window).scroll(function() {
    var styledDiv = $('#styledDiv'),
        targetScroll = $('#float').position().top,
        currentScroll = $('html').scrollTop() || $('body').scrollTop();

    styledDiv.toggleClass('fixedPos', currentScroll >= targetScroll);
});

Here is a simple JSFiddle of the above in action. 这是一个简单的JSFiddle以上的实际操作。

Edit : Have now refactored this code to a more elegant solution. 编辑 :现在已经将此代码重构为更优雅的解决方案。

Edit 2 : Following an email I received about a question, I've updated the code above so that it also works in Firefox. 编辑2 :在收到有关问题的电子邮件后,我更新了上面的代码,以便它也适用于Firefox。 As $('body').scrollTop() will not work in Firefox (See comments on the jQuery API page ), we need to check both the html and body elements. 作为$('body').scrollTop()在Firefox中不起作用(请参阅jQuery API页面上的评论),我们需要检查htmlbody元素。

This is the relevant jQuery/JavaScript code use on that site. 这是该站点上使用的相关jQuery / JavaScript代码。

  if (window.XMLHttpRequest) {
    var topGagStay = $("top-gag-stay");
    var isLoggedIn = $("profile-menu") ? true : false;
    var sidebarAdsTop = 1061 - 545;
    var signupBtnOffset = 60;
    var dockPos = 72;
    if (!isLoggedIn && !GAG.isReadOnly()) {
      sidebarAdsTop += signupBtnOffset
    }
    if (formMessageShown) {
      sidebarAdsTop += formMessageOffset
    }
    if (topGagStay) {
      if (document.documentElement.scrollTop > sidebarAdsTop || self.pageYOffset > sidebarAdsTop) {
        if (topGagStay.style.position != "fixed") {
          topGagStay.style.position = "fixed";
          topGagStay.style.top = dockPos + "px"
        }
      } else {
        if (document.documentElement.scrollTop < sidebarAdsTop || self.pageYOffset < sidebarAdsTop) {
          topGagStay.style.position = "";
          topGagStay.style.top = ""
        }
      }
    }
  }

Thank FireBug and http://jsbeautifier.org/ for the code (and 9GAG, of course). 感谢FireBug和http://jsbeautifier.org/代码(当然还有9GAG)。

I have tried the above answer by beardtwizzle and it worked fine. 我已经通过beardtwizzle尝试了上述答案,并且工作正常。 Also made it work for the case when the page is scrolled upto the bottom of the page. 还使它适用于页面滚动到页面底部的情况。

see the working demo /tutorial here 在这里查看工作演示 /教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM