【问题标题】:submit button stays hover color after clicked单击后提交按钮保持悬停颜色
【发布时间】:2015-05-15 00:21:53
【问题描述】:

单击“提交”或“重置”按钮后,按钮颜色保持悬停颜色,并且在您单击页面中的其他位置之前不会恢复为原始的“预单击”按钮颜色。

我本质上希望按钮颜色在单击后变回原始颜色。谁能建议如何做到这一点?

CSS/HTML:

.form input:focus {
  background: #FFFFAD;
  outline: none;
}
.buttons {
  text-align: left;
}
.buttons input {
  font-size: 2.5em;
  font-weight: bold;
  font-family: "Arial", serif;
  padding: 8px 40px;
  background: #4470B6;
  border: 0px;
  margin-left: 0px;
  margin-right: 50px;
  margin-top: 50px;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 50px;
  -webkit-box-shadow: 0 0 4px rgba(0, 0, 0, .75);
  -moz-box-shadow: 0 0 4px rgba(0, 0, 0, .75);
  box-shadow: 0 0 4px rgba(0, 0, 0, .75);
  color: #FFFAFA;
}
.buttons input:hover,
.buttons input:focus {
  background-color: #50627E;
  outline: none;
  -webkit-box-shadow: 0 0 1px rgba(0, 0, 0, .75);
  -moz-box-shadow: 0 0 1px rgba(0, 0, 0, .75);
  box-shadow: 0 0 1px rgba(0, 0, 0, .75);
}
<!--  Details Form -->

<section class="details">
  <form id="form" action="test.php" method="post" autocomplete="on" target="_blank">

    <div class="form">
      <label>First Name:</label>
      <input type="text" name="firstname" placeholder="First Name" autofocus />
    </div>

    <div class="buttons">
      <input type="submit" value="Search">
      <input type="reset" value="Reset">
    </div>
  </form>
</section>

【问题讨论】:

  • 您的.buttons input:focus 选择器不是导致问题的原因吗?
  • 事实证明,确实如此。请参阅下文了解如何...

标签: html css


【解决方案1】:

我假设您将按钮设置为焦点以消除轮廓,因此只需将选择器拆分为 2 并在焦点上仅删除轮廓:

.form input:focus {
  background: #FFFFAD;
  outline: none;
}
.buttons {
  text-align: left;
}
.buttons input {
  font-size: 2.5em;
  font-weight: bold;
  font-family: "Arial", serif;
  padding: 8px 40px;
  background: #4470B6;
  border: 0px;
  margin-left: 0px;
  margin-right: 50px;
  margin-top: 50px;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 50px;
  -webkit-box-shadow: 0 0 4px rgba(0, 0, 0, .75);
  -moz-box-shadow: 0 0 4px rgba(0, 0, 0, .75);
  box-shadow: 0 0 4px rgba(0, 0, 0, .75);
  color: #FFFAFA;
}
.buttons input:hover
{
  background-color: #50627E;
  outline: none;
  -webkit-box-shadow: 0 0 1px rgba(0, 0, 0, .75);
  -moz-box-shadow: 0 0 1px rgba(0, 0, 0, .75);
  box-shadow: 0 0 1px rgba(0, 0, 0, .75);
}
.buttons input:focus {
  outline: none;
}
<!--  Details Form -->

<section class="details">
  <form id="form" action="test.php" method="post" autocomplete="on" target="_blank">

    <div class="form">
      <label>First Name:</label>
      <input type="text" name="firstname" placeholder="First Name" autofocus />
    </div>

    <div class="buttons">
      <input type="submit" value="Search">
      <input type="reset" value="Reset">
    </div>
  </form>
</section>

【讨论】:

  • 谢谢香蕉。这是完全正确的。我不想要与 ':focus' 相关联的大纲。
  • 对不起,我的意思是:focus
  • 我实际上正在寻找最初提到的解决方案,这与单击按钮后留在按钮上的:hover 样式有关(也可能是:active)。
【解决方案2】:

那是因为 :hover:focus 背景颜色相同 所以我只为:hover 添加了背景颜色,而没有为:focus 添加背景颜色

如果你希望你可以删除这个类表单 css .buttons input:focus

