css的transition实现一个hover边框加载的按钮

1.实现效果

11165.gif

2.实现步骤

定义一个如图所示的矩形按钮

<div>苏苏就是小苏苏呢</div>
div {
    border: 1px solid #EDEDED;
    padding: 0 40px;
    display: block;
    line-height: 40px;
    -webkit-transition: all 0.6s ease-in;
    -moz-transition: all 0.6s ease-in;
    -ms-transition: all 0.6s ease-in;
    -o-transition: all 0.6s ease-in;
    transition: all 0.6s ease-in;
    color: #FFF;
    font-weight: 600;
    position: relative;
}

为其添加两个伪元素

*,
*:before,
*:after {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}
div:after,
div:before {
    box-sizing: border-box;
    border: 1px solid transparent;
    width: 0;
    height: 0;
    content: '';
    display: block;
    position: absolute;
    user-select: none;
}

div:before {
    bottom: 0;
    right: 0;
    -webkit-transition: border-color 0.4s ease-in 0.4s, width 0.2s ease-in 0.2s, height 0.2s ease-in;
    transition: border-color 0.4s ease-in 0.4s, width 0.2s ease-in 0.2s, height 0.2s ease-in;

}

div:after {
    top: 0;
    left: 0;
    -webkit-transition: border-color 0.8s ease-in 0.8s, width 0.2s ease-in 0.6s, height 0.2s ease-in 0.4s;
    transition: border-color 0.8s ease-in 0.8s, width 0.2s ease-in 0.6s, height 0.2s ease-in 0.4s;
}

添加伪类hover:

div:hover {
    border: 1px solid #ffff00;
}
div:hover:after,
div:hover:before {
    width: 100%;
    height: 100%;
}

div:hover:after {
    border-top-color: #ffff00;
    border-right-color: #ffff00;
    -webkit-transition: width 0.2s ease-out, height 0.2s ease-out 0.2s;
    transition: width 0.2s ease-out, height 0.2s ease-out 0.2s;
}


div:hover:before {
    border-bottom-color: #ffff00;
    border-left-color: #ffff00;
    -webkit-transition: border-color 0s ease-out 0.4s, width 0.2s ease-out 0.4s, height 0.2s ease-out 0.6s;
    transition: border-color 0s ease-out 0.4s, width 0.2s ease-out 0.4s, height 0.2s ease-out 0.6s;
}

3.transaction

transition 属性是一个简写属性,用于设置四个过渡属性:

transition-property:规定设置过渡效果的 CSS 属性的名称。
transition-duration:规定完成过渡效果需要多少秒或毫秒。
transition-timing-function:规定速度效果的速度曲线。
transition-delay:定义过渡效果何时开始。
tips:请始终设置 transition-duration 属性,否则时长为 0,就不会产生过渡效果。
transition-timing-function:

在这里插入图片描述

4.实现思路

伪元素默认的宽高为0,当鼠标悬浮时候,设置一定的过渡时间使其宽高为100%。
上述demo中:
在0.2s内 border-top-color变化,border-right-color推迟0.2s之后完成一个0.2s的过渡。
同理border-bottom-color推迟0.4s,border-left-color推迟0.6s.

5.完整demo

demo

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 上一篇介绍了CSS3里transform变形属性,本篇介绍另一个人气动画属性transition。transiti...
    张歆琳阅读 25,360评论 1赞 31
  • 过渡 transition transition属性用于设置元素的过渡动画交互效果 transition是一个简写...
    JunChow520阅读 276评论 0赞 0
  • 转自:https://www.jianshu.com/p/56f8ddafc63f上一篇介绍了CSS3里trans...
    BULL_DEBUG阅读 909评论 0赞 0
  • 据传说有研究表明凝视美女能增加寿命、放松心情、缓解压力等等好处。为啥我见了美女只会血压升高、情绪激动?难道你们所说...
    thisDong阅读 970评论 1赞 1
  • transitiontransition-property 过渡属性transition-duration 过渡持...
    Rella7阅读 498评论 0赞 0

友情链接更多精彩内容