CSS3之transition实现下划线的示例代码
前端  /  管理员 发布于 7年前   206
本文章介绍了CSS3之transition实现下划线的示例代码,分享给大家,具体如下:
在这里先看看我们的demo
认识transition
这是CSS3中新增的一个样式,可以实现动画的过度。通常使用在添加某种效果可以从一种样式转变到另一个的时候。
transition属性
transition: width 1s linear 2s; /*简写实例*/
/*等同如下*/transition-property: width;transition-duration: 1s;transition-timing-function: linear;transition-delay: 2s;
tranform属性
实现我们需要的效果
当然,在这就直接放出代码,代码中有注释方便理解
/*css代码*/h2{ 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"> <h2 id="title"> 百度前端学院 <i class="line" id="line"></i> </h2> <button id="change">Change</button></div>//js部分代码(function () { let btn = document.getElementById('change'); let h2 = document.getElementById('title'); let line = document.getElementById('line'); let count = 0; btn.onclick = function () { if(count%2===0){line.style.transform = "scaleX(1)";h2.style.animation = "changeColor1 1s";h2.style.color = "#188EA7";count++; }else{line.style.transform = "scaleX(0)";h2.style.animation = "changeColor2 1s";h2.style.color = "#000";count++; }}})();
总结
到这里我们就已经将此效果完全呈现,同时我们也学习了CSS3中的transition属性和tranform属性。当然完成此动画还需要有一些html和css基础。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号