简体   繁体   English

静态和固定div标头之间的平滑过渡

[英]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>

I have this JavaScript that makes the header div scroll down and stay in a fixed position at 320px, the only problem is that the header pops up roughly. 我有这个JavaScript,可以使标头div向下滚动并保持在320px的固定位置,唯一的问题是标头会大致弹出。 How can I make the transition between static position to fixed smoothy? 如何在静态位置到固定平滑之间过渡?

Try this for both of your conditions: 针对您的两个条件尝试以下操作:

document.getElementById("header").style.transition = "all 2s";

A life example is given in W3school here . 这里的 W3school提供了一个生活示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM