<!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>
<style>
.dom {
width: 100%;
height: 400px;
border: 1px solid red;
}
</style>
</head>
<body>
<div class="dom">dom1</div>
<div class="dom">dom2</div>
<div class="dom">dom3</div>
<div class="dom">dom4</div>
<div class="dom">dom5</div>
<div class="dom">dom6</div>
<div class="dom">dom7</div>
<div class="dom">dom8</div>
<div class="dom">dom9</div>
<div class="dom">dom10</div>
<script>
let observer = new IntersectionObserver((e) => {
e.forEach((dom) => {
// isIntersecting为true则dom出现在页面上
dom.isIntersecting ? (dom.target.style.width = '100px') : ''
})
}, {})
let doms = document.getElementsByClassName('dom')
for (let dom of doms) {
observer.observe(dom)
}
</script>
</body>
</html>
js监听dom节点是否出现在页面上
最新推荐文章于 2024-04-26 09:23:35 发布