美文网首页神奇的css
css的transition实现一个hover边框加载的按钮

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

作者: 苏苏哇哈哈 | 来源:发表于2021-10-10 02:05 被阅读0次

    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

    相关文章

      网友评论

        本文标题:css的transition实现一个hover边框加载的按钮

        本文链接:https://www.haomeiwen.com/subject/vancoltx.html