.form input:focus {
  background: #FFFFAD;
  outline: none;
}
.buttons {
  text-align: left;
}
.buttons input {
  font-size: 2.5em;
  font-weight: bold;
  font-family: "Arial", serif;
  padding: 8px 40px;
  background: #4470B6;
  border: 0px;
  margin-left: 0px;
  margin-right: 50px;
  margin-top: 50px;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 50px;
  -webkit-box-shadow: 0 0 4px rgba(0, 0, 0, .75);
  -moz-box-shadow: 0 0 4px rgba(0, 0, 0, .75);
  box-shadow: 0 0 4px rgba(0, 0, 0, .75);
  color: #FFFAFA;
}
.buttons input:hover,
.buttons input:focus {
  
  outline: none;
  -webkit-box-shadow: 0 0 1px rgba(0, 0, 0, .75);
  -moz-box-shadow: 0 0 1px rgba(0, 0, 0, .75);
  box-shadow: 0 0 1px rgba(0, 0, 0, .75);
}
.buttons input:hover{
background-color: #50627E;}
<!--  Details Form -->

<section class="details">
  <form id="form" action="test.php" method="post" autocomplete="on" target="_blank">

    <div class="form">
      <label>First Name:</label>
      <input type="text" name="firstname" placeholder="First Name" autofocus />
    </div>

    <div class="buttons">
      <input type="submit" value="Search">
      <input type="reset" value="Reset">
    </div>
  </form>
</section>

【讨论】:

  • 感谢 Sanoj 的回复。这也很好用。
【解决方案3】:

您非常接近解决方案。为了达到您想要的效果,请尝试使用focusactive 而不是hoverfocus

active 状态仅在元素被点击时触发。

这就是我所做的

.buttons input:focus {
  outline: none
}
.buttons input:active {
  border: 0;
  background-color: #50627E;
  outline: none;
  -webkit-box-shadow: 0 0 1px rgba(0, 0, 0, .75);
  -moz-box-shadow: 0 0 1px rgba(0, 0, 0, .75);
  box-shadow: 0 0 1px rgba(0, 0, 0, .75);
}

您可以在下面查看完整的解决方案:

.form input:focus {
  background: #FFFFAD;
  outline: none;
}
.buttons {
  text-align: left;
}
.buttons input {
  font-size: 2.5em;
  font-weight: bold;
  font-family: "Arial", serif;
  padding: 8px 40px;
  background: #4470B6;
  border: 0px;
  margin-left: 0px;
  margin-right: 50px;
  margin-top: 50px;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 50px;
  -webkit-box-shadow: 0 0 4px rgba(0, 0, 0, .75);
  -moz-box-shadow: 0 0 4px rgba(0, 0, 0, .75);
  box-shadow: 0 0 4px rgba(0, 0, 0, .75);
  color: #FFFAFA;
}

.buttons input:focus {
  outline: none
}
.buttons input:active {
  border: 0;
  background-color: #50627E;
  outline: none;
  -webkit-box-shadow: 0 0 1px rgba(0, 0, 0, .75);
  -moz-box-shadow: 0 0 1px rgba(0, 0, 0, .75);
  box-shadow: 0 0 1px rgba(0, 0, 0, .75);
}
<!--  Details Form -->

<section class="details">
  <form id="form" action="test.php" method="post" autocomplete="on" target="_blank">

    <div class="form">
      <label>First Name:</label>
      <input type="text" name="firstname" placeholder="First Name" autofocus />
    </div>

    <div class="buttons">
      <input type="submit" value="Search">
      <input type="reset" value="Reset">
    </div>
  </form>
</section>

【讨论】:

  • 谢谢加里。我没有意识到有一个:active 选项。我刚开始学习五天。感谢回复。
  • 大纲存在一些可访问性问题:无;见outlinenone.com
【解决方案4】:

因为我在移动设备上遇到了同样的问题。手机有一个叫做粘性悬停的东西。结束了@ media(hover: hover) 样式。 参考文字:

例如,您可以在媒体查询 (@media hover:hover{}) 中定义您的正常 :hover 样式,以将它们限制为完全支持 :hover 的设备(配备鼠标或某些指针设备的设备):

 @media (hover:hover) {
    nav a:hover{
        background: yellow;
    }
}

或者对于不影响原始 :hover 样式的更先进的方法,目标设备不支持 :hover 完全:

@media (hover:none), (hover:on-demand) {
    nav a:hover{ /* suppress hover effect on devices that don't support hover fully
        background: none;
    }
}

参考:http://javascriptkit.com/dhtmltutors/sticky-hover-issue-solutions.shtml

还有这个:How to prevent sticky hover effects for buttons on touch devices

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
  • @slideshowp2 好的,已修复。但仍然是原始答案的更好补充,因为今天的用户在移动设备上,并且粘性悬停在那里是一个真正的问题。
猜你喜欢
  • 2013-10-24
  • 2014-03-08
  • 2012-03-13
  • 2021-12-21
  • 1970-01-01
  • 2011-11-30
  • 1970-01-01
  • 2021-06-12
  • 1970-01-01
相关资源
最近更新 更多