禁用按钮时禁用悬停效果

时间:2016-05-05 12:08:16

标签: html css css3

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

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

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

.button-disabled:hover{
    background-color: /** Same as when not hovering **/
}



.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;  
}

<button class="button button-blue">Enabled Button</button>
<button class="button button-blue button-disabled" disabled>Disabled Button</button>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:13)

您可以使用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;
}
<button class="button button-blue">Enabled Button</button>
<button class="button button-blue button-disabled" disabled>Disabled Button</button>

由于这仅适用于新版本的浏览器,因此最好使用相同的颜色,同时添加: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;
}
<button class="button button-blue">Enabled Button</button>
<button class="button button-blue button-disabled" disabled>Disabled Button</button>

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

答案 1 :(得分:3)

不使用选择器以及属性选择器或类选择器。请注意,这在IE8及以下版本中无效。

&#13;
&#13;
.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:not(.button-disabled){
    background-color: #2980b9; 
}

.button-blue:hover:not([disabled]){
    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;  
}
&#13;
<button class="button button-blue">Enabled Button</button>
<button class="button button-blue button-disabled" disabled>Disabled Button</button>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

不使用选择器

&#13;
&#13;
.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:not(.button-disabled):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;  
}
&#13;
<button class="button button-blue">Enabled Button</button>
<button class="button button-blue button-disabled" disabled>Disabled Button</button>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

有一个非常简单的方法。我不知道为什么会有人把它弄复杂。

使用以下内容:

Import-Module Pester Import-Module AzureAdPreview -Force Import-Module Microsoft.Xrm.Data.Powershell ...