我使用悬停,激活和禁用样式按钮。

但问题是,当按钮被禁用时,悬停和活动样式仍然适用。

如何应用悬停和活动仅在启用按钮?


当前回答

为什么不在css中使用属性“disabled”。这必须适用于所有浏览器。

button[disabled]:hover {
    background: red;
}
button:hover {
    background: lime;
}

其他回答

在sass (scss)中:

 button {
  color: white;
  cursor: pointer;
  border-radius: 4px;

  &:disabled{
    opacity: 0.4;

    &:hover{
      opacity: 0.4;  //this is what you want
    }
  }

  &:hover{
    opacity: 0.9;
  }
}
.button:active:hover:not([disabled]) {
    /*your styles*/
}

你可以试试这个。

我在我的项目中使用了下面的一个。

.bg-brand-3 {
  background-color: black;
  &[type="button"]:enabled {
    &:hover {
      background-color: orange;
    }
    &:active {
      background-color: green;
    }
  }
  &[type="submit"] {
    // css
  }
}

:enable与:not(:disabled)含义相同。

为什么不在css中使用属性“disabled”。这必须适用于所有浏览器。

button[disabled]:hover {
    background: red;
}
button:hover {
    background: lime;
}

使用最轻的触摸:通过规则顺序覆盖。

.btn {
  /* base styles */
}

.btn:hover {
  color: red;
}

.btn:active {
  color: green;
}

.btn:disabled {
  color: #aaa;
}

诀窍在于顺序——首先应用所有非禁用状态,确保它们都具有相同的特异性,最后应用禁用状态,具有相同的特异性。

这对于添加到链接的类或没有disabled属性的非交互式元素不起作用。

(编辑删除了更高特异性的规则,并打乱了指针事件)