header 滾動至上方fixed

單純筆記用@@
白色滾動到上方時position:fixed

/*
<div class="div1"></div>
<div class="div2" id="Header"></div>
<div class="div3" id="someDiv"></div>
*/
<script>
window.addEventListener('scroll', function(ev) {
var someDiv = document.getElementById('someDiv');
var distanceToTop = someDiv.getBoundingClientRect().top;
if(distanceToTop < 50){
document.getElementById("Header").style.position = "fixed";
document.getElementById("Header").style.top = "0";
console.log("fixed")
}else{
document.getElementById("Header").style.position = "relative";
console.log("relative")
}
console.log(distanceToTop);
});
</script>
view raw scrollStop.js hosted with ❤ by GitHub

頁面 1 / 5: 12345 下一頁
蔡松霖
Related Posts with Thumbnails