图片库变暗按钮

时间:2017-10-19 15:14:43

标签: javascript html5 css3

在我的图片库的右上角,有一个按钮,单击该按钮会创建一个使图像变暗的叠加层。我希望能够在点击它时打开和关闭它。我怎样才能做到这一点?

这是我的代码

https://codepen.io/Modestas/pen/qPypXZ?editors=1010

btn.addEventListener("click", ()=> {
 var btnClass = btn.getAttribute("class");
  if(btnClass === "dark") {
    overlay.backgroundColor = "rgba(0,0,0,0.5)";
    btn.textContent = "Lighten";
    btn.setAttribute("class", "light"); 
  } else if (btn.className != "dark") {
    overlay.backgroundColor = "rgba(0,0,0,0)";
    btn.textContent = "Darken";
    btn.setAttribute("class", "dark"); 
  }
})

1 个答案:

答案 0 :(得分:2)

而不是overlay.backgroundColor =overlay.style.backgroundColor =