禁用按钮时禁用悬停效果

dim*_*cas 5 html css css3

我创建了一组具有不同大小,颜色和效果的按钮,因此有绿色按钮,红色按钮等,其中一个是下面的蓝色按钮.正如您所看到的,鼠标悬停时背景颜色会变为更暗的颜色

我想只创建一个CSS类,.button-disabled这将使按钮看起来像一个禁用的按钮.我试图想办法在禁用按钮时删除悬停效果(如下例中的第二个按钮)

请注意,我希望将此类应用于具有不同背景颜色的按钮,因此我不能只添加以下内容:

.button-disabled:hover{
    background-color: /** Same as when not hovering **/
}
Run Code Online (Sandbox Code Playgroud)

.button{
   text-decoration: none;
   border: none;
   padding: 12px 20px;
   cursor: pointer;
   outline: 0;
   display: inline-block;
   margin-right: 2px;  
   color: #ffffff;
   border-radius: 12px;
}

.button-blue{
    background-color: #3498db; 
}

.button-blue:hover{
    background-color: #2980b9; 
}

.button-blue:active{
    color: #3498db;
    background-color: #ffffff; 
    box-shadow: 0px 0px 6px 2px rgba(0,0,0,0.2);
}

.button-disabled{
    opacity: 0.6;  
}
Run Code Online (Sandbox Code Playgroud)
<button class="button button-blue">Enabled Button</button>
<button class="button button-blue button-disabled" disabled>Disabled Button</button>
Run Code Online (Sandbox Code Playgroud)

Pra*_*man 16

您可以使用pointer-events: none它来确保它不做任何事情.这是阻止任何hover效果甚至点击元素上发生的正确方法:

.button {
  text-decoration: none;
  border: none;
  padding: 12px 20px;
  cursor: pointer;
  outline: 0;
  display: inline-block;
  margin-right: 2px;
  color: #ffffff;
  border-radius: 12px;
}
.button-blue {
  background-color: #3498db;
}
.button-blue:hover {
  background-color: #2980b9;
}
.button-blue:active {
  color: #3498db;
  background-color: #ffffff;
  box-shadow: 0px 0px 6px 2px rgba(0, 0, 0, 0.2);
}
.button-disabled {
  opacity: 0.6;
  pointer-events: none;
}
Run Code Online (Sandbox Code Playgroud)
<button class="button button-blue">Enabled Button</button>
<button class="button button-blue button-disabled" disabled>Disabled Button</button>
Run Code Online (Sandbox Code Playgroud)

由于这仅适用于新版本的浏览器,因此最好使用相同的颜色,同时添加:hover状态:

.button {
  text-decoration: none;
  border: none;
  padding: 12px 20px;
  cursor: pointer;
  outline: 0;
  display: inline-block;
  margin-right: 2px;
  color: #ffffff;
  border-radius: 12px;
}
.button-blue {
  background-color: #3498db;
}
.button-blue:hover {
  background-color: #2980b9;
}
.button-blue:active {
  color: #3498db;
  background-color: #ffffff;
  box-shadow: 0px 0px 6px 2px rgba(0, 0, 0, 0.2);
}
.button-blue.button-disabled:hover,
.button-disabled {
  opacity: 0.6;
  background-color: #3498db;
}
Run Code Online (Sandbox Code Playgroud)
<button class="button button-blue">Enabled Button</button>
<button class="button button-blue button-disabled" disabled>Disabled Button</button>
Run Code Online (Sandbox Code Playgroud)

当您定义了多个类时,这将变得很痛苦,并且您必须disabled为每种颜色重新定义类.