如何使用javascript改变背景图像的透明度

How to change transparency of background image using javascript?

本文关键字:图像 透明度 背景 改变 何使用 javascript      更新时间:2023-09-26

元素有一个重复的不透明背景。我需要能够使用javascript改变背景图像的不透明度。我想根据滚动位置调整图像的不透明度。

我找到了这个链接,使用css "after"伪类:css -tricks

但是看起来我不能使用javascript更改伪类的属性。有人知道我该怎么做吗?

试试这样:
HTML:

<div id="img"></div>
CSS:

#img{background:url(enter your image file url here); width:100%; height:100%;}
JavaScript:

document.getElementById("img").style.opacity=0.5;