Home > Mobile >  How to show up button when scroll?
How to show up button when scroll?

Time:01-03

I have an up button.

up

I want the up button not to be seen at first, but to be seen later when a scroll is done.

And I want to replace d-none with d-block.

HTML

<a href="#"  id="scrollTop"><i ></i></a>

JS

if (document.getElementById('scrollTop').scrollTop === 200) {
    document.getElementById('scrollTop').classList.add('d-block');
    document.getElementById('scrollTop').classList.remove('d-none');    
}

CodePudding user response:

Listen on scroll event. working example to show how it works:

const myID = document.getElementById("myID");

var myScrollFunc = function () {
    var y = window.scrollY;
    if (y >= 800) {
        myID.className = "bottomMenu show"
    } else {
        myID.className = "bottomMenu hide"
    }
};

window.addEventListener("scroll", myScrollFunc);
body {
    height: 2000px;
}
.bottomMenu {
    position: fixed;
    bottom: 10px;
    right: 10px;
    color: white;
    width: 50px;
    height: 60px;
    border-top: 1px solid #000;
    background: red;
    z-index: 1;
    transition: all .5s;
}
.hide {
    opacity:0;
   
}
.show {
    opacity:1;
  
}
<div id="myID" >top</div>

CodePudding user response:

You should place your code in something like this :

document.addEventListener('scroll', () => {... your code ...})
  • Related