2017-10-17 56 views

回答

0

您可以使用IntersectionObserver,检查是否元素.isIntersectingIntersectionObserver回调函数

const element = document.getElementById("box"); 
 

 
const intersectionObserver = new IntersectionObserver((entries) => { 
 
    let [entry] = entries; 
 
    if (entry.isIntersecting) { 
 
    setTimeout(() => alert(`${entry.target.id} is visible`), 100) 
 
    } 
 
}); 
 
// start observing 
 
intersectionObserver.observe(box); 
 

 
element.scrollIntoView({behavior: "smooth"});
body { 
 
    height: calc(100vh * 2); 
 
} 
 

 
#box { 
 
    position: relative; 
 
    top:500px; 
 
}
<div id="box"> 
 
box 
 
</div>

+0

IntersectionObserver Safari中并没有:( –

+0

支持@SushantGupta _ “IntersectionObserver Safari中并没有支持” _ Safari浏览器不支持'.scrollIntoView()',根据链接到MDN的问题。 – guest271314

+0

'scrollIntoView'正在Safari中为我工作,只不过'{behavior:'smooth'}'不支持移植。 http://caniuse.com/#search=scrollintoview –

相关问题