使div悬停时更改颜色

时间:2019-12-15 23:04:23

标签: html css animation

您好,请您帮忙。 我正在尝试使round div(带有图标)在用户将其悬停时,边框将更改为平滑的边框颜色。 我已经看过这个https://codepen.io/katmai7/pen/jCAhv,但是我无法使动画平滑,也无法使运动从中心顶部开始,然后是向右,然后是底部中心,然后是顶部。

.wrap_text{
  margin: 0px auto;
  width: 105px;
}

@color: red;

.circle_text {
    position: relative;
    overflow: visible;
    width: 105px;
    height: 105px;
    border-radius: 50%;
    box-shadow: 0px 0px 0 2px #e3e3e3,0px 0px 0 2px #e3e3e3,0px 0px 0 2px #e3e3e3,0px 0px 0 2px #e3e3e3,0 0 0 2px #e3e3e3 !important;  
  
span.text_text {
    position: absolute;
    top: 26px;
    left: 17%;
    width: 60px;
    color: #fff;
    text-align: center;
    text-transform: uppercase;
    font: 18px sans-serif;
    transition: opacity .2s ease;
}
  
  &:hover{
    animation: border .4s ease 1 forwards;
    
    .text_text{
      opacity: 1;
    }
    
  
    
  }
}


@keyframes border{
  0% {
    box-shadow: 60px 60px 0 3px @color, -60px 60px 0 3px @color, -60px -60px 0 3px @color, 60px -60px 0 3px @color, 0 0 0 3px #855f92;
  }
  25% {
    box-shadow: 0 125px 0 3px @color, -60px 60px 0 3px @color, -60px -60px 0 3px @color, 60px -60px 0 3px @color, 0 0 0 3px #855f92;
  }
  50% {
    box-shadow: 0 125px 0 3px @color, -125px 0px 0 3px @color, -60px -60px 0 3px @color, 60px -60px 0 3px @color, 0 0 0 3px #855f92;
  }
  75% {
    box-shadow: 0 125px 0 3px @color, -125px 0px 0 3px @color, 0px -125px 0 3px @color, 60px -60px 0 3px @color, 0 0 0 3px #855f92;
  }
  100% {
    box-shadow: 0 125px 0 3px @color, -125px 0px 0 3px @color, 0px -125px 0 3px @color, 120px -40px 0 3px @color, 0 0 0 3px #855f92;
  } 
}



@keyframes line_text{
  0%{
    right: 60px;
    width: 0;
  }
  100% {
    right: 10px;
    width: 100px;
  }
}
<div class="icon-service">
    <div class="wrap_text">
        <div class="circle_text">
        <span class="text_text"><img src="img.png" alt=""></span>
       </div>
    </div>

有人可以帮助我吗? 预先谢谢你

1 个答案:

答案 0 :(得分:0)

基于此previous answer,您可以使用clip-path和更少的代码来实现这一目标

.box {
  width:150px;
  height:150px;
  margin:20px;
  box-sizing:border-box;
  
  text-align:center;
  font-size:30px;
  line-height:150px;
  position:relative;
  z-index:0;
}
.box:before {
  content:"";
  position:absolute;
  z-index:-1;
  top:0;
  left:0;
  right:0;
  bottom:0;
  border:3px solid #000;
  border-radius:50%;
  transform: scaleX(-1) rotate(45deg);
  clip-path:polygon(50% 50%,0 0,0 0,0 0, 0 0,0 0);
  transition:1s all;
}
.box:hover::before {
  animation:change 1s linear forwards;
  border-color:red;
}

@keyframes change {
  25% {
    clip-path:polygon(50% 50%,0 0,   0 100%,0 100%,0 100%,0 100%);
  }
  50% {
    clip-path:polygon(50% 50%,0 0,0 100%,   100% 100%, 100% 100%,100% 100%);
  }
  75% {
    clip-path:polygon(50% 50%,0 0,0 100%,100% 100%,    100% 0,100% 0);
  }
  100% {
    clip-path:polygon(50% 50%,0 0,0 100%,100% 100%, 100% 0,     0% 0%);
  }
}

body {
 background:pink;
}
<div class="box">
  Hover
</div>