【问题标题】:When i mouseover the same element i want it to get darker - Vanilla JS当我将鼠标悬停在同一个元素上时,我希望它变暗 - Vanilla JS
【发布时间】:2018-07-17 02:48:28
【问题描述】:

我希望我将鼠标悬停在 div 上,每次鼠标悬停时它都会变暗(如 etch-a-sketch),但我遇到了一个问题,即不让不透明度能够变得更暗一次.

for (var i = 0; i < (16 * 16); i++) {
  var iDiv = document.createElement('div');
  iDiv.textContent = "  ";
  iDiv.id = 'block';
  iDiv.className = 'block';
  var container = document.getElementById("container");

  container.appendChild(iDiv);

  iDiv.addEventListener("mouseover", function(event) {
    // highlight the mouseover target
    this.style.backgroundColor = "black";
    this.style.opacity += 0.2;

    // reset the color after a short delay
    setTimeout(function() {
      this.target.style.color = " ";
    }, 500);
  }, false);
}
.container {
  display: grid;
  grid-template-columns: repeat(16, 1fr);
  grid-auto-rows: repeat(16, 1fr);
  width: 95%;
  height: 95%;
  border: 1px solid black;
  margin: auto;
}

#block {
  background: white;
  padding: 12.5px;
}

#block:hover {
  background: ;
}

.color {
  background: rgba {
    0,
    0,
    0,
    0.1
  }
}
&lt;div class="container" id="container"&gt;&lt;/div&gt;

【问题讨论】:

    标签: javascript mouseover opacity


    【解决方案1】:

    问题是,opacity 变成了 string,所以当你添加 0.2 而不是得到 0.4 时,你得到的是 0.20.2

    你需要先parseFloat就可以了。

    for (let i = 0; i < (16*16); i++) {
      const iDiv = document.createElement('div');
      const container = document.getElementById("container");
      iDiv.textContent = "  ";
      iDiv.id = 'block';
      iDiv.className = 'block';
    
      container.appendChild(iDiv); 
    
      iDiv.addEventListener("mouseover", function(event) {   
        // highlight the mouseover target
        this.style.backgroundColor = "black";
        this.style.opacity = (parseFloat(this.style.opacity) || 0) + 0.2;
    
        // reset the color after a short delay
        setTimeout(() => {
          this.style.backgroundColor = "none";
          this.style.opacity = 0;
        }, 5000);
      }, false);
    }
    .container {
      display: grid;
      grid-template-columns: repeat(16, 1fr);
      grid-auto-rows: repeat(16, 1fr);
      width: 95%;
      height: 95%;
      border: 1px solid black;
      margin: auto;
    }
    
    #block{
      background: white;
      padding: 12.5px;
    }
    
    #block:hover{
      background:;
    }
    
    .color{
      background: rgba{0,0,0,0.1}
    }
    &lt;div id="container" class="container"&gt;&lt;/div&gt;

    附带说明,对于您的超时,它不起作用,因为在调用 setTimeout() 时,this 的上下文将丢失。您需要bind() 函数或使用箭头函数(就像我一样)。我将超时设置为 5000 而不是 500,这样您就可以更轻松地看到主要焦点。

    【讨论】:

    • 非常感谢您抽出宝贵时间帮助我。
    • @mahdi0123 当然可以。如果事实证明它是您的正确答案,请务必投票并将其标记为答案。欢迎使用 StackOverflow。
    【解决方案2】:

    您有一些问题。首先,如果您想在setTimeout 中引用this,则需要一个变量。其次,不透明度值保存为字符串,所以你必须解析它。

    for (var i=0; i<(16*16); i++){
      var iDiv = document.createElement('div');
      iDiv.textContent = "  ";
      iDiv.id = 'block';
      iDiv.className = 'block';
      var container = document.getElementById("container");
    
      container.appendChild(iDiv); 
    
      iDiv.addEventListener("mouseover", function( event ) {   
        // highlight the mouseover target
        var that = event.target;
        that.style.backgroundColor = "black";    
        if(parseFloat(that.style.opacity)) {
          that.style.opacity = parseFloat(that.style.opacity) + 0.2;
        } else {
          that.style.opacity = 0.2;
        }
    
        // reset the color after a short delay
        setTimeout(function() {
          that.style.color = " ";
        }, 500);
      }, false);
    
     }
        .container{
      display: grid;
      grid-template-columns: repeat(16, 1fr);
      grid-auto-rows: repeat(16, 1fr);
      width: 95%;
      height: 95%;
      border: 1px solid black;
      margin: auto;
    }
    
    #block{
      background: white;
      padding: 12.5px;
    }
    
    #block:hover{
      background:;
    }
    
    .color{
      background: rgba{0,0,0,0.1}
    }
    <div class="container" id="container">
    </div>
    
    
    
         

    【讨论】:

    • 感谢您的宝贵时间。感谢您回答我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 2023-03-09
    • 1970-01-01
    • 2021-11-07
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多