最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pure js add and remove (toggle) class after scrolling x amount? - Stack Overflow

programmeradmin9浏览0评论

I don't want to use jQuery for this.

It's really simple, I just want to add a class after scrolling past a certain amount of pixels (lets say 10px) and remove it if we ever go back to the top 10 pixels.

My best attempt was:

var scrollpos = window.pageYOffset;
var header = document.getElementById("header");

function add_class_on_scroll() {
    header.classList.add("fade-in");
}

function remove_class_on_scroll() {
    header.classList.remove("fade-in");
}

window.addEventListener('scroll', function(){
    if(scrollpos > 10){
        add_class_on_scroll();
    }
    else {
        remove_class_on_scroll();
    }
    console.log(scrollpos);
});

But console shows a number that continues to grow regardless of scrolling up or down. And the class fade-in never gets added, though console shows we past 10.

I don't want to use jQuery for this.

It's really simple, I just want to add a class after scrolling past a certain amount of pixels (lets say 10px) and remove it if we ever go back to the top 10 pixels.

My best attempt was:

var scrollpos = window.pageYOffset;
var header = document.getElementById("header");

function add_class_on_scroll() {
    header.classList.add("fade-in");
}

function remove_class_on_scroll() {
    header.classList.remove("fade-in");
}

window.addEventListener('scroll', function(){
    if(scrollpos > 10){
        add_class_on_scroll();
    }
    else {
        remove_class_on_scroll();
    }
    console.log(scrollpos);
});

But console shows a number that continues to grow regardless of scrolling up or down. And the class fade-in never gets added, though console shows we past 10.

Share Improve this question asked Sep 30, 2015 at 1:30 John_911John_911 1,1703 gold badges22 silver badges38 bronze badges 1
  • Could you post the code to jsfiddle or jsbin. so that it would be easier to reproduce the issue? – polkovnikov.ph Commented Sep 30, 2015 at 1:35
Add a ment  | 

3 Answers 3

Reset to default 13

You forgot to change the offset value in the scroll handler.

//use window.scrollY
var scrollpos = window.scrollY;
var header = document.getElementById("header");

function add_class_on_scroll() {
    header.classList.add("fade-in");
}

function remove_class_on_scroll() {
    header.classList.remove("fade-in");
}

window.addEventListener('scroll', function(){ 
    //Here you forgot to update the value
    scrollpos = window.scrollY;

    if(scrollpos > 10){
        add_class_on_scroll();
    }
    else {
        remove_class_on_scroll();
    }
    console.log(scrollpos);
});

Now you code works properly

Explanation

There is no documentation for that, like you asked for. This is just an issue in the logic workflow.

When you say that scrollpos = window.scrollY your page is at an top-offset of 0, so your variable stores that value. When the page scrolls, your scroll listener will fires. When yout listener checks for the scrollpos value, the value is still 0, of course. But if, at every scroll handler, you update the scrollpos value, now you can have a dynamic value.

Another option is you to create a getter, like

var scrollpos = function(){return window.scrollY};

This way you can dynamically check what that method will return for you at every offset.

if(scrollpos() > 10)

See? Hope that helped. (:

One simple way to achieve what you want (one line of code inside the scroll event):

window.addEventListener('scroll', function(e) {
    document.getElementById('header').classList[e.pageY > 10 ? 'add' : 'remove']('fade-in');
});
#header {
  height: 600px;
}

.fade-in {
  background-color: orange;
}
<div id='header'></div>

just use the method toggle in classList

header.classList.toggle('fade-in')
发布评论

评论列表(0)

  1. 暂无评论