Intersection Observer 观察元素何时进入或退出浏览器的视口
<!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7"><![endif]-->
<!--[if IE 7]><html class="no-js lt-ie9 lt-ie8"><![endif]-->
<!--[if IE 8]><html class="no-js lt-ie9"><![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->
<html lang="zh-cmn-Hans">
<head>
<meta charset="utf-8">
<title>Intersection Observer API</title>
<link rel="shortcut icon" type="image/ico" href="" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!--移动端视图-->
<meta name="renderer" content="webkit" />
<meta name="keywords" content="ajanuw" />
<!--关键词-->
<meta name="description" content="ajanuw, b,c" />
<!--网站内容描述-->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta http-equiv="Pragma" content="no-cache" />
<!--禁止浏览器从本地计算机的缓存中访问页面内容-->
<meta http-equiv="Window-target" content="_top" />
<!--强制页面在当前窗口以独立页面显示-->
<meta http-equiv="content-Type" content="text/html;charset=utf-8" />
<!--显示字符集的设定-->
<meta http-equiv="Content-Language" content="zh-cn" />
<!--显示语言的设定-->
<meta http-equiv="imagetoolbar" content="false" />
<!--指定是否显示图片工具栏,false不显示,true显示-->
<link rel="icon" sizes="192x192" href="https://pic.cnblogs.com/avatar/1053296/20171128213246.png" />
<link rel="apple-touch-icon" href="https://pic.cnblogs.com/avatar/1053296/20171128213246.png" />
<meta name="msapplication-square310x310logo" content="https://pic.cnblogs.com/avatar/1053296/20171128213246.png" />
<meta name="theme-color" content="#ff4081" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<!-- Chrome, Firefox OS and Opera -->
<link rel="apple-touch-startup-image" href="https://pic.cnblogs.com/avatar/1053296/20171128213246.png" />
<style>
body {
height: 1000px;
}
.full-img {
max-width: 100%;
}
.doc {
position: fixed;
right: 0;
top: 0;
color: #fff;
}
</style>
</head>
<body>
<!--[if lt IE 8]> <h3>请升级你的浏览器,或更换主浏览器!!!</h3> <![endif]-->
<img class="full-img" src="http://per.kelantu.com/photos/1515232896-FnIf9c57Hf1-l7DUdBwlXdN1bcLv-orij0?e=3153600000&token=CT86R8zZYVXDyHWEWFoMX4pz0ksOzxtKCaC80si4:HcaTgLGmJ6IbOt_d9ZGOQIqtv6g="
alt="">
<div class="doc"></div>
<script>
let img = document.querySelector('.full-img');
let doc = document.querySelector('.doc');
var options = {
root: null,
rootMargin: '0px',
threshold: [0, 0.5, 1] // 阈值
}
var callback = function (entries, observer) {
entries.forEach(entry => {
// 总面积
var h = entry.boundingClientRect.height;
// 可视 面积
var bandH = entry.intersectionRect.height;
if (bandH == h) {
console.log('100%');
}
if (bandH < h * 0.5 && bandH != 0) {
console.log('小于 50%');
}
if (bandH > h * 0.5 && bandH < h) {
console.log('大于 50%');
}
if (bandH <= 0) {
console.log('0%');
}
if (entry.isIntersecting) {
Object.assign(doc, {
...Object.assign(doc.style, {
background: 'green'
}),
textContent: 'image show '
});
} else {
Object.assign(doc, {
...Object.assign(doc.style, {
background: 'red'
}),
textContent: 'image hidden '
});
}
});
};
if ('IntersectionObserver' in window) {
var observer = new IntersectionObserver(callback, options);
observer.observe(img);
}
</script>
</body>
</html>
See also: