有时候我们希望可以做出,当浏览到指定位置,弹出元素,并带有入场动画效果 如下图demo所示,此时我们便可以使用IntersectionObserver
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>TOP</h1>
<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><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div id="mydiv" style="background:red; width:100px;height:100px;margin-left:500px; transition:background 3s 0.5s,margin-left 2s; "> </div>
<script type="text/javascript">
var div=document.getElementById("mydiv");
function callback(a){
if(a[0].isIntersecting){
div.style.background="blue";
div.style.marginLeft="10px";
}
else{
div.style.background="red";
div.style.marginLeft="500px";
}
}
var x= new IntersectionObserver(callback);
x.observe(div);
</script>
</body>
</html>
参考: https://developer.mozilla.org/zh-CN/docs/Web/API/IntersectionObserver https://cloud.tencent.com/developer/article/1499647
|