【问题标题】:How to animate border on input and make it expand from center?如何为输入设置动画边框并使其从中心扩展?
【发布时间】:2018-07-28 04:29:57
【问题描述】:

我试图在我的输入按钮上添加线性效果。当用户将鼠标悬停在按钮上时,我希望底部边框线从中心开始,然后同时向右和向左移动。因此,当用户将鼠标悬停在它上面时,这条线会从中心扩展到角落。

我设法添加了一条悬停线,但它没有按我的意愿工作。我环顾四周,只能找到有关标题或标签的问题,而不是按钮。

#btn {
  margin: 10px;
  padding: 20px;
  width: 470px;
  height: 50px;
  border: 2px solid #f7f7f7;
  text-align: center;
  text-transform: uppercase;
  position: relative;
}

#btn:hover {
  cursor: pointer;
  border-bottom: 2px solid red;
  transition: 0.3s;
}
<input id="btn" type="submit" name="" value="Click">

【问题讨论】:

  • 与底部边框一样吗?您可能需要添加一个单独的 div,然后使用 jQuery .hover() 增加其宽度并更改其位置
  • @LinusGeffarth 没错,就是底部边框!将其添加到我的问题中!
  • @LinusGeffarth 你知道我如何在一个按钮上做到这一点吗?

标签: html css


【解决方案1】:

不能使用伪元素,因为它是一个输入标签,因此您可以尝试使用 linear-gradient 作为背景的解决方案:

#btn {
  margin: 10px;
  padding: 20px;
  width: 470px;
  height: 50px;
  border: 2px solid #f7f7f7;
  text-align: center;
  text-transform: uppercase;
  cursor: pointer;
  background: 
    linear-gradient(red, red) bottom / 0% 2px no-repeat
    #ccc;
  transition: 1s;
}

#btn:hover {
  background-size: 100% 2px;
}
<input id="btn" type="submit" name="" value="Click">

【讨论】:

  • 谢谢它的工作!但是是什么让它从中心转向两侧呢?
  • @Alison 在开始时背景尺寸为 0% 2px [width height] 并放置在 50% 100% [xy] : center bottom ... 然后我只是增加尺寸,尤其是宽度...因此尺寸会增加,同时将渐变保持在中心
猜你喜欢
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
相关资源
最近更新 更多