[英]Smooth transition between static and fixed div header
<script>
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 320 || document.documentElement.scrollTop >
320) {
document.getElementById("header").style.position = "fixed";
} else {
document.getElementById("header").style.position = "static";
}
}
</script>
我有這個JavaScript,可以使標頭div向下滾動並保持在320px的固定位置,唯一的問題是標頭會大致彈出。 如何在靜態位置到固定平滑之間過渡?
針對您的兩個條件嘗試以下操作:
document.getElementById("header").style.transition = "all 2s";
這里的 W3school提供了一個生活示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.