var observer = new IntersectionObserver(callback[, options]);
css
callback 當目標元素的透明度穿過設定的threshold值時,函數會被調用。callback接受兩個 參數。vue
options 若options沒設置。observer使用document的viewport做爲root,沒有margin,0%的threshold(意味着即便有1 px的變化也會觸發回調)git
下面的例子在threshold值變化在10%以上時觸發myObserverCallback。github
let observer = new IntersectionObserver(myObserverCallback, { "threshold": 0.1 });
<template> <div> <img v-for="(image, i) in images" :key="i" src :data-img-url="image" /> </div> </template> <script> export default { data() { return { images: [ 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574247890587&di=88d4066be3d57ac962a6bec37e265d37&imgtype=0&src=http%3A%2F%2F01.imgmini.eastday.com%2Fmobile%2F20170810%2F20170810151144_d41d8cd98f00b204e9800998ecf8427e_3.jpeg', 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4054762707,1853885380&fm=26&gp=0.jpg', 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574247912077&di=508a949e5e291875debf6ca844292cd4&imgtype=0&src=http%3A%2F%2F03imgmini.eastday.com%2Fmobile%2F20180827%2F20180827095359_6759372e9bd28026ee6f53b500fb4291_2.jpeg', ], }; }, mounted() { const images = document.querySelectorAll('img'); const observerLazyLoad = new IntersectionObserver((entries) => { entries.forEach((item) => { if (item.isIntersecting) { item.target.src = item.target.dataset.imgUrl; } }); }); images.forEach((image) => { observerLazyLoad.observe(image); }); }, }; </script> <style lang="scss" scoped> img { display: block; height: 500px; margin: 30px; } </style>
<template> <div> <p class="fixed-top-helper"></p> <p class="fixed-top-reference"></p> <header>頭部</header> <main> <img v-for="(image, i) in images" :key="i" :src="image" /> </main> </div> </template> <script> export default { data() { return { images: [ 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574247890587&di=88d4066be3d57ac962a6bec37e265d37&imgtype=0&src=http%3A%2F%2F01.imgmini.eastday.com%2Fmobile%2F20170810%2F20170810151144_d41d8cd98f00b204e9800998ecf8427e_3.jpeg', 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4054762707,1853885380&fm=26&gp=0.jpg', 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574247912077&di=508a949e5e291875debf6ca844292cd4&imgtype=0&src=http%3A%2F%2F03imgmini.eastday.com%2Fmobile%2F20180827%2F20180827095359_6759372e9bd28026ee6f53b500fb4291_2.jpeg', ], }; }, mounted() { const header = document.querySelector('header'); const fixedTopReference = document.querySelector('.fixed-top-reference'); fixedTopReference.style.top = `${header.offsetTop}px`; const observerFixedTop = new IntersectionObserver((entries) => { entries.forEach((item) => { if (item.boundingClientRect.top < 0) { header.classList.add('fixed'); } else { header.classList.remove('fixed'); } }); }); observerFixedTop.observe(fixedTopReference); }, }; </script> <style lang="scss" scoped> .fixed-top-helper { height: 1px; background: #ccc; } header { background: #ccc; &.fixed { position: fixed; top: 0; left: 0; width: 100%; } } main { img { display: block; height: 500px; margin: 30px; } } footer { background: #ccc; } </style>
注意事項:數組
<template> <div> <main> <img v-for="(image, i) in images" :key="i" src="image" /> </main> <footer>底部</footer> </div> </template> <script> export default { data() { return { images: [ 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574247890587&di=88d4066be3d57ac962a6bec37e265d37&imgtype=0&src=http%3A%2F%2F01.imgmini.eastday.com%2Fmobile%2F20170810%2F20170810151144_d41d8cd98f00b204e9800998ecf8427e_3.jpeg', 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4054762707,1853885380&fm=26&gp=0.jpg', 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574247912077&di=508a949e5e291875debf6ca844292cd4&imgtype=0&src=http%3A%2F%2F03imgmini.eastday.com%2Fmobile%2F20180827%2F20180827095359_6759372e9bd28026ee6f53b500fb4291_2.jpeg', ], }; }, mounted() { const footer = document.querySelector('footer'); const observerTouchBottom = new IntersectionObserver((entries) => { entries.forEach((item) => { if (item.isIntersecting) { setTimeout(() => { console.log('滾動到了底部,能夠發request請求數據了'); }, 2000); } }); }); observerTouchBottom.observe(footer); }, }; </script> <style lang="scss" scoped> main { img { display: block; height: 500px; margin: 30px; } } footer { background: #ccc; } </style>
<template> <div> <p class="fixed-top-helper"></p> <p class="fixed-top-reference"></p> <header>頭部</header> <main> <img v-for="(image, i) in images" :key="i" src :data-img-url="image" /> </main> <footer>底部</footer> </div> </template> <script> export default { data() { return { images: [ 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574247890587&di=88d4066be3d57ac962a6bec37e265d37&imgtype=0&src=http%3A%2F%2F01.imgmini.eastday.com%2Fmobile%2F20170810%2F20170810151144_d41d8cd98f00b204e9800998ecf8427e_3.jpeg', 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4054762707,1853885380&fm=26&gp=0.jpg', 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574247912077&di=508a949e5e291875debf6ca844292cd4&imgtype=0&src=http%3A%2F%2F03imgmini.eastday.com%2Fmobile%2F20180827%2F20180827095359_6759372e9bd28026ee6f53b500fb4291_2.jpeg', ], }; }, mounted() { const images = document.querySelectorAll('img'); const observerLazyLoad = new IntersectionObserver((entries) => { entries.forEach((item) => { if (item.isIntersecting) { item.target.src = item.target.dataset.imgUrl; } }); }); images.forEach((image) => { observerLazyLoad.observe(image); }); const footer = document.querySelector('footer'); const observerTouchBottom = new IntersectionObserver((entries) => { entries.forEach((item) => { if (item.isIntersecting) { setTimeout(() => { console.log('滾動到了底部,能夠發request請求數據了'); }, 2000); } }); }); observerTouchBottom.observe(footer); const header = document.querySelector('header'); const fixedTopReference = document.querySelector('.fixed-top-reference'); fixedTopReference.style.top = `${header.offsetTop}px`; const observerFixedTop = new IntersectionObserver((entries) => { entries.forEach((item) => { if (item.boundingClientRect.top < 0) { header.classList.add('fixed'); } else { header.classList.remove('fixed'); } }); }); observerFixedTop.observe(fixedTopReference); }, }; </script> <style lang="scss" scoped> .fixed-top-helper { height: 1px; background: #ccc; } header { background: #ccc; &.fixed { position: fixed; top: 0; left: 0; width: 100%; } } main { img { display: block; height: 500px; margin: 30px; } } footer { background: #ccc; } </style>
官方提供了Basic Process,Progress,Sticky Side,Sticky Overlay幾種示例。異步
在項目中能夠當作適當引用。ide
項目地址:https://github.com/russellgol...
demo地址:https://russellgoldenberg.git...函數
https://developer.mozilla.org...
https://developer.mozilla.org...
https://developer.mozilla.org...
https://juejin.im/post/5ca15c...
https://medium.com/walmartlab...
https://juejin.im/post/5d6651...
https://github.com/russellgol...工具
原文地址:IntersectionObserver是什麼?post