简体   繁体   English

半透明标题在滚动时变得不透明

[英]Semi-transparent header becomes opaque on scroll

I am attempting to display 5the header of my website at 50% opacity (the header and navigation will overlap a slideshow that's behind it). 我试图以5%的不透明度显示5我网站的标题(标题和导航将与它后面的幻灯片重叠)。 When the user scrolls down the page, I want the header area to remain locked to the top of the webpage and the opacity be removed. 当用户向下滚动页面时,我希望页眉区域保持锁定在网页顶部,并消除不透明度。 I found an example that I was able to modify. 我找到了一个可以修改的示例。 It works in all browsers except IE. 它适用于除IE之外的所有浏览器。 Does anyone know of a work-around for IE? 有人知道IE的解决方法吗?

window.addEventListener('scroll', function () {
 document.body.classList[
  window.scrollY > 20 ? 'add': 'remove'
 ]('scrolled');
});

Here is the working example: https://jsfiddle.net/SEH5M/524/ 这是工作示例: https : //jsfiddle.net/SEH5M/524/

Cheers! 干杯!

In IE you need to use: 在IE中,您需要使用:

 **window.document.documentElement.scrollTop** instead of **window.scrollY**.

find more here: IE8 alternative to window.scrollY? 在此处找到更多信息: IE8替代window.scrollY?

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

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