html - 当鼠标在元素之间移动时,有没有办法让“:active”选择器的样式应用于当前悬停的元素

当您单击其中一个“cButton”元素时,将对其应用活动样式,但如果您按住鼠标按钮,然后将鼠标悬停在另一个 cButton 上,同时仍然按住,则不会应用该样式。我知道一种在 Javascript 中执行此操作的方法,但我正在尝试使用纯 css 来执行此操作。

body{

  display: flex;
  justify-content: space-evenly;

}

.cButton{
  
  width: 100px;
  aspect-ratio: 1;
  background: red;
  
}

.cButton:active{

  background: blue;

}
<div class="cButton">
</div>
<div class="cButton">
</div>
<div class="cButton">
</div>
<div class="cButton">
</div>
<div class="cButton">
</div>
<div class="cButton">
</div>

回答1

恐怕这在纯 css 中是不可能的。因为据我所知,您需要鼠标悬停在 css 中。

回答2

不幸的是,你不能在纯 CSS 中做到这一点。但是,这里有一些有效的 JavaScript 代码:

window.onload = function() {
  document.querySelectorAll(".cButton").forEach(function(ele) {
    ele.onmousedown = function() {
      ele.classList.add("down")
    }
    ele.onmouseover = function(e) {
      if (e.buttons == 1 && e.button == 0) {
        ele.classList.add("down");
      }
    }
    ele.onmouseup = function() {
      ele.classList.remove("down")
    }
    ele.onmouseout = function() {
      ele.classList.remove("down")
    }
  });
}
body {
  display: flex;
  justify-content: space-evenly;
}

.cButton {
  width: 100px;
  aspect-ratio: 1;
  background: red;
}

.cButton.down {
  background: blue;
}
<div class="cButton">
</div>
<div class="cButton">
</div>
<div class="cButton">
</div>
<div class="cButton">
</div>
<div class="cButton">
</div>
<div class="cButton">
</div>

相似文章

php - PHP telnet 解析 mysql table 的数据输出

大家好,我正在开发一个小项目并通过它学习php编码..但我被困在下面的一个新问题上..我正在使用PHPtelnet发送cmds并从设备获取数据回复..所以我发送了一个cmd来检查我正在使用下面的示例代...

javascript - 沙盒中的沙盒模态

快速说明:这是一个我会说的项目(从0到100分)我已经设法在92岁时完成了。所以没有必要处理整个代码。(如果需要,你可以。)我可以称之为:图像滑块、图像库、静态滑块、静态轮播,或者可能是静态图像滑块轮...

随机推荐

最新文章