简体   繁体   English

滚动时背景颜色缓和

[英]Background color ease on scroll

<script>
  window.onscroll = function() {scrollFunction()};

function scrollFunction() {
  if (document.body.scrollTop > 10 || document.documentElement.scrollTop > 10) {
    document.getElementsById("bg-custom").style.backgroundColor = "#ede9e000";
  } else {
    document.getElementById("bg-custom").style.backgroundColor = "#ede9e0";
  }
}
</script>
<nav class="navbar navbar-expand-lg bg-custom sticky-top" id="bg-custom">

Hello guys I have this little part of my code where I want to ease the backgroundcolor on scroll.大家好,我有一小部分代码,我想减轻滚动时的背景色。 The color already changes on scroll, but how can I fix that is eases nice?颜色已经在滚动时发生了变化,但是我该如何解决这个问题呢? If anyone can help me I would appreciate it a lot!如果有人可以帮助我,我将不胜感激!

One method is to add a transition property in the CSS styling for #bg-custom .一种方法是在#bg-custom的 CSS 样式中添加一个transition属性。 This will allow any properties that are changed to ease from the starting state/value into the value newly set through JavaScript.这将允许任何更改的属性从起始状态/值缓和到通过 JavaScript 新设置的值。

 window.onscroll = function() { scrollFunction() }; function scrollFunction() { if (document.body.scrollTop > 10) { document.getElementsById("bg-custom").style.backgroundColor = "red"; } else { document.getElementById("bg-custom").style.backgroundColor = "blue"; } }
 .bg-custom { position: absolute; background-color: red; height: 50vh; width: 100vw; transition: 2s; z-index: 2; } div { position: absolute; height: 200vh; width: 100vw; }
 <nav class="navbar navbar-expand-lg bg-custom sticky-top" id="bg-custom"> <div></div>

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

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