滑入并滑出页面滚动按钮

问题描述 投票:0回答:1
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
<style>
.container{
  height: 2000px;
  background:green;
  padding:10px;
}

.btn{
  position:fixed;
  top:50%;
  right:-50%;
  transition:all 600ms ease-out;
}

.slideIn{
  animation: 600ms ease-in 0s  slideIn;
  animation-fill-mode: forwards;
}

.slideOut{
animation: 600ms ease-out 0s  slideOut;
animation-fill-mode: forwards;
}

@keyframes slideIn {
from{
right:-50%;
}   
to{
right:0%;
}
}

 @keyframes slideOut {
from{
  right:0%;
}
to{
right:-50%;
 }
}
</style>
</head>
<body>
  <div id="container" class="container">
    <div style="height: 200px;background:red;"></div>
    <div style="height: 200px;background:yellow;"></div>
    <div id="someEle" style="height: 200px;background:blue;"></div>
      <div style="height: 200px;background:red;"></div>
    <div style="height: 200px;background:yellow;"></div>
  </div>
  <button id="btn" class="btn">GET API ACCESS</button>
<script>


function init(){
    var parent = document.getElementById("container");
    window.addEventListener('scroll',function(e){
    var parentScrollHeight = parent.scrollHeight;
    var parentScrollTop = document.documentElement.scrollTop;
    var startPoint = (parentScrollHeight/100) * 10;
    var endPoint = (parentScrollHeight/100) * 70;
      console.log(parentScrollTop,startPoint,endPoint)
    try{
    var ele = document.getElementById("btn");
          if(parentScrollTop > startPoint && parentScrollTop < endPoint){
            console.log("added")
            ele.classList.add("slideIn");
          }else{
            console.log("removed");
            ele.classList.remove("slideIn");
          }
       }catch(e){
    console.log(e);
  }
});
}
init();
</script>
</body>
</html>

如何在滚动页面时滑入和滑出按钮。滑入工作正常,滑出无效。

javascript css
1个回答
1
投票

您应该为btn添加动画

css

.btn{
  position:fixed;
  top:50%;
  right:-50%;
  transition:all 600ms ease-out;
  animation: 600ms ease-out 0s  slideOut;
animation-fill-mode: forwards;
}
© www.soinside.com 2019 - 2024. All rights reserved.