CSS3动画边框颜色

53

我想使用CSS3为一个元素的边框添加动画效果,无论是在悬浮状态还是普通状态下。是否有人能够提供代码片段或指导我如何实现?

我可以使用jQuery实现,但是希望能找到一些纯CSS3的解决方案。

4个回答

83

您可以使用CSS3的transition来实现这个效果。请参考下面的示例:

http://jsfiddle.net/ujDkf/1/

以下是主要代码:

#box {
  position : relative;
  width : 100px;
  height : 100px;
  background-color : gray;
  border : 5px solid black;
  -webkit-transition : border 500ms ease-out;
  -moz-transition : border 500ms ease-out;
  -o-transition : border 500ms ease-out;
  transition : border 500ms ease-out;
}

#box:hover {
   border : 10px solid red;   
}

35

你也可以尝试这个...

button {
  background: none;
  border: 0;
  box-sizing: border-box;
  margin: 1em;
  padding: 1em 2em;
  box-shadow: inset 0 0 0 2px #f45e61;
  color: #f45e61;
  font-size: inherit;
  font-weight: 700;
  vertical-align: middle;
  position: relative;
}
button::before, button::after {
  box-sizing: inherit;
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
}

.draw {
  -webkit-transition: color 0.25s;
  transition: color 0.25s;
}
.draw::before, .draw::after {
  border: 2px solid transparent;
  width: 0;
  height: 0;
}
.draw::before {
  top: 0;
  left: 0;
}
.draw::after {
  bottom: 0;
  right: 0;
}
.draw:hover {
  color: #60daaa;
}
.draw:hover::before, .draw:hover::after {
  width: 100%;
  height: 100%;
}
.draw:hover::before {
  border-top-color: #60daaa;
  border-right-color: #60daaa;
  -webkit-transition: width 0.25s ease-out, height 0.25s ease-out 0.25s;
  transition: width 0.25s ease-out, height 0.25s ease-out 0.25s;
}
.draw:hover::after {
  border-bottom-color: #60daaa;
  border-left-color: #60daaa;
  -webkit-transition: border-color 0s ease-out 0.5s, width 0.25s ease-out 0.5s, height 0.25s ease-out 0.75s;
  transition: border-color 0s ease-out 0.5s, width 0.25s ease-out 0.5s, height 0.25s ease-out 0.75s;
}
<section class="buttons">
  <button class="draw">Draw</button>
</section>


这太棒了,正是我一直在寻找的。是否有办法将这种效果应用到整个表格行? - NightOwlPrgmr

13

如果您需要无限运行过渡效果,请尝试以下示例:

#box {
  position: relative;
  width: 100px;
  height: 100px;
  background-color: gray;
  border: 5px solid black;
  display: block;
}

#box:hover {
  border-color: red;
  animation-name: flash_border;
  animation-duration: 2s;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
  -webkit-animation-name: flash_border;
  -webkit-animation-duration: 2s;
  -webkit-animation-timing-function: linear;
  -webkit-animation-iteration-count: infinite;
  -moz-animation-name: flash_border;
  -moz-animation-duration: 2s;
  -moz-animation-timing-function: linear;
  -moz-animation-iteration-count: infinite;
}

@keyframes flash_border {
  0% {
    border-color: red;
  }
  50% {
    border-color: black;
  }
  100% {
    border-color: red;
  }
}

@-webkit-keyframes flash_border {
  0% {
    border-color: red;
  }
  50% {
    border-color: black;
  }
  100% {
    border-color: red;
  }
}

@-moz-keyframes flash_border {
  0% {
    border-color: red;
  }
  50% {
    border-color: black;
  }
  100% {
    border-color: red;
  }
}
<div id="box">roll over me</div>


2
嗨,@Tahir。提醒一下,答案应该是自包含的;虽然可以将外部资源作为参考引用,但答案信息不应仅存在于外部网站上。因此,我将你的JSFiddle代码带入了答案本身,将SCSS转换为CSS,并将动画名称重命名为flash_border(因为flash名称与Stack Overflow的某些内容冲突)。 - Chris Forrence
1
很好,根据您的代码,我制作了一个具有持续时间和无限循环的自动运行程序: http://jsfiddle.net/913xjzL6/24/ - Pyetro

0
有点晚了,但是...
这是我的做法:

.class {
  color: tomato;
  border: 10px solid currentColor;
  transition: color 250ms linear;

  // not part of solution just layout
  width: 100px;
  height: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 2rem;
  text-transform: uppercase;
}

.class:hover {
  color: rebeccapurple;
}
<div class='class' >color way</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接