温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在CSS3中利用transition属性实现下划线

发布时间:2021-04-07 17:42:10 来源:亿速云 阅读:153 作者:Leah 栏目:web开发

怎么在CSS3中利用transition属性实现下划线?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

transition属性

  1. transition: 简写属性,用于在一个属性中设置四个过渡属性。

  2. transition-property: 规定应用过渡的 CSS 属性的名称。

  3. transition-duration: 定义过渡效果花费的时间。默认是 0。

  4. transition-timing-function: 规定过渡效果的时间曲线。默认是 "ease"。

    1. linear: 规定以相同速度开始至结束的过渡效果(等于 cubic-bezier(0,0,1,1))

    2. ease: 规定慢速开始,然后变快,然后慢速结束的过渡效果(cubic-bezier(0.25,0.1,0.25,1))

    3. ease-in: 规定以慢速开始的过渡效果(等于 cubic-bezier(0.42,0,1,1))

    4. ease-out: 规定以慢速结束的过渡效果(等于 cubic-bezier(0,0,0.58,1))

    5. ease-in-out: 规定以慢速开始和结束的过渡效果(等于 cubic-bezier(0.42,0,0.58,1))

    6. cubic-bezier(n,n,n,n): 在 cubic-bezier 函数中定义自己的值。可能的值是 0 至 1 之间的数值。

  5. transition-delay: 规定过渡效果何时开始。默认是 0。

transition: width 1s linear 2s;        /*简写实例*/

/*等同如下*/
transition-property: width;
transition-duration: 1s;
transition-timing-function: linear;
transition-delay: 2s;

tranform属性

  • translate() 根据左(X轴)和顶部(Y轴)位置给定的参数,从当前元素位置移动。

  • rotate() 在一个给定度数顺时针旋转的元素。负值是允许的,这样是元素逆时针旋转。

  • scale() 该元素增加或减少的大小,取决于宽度(X轴)和高度(Y轴)的参数:

  • skew() 包含两个参数值,分别表示X轴和Y轴倾斜的角度,如果第二个参数为空,则默认为0,参数为负表示向相反方向倾斜。

  • matrix() matrix 方法有六个参数,包含旋转,缩放,移动(平移)和倾斜功能。

实现我们需要的效果

当然,在这就直接放出代码,代码中有注释方便理解

/*css代码*/

h3{
    position: relative;
    padding: 15px;
    text-align: center;    
}
button{
    width: 100px;
    height: 40px;
    border-radius: 15px;
    border: none;
    background: #188FF7;
    color: #fff;
    outline: none;
    cursor: pointer;
    font-weight: bold;
}
button:hover{
    background: #188EA7;
}
.container{
    width: 600px;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: 0 auto;
    
}
.line{
    position: absolute;
    left: 0;
    bottom: 0;
    height: 3px;
    width: 100%;
    transition: transform .5s;
    background: #188EA7;
    color: #188EA7;
    transform: scaleX(0);
    z-index: 1111;            
}

@keyframes changeColor1{
    from{
        color: #000;
    }
    to{
        color: #188EA7;
    }
}
@keyframes changeColor2{
    from{                
        color: #188EA7;
    }
    to{
        color: #000;
    }
}
<!--html代码-->

<div class="container">
    <h3 id="title">
        百度前端学院
        <i class="line" id="line"></i>
    </h3>
    <button id="change">Change</button>
</div>
//js部分代码

(function () {
    let btn = document.getElementById('change');
    let h3 = document.getElementById('title');
    let line = document.getElementById('line');
    let count = 0;
    btn.onclick = function () {
        if(count%2===0){
            line.style.transform = "scaleX(1)";
            h3.style.animation = "changeColor1 1s";
            h3.style.color = "#188EA7";
            count++;
        }else{
            line.style.transform = "scaleX(0)";
            h3.style.animation = "changeColor2 1s";
            h3.style.color = "#000";
            count++;
        }
        
    }
})();

看完上述内容,你们掌握怎么在CSS3中利用transition属性实现下划线的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI