简体   繁体   English

CSS / JS在页面滚动到某个点时将class添加到div

[英]CSS/JS Add class to div when page is scrolled to a certain point

I'm looking for a way to add a class to a div when the page is scrolled to a certain point.我正在寻找一种在页面滚动到某个点时将 class 添加到 div 的方法。

The important parts of my CSS:我的 CSS 的重要部分:

.top-bar {
    position: fixed;
    top: 0;
    width: 100%;
    height: 6.5vh;
}

.top-bar-bg {
    background: -moz-linear-gradient(0deg,#6272e9,#36104a);  
}

I've tried adding the class ( top-bar-bg ) using我尝试使用添加 class ( top-bar-bg

window.addEventListener("scroll", function(event){
var scroll = this.scrollY;
if(scroll > 860) {
   this.document.getElementById("nav").classList.add("top-bar-bg");
} else {
   this.document.getElementById("nav").classList.remove("top-bar-bg");
}
});

to then notice that when I zoom in or out, scrollY will be different values so the class is added to early/late.然后注意,当我放大或缩小时,scrollY 将是不同的值,因此 class 被添加到早期/晚期。

I've also tried doing this way only to realize that it gives me the same issue.我也试过这样做只是为了意识到它给了我同样的问题。

Am I missing something (obvious)?我错过了什么(很明显)吗? Or is this not the right way to do it?或者这不是正确的方法吗? If you know how to do it, please tell me.如果你知道怎么做,请告诉我。

I've simplified the code and totally revamped it.我已经简化了代码并彻底修改了它。

$(function(){
    var btn = $('.button');
    var btnPosTop = btn.offset().top;
    var win = $(window);
    win.scroll(function(e){
        var scrollTop = win.scrollTop();
        if(scrollTop >= btnPosTop){
            //we've reached the button
            btn.css({position:'fixed',top:0,marginTop:0});
        }else if(btn.css('position') === 'fixed'){
            //if we scroll back up past the button's original position, and the button had previously been changed to its fixed position, we change it back
            btn.css({position:'',top:'',marginTop:'100px'});
        }
    });
});

Anyway, you should put the code for making the text appear inside of the if(scrollTop >= btnPosTop) statement.无论如何,您应该将使文本显示在if(scrollTop >= btnPosTop)语句中的代码。 Just like the button, it should get a position of fixed (either by setting it in JS or by adding a class to the containing div that has its CSS specified as having position:fixed ) so that it moves along as the page scrolls. Just like the button, it should get a position of fixed (either by setting it in JS or by adding a class to the containing div that has its CSS specified as having position:fixed ) so that it moves along as the page scrolls.

Fiddle: JSFiddle Example小提琴: JSFiddle 示例

is this what you were looking for?这是你要找的吗?

 $(document).ready(function() { $(window).scroll(function(){ if ($(this).scrollTop() > 10) { $('.box').addClass('box-hover'); } else { $('.box').removeClass('box-hover'); } }); });
 .box { color: red; }.box:hover { color: orange }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="box">1</div> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <!-- After scroll down add class box-hover--> <div class="box box-hover">1</div>

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

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