<!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>


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

最佳答案

您应该为btn添加动画

的CSS

.btn{
  position:fixed;
  top:50%;
  right:-50%;
  transition:all 600ms ease-out;
  animation: 600ms ease-out 0s  slideOut;
animation-fill-mode: forwards;
}

07-25 23:43