使用CSS3来代替JS实现交互

发布时间:2022-04-13 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了使用CSS3来代替JS实现交互脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

【CSS3和JS】

对于CSS了解同学知道,CSS的实现是最底层的,在实现方式和性能上都不是,JS这种提供接口的脚本可比的;从CSS3的动画和JS动画对比角度来看两者,会更清晰;而且随着前端框架的使用,页面动画会越来越多的应用CSS3

【CSS3的其他用法】

除了动画的代替,还有就是对于各种交互的实现上,也体现了CSS的强大,更多的是使用CSS3提供的选择器;

先来看个例子:可以更好的体现我这次对于CSS的深刻感悟

<style>
body{background:#f4f4f4;margin:0;}
/*list*/
.list__con{}
.list__con .box{background:#fff;posITion:relative;border-bottom:solid 1px #858585;overflow:hidden;}
.list__con .box:hover{background:#f4f4f4;-webkit-transition:all .6s;transition:all .6s;}
.list__con .box:hover .delete{-webkit-transition:all .6s;transition:all .6s;opacity:1;}
.list__con .input{-webkit-apPEarance:none;appearance:none;position:absolute;top:10px;left:12px;padding:0;border:none;margin:0;width:24px;height:24px;border:solid 2px red;border-radius:50%;box-sizing:border-box;outline:none;cursor:pointer;}
.list__con .input:checked::after{content:'';width:14px;height:14px;background:red;position:absolute;top:3px;left:3px;border-radius:50%;}
.list__con label{line-height:24px;padding:10px 0 10px 48px;display:block;-webkit-transition:all .4s;transition:all .4s;}
.list__con .input:checked+label{color:#d9d9d9;text-decoration:line-through;}
.list__con .delete{width:44px;height:44px;float:right;position:relative;cursor:pointer;opacity:0;}
.list__con .delete:hover::after{-webkit-transform:rotate(225deg);transform:rotate(225deg);}
.list__con .delete:hover::before{-webkit-transform:rotate(225deg);transform:rotate(225deg);}
.list__con .delete::after{content:'';position:absolute;width:2px;height:20px;background:red;top:12px;left:50%;margin-left:-1px;-webkit-transform:rotate(45deg);transform:rotate(45deg);border-radius:4px;-webkit-transition:all .6s;transition:all .6s;}
.list__con .delete::before{content:'';position:absolute;width:20px;height:2px;background:red;top:50%;left:12px;margin-top:-1px;-webkit-transform:rotate(45deg);transform:rotate(45deg);border-radius:4px;-webkit-transition:all .6s;transition:all .6s;}
</style>
<div class="list__con">
    <div class="box">
        <div class="delete"></div>
        <input class="input" type="checkbox" />
        <label>啥地方垃圾费</label>
    </div>
</div>

【说明】

上面包括了诸多的交互,hover的交互、checked的交互;

对于上面的实现方法相信,早已经有很多人实现了;这里主要说说我对于这次的感悟和对于技的憧憬,到现在工作也有近四年了,在这些年中,成长快慢自知,但相同的一点就是,很多知识都会用,也知道怎么用;可这次才真正明白,什么叫只知其意,不知其真意,为何!

上面的交互效果,在之前我都是用css和js结合的方式实现;期间也尝试着全部使用css实现交互效果,不过却在探索的时候故步自封导致到现在才真正的实现当初的想法;

使用CSS完全实现的好处,提高了可复用性和可维护性,对于实现相应组件提供了更好的实现方式,同时也提高了性能;
&nbsp;

.list__con .input:checked+label 

如上面这种用法,我们可以使用CSS3增加的各种强大的选择器,实现更多更好的交互效果;从此离开使用JS改变DOM的悲剧,更多的倾向于CSS

脚本宝典总结

以上是脚本宝典为你收集整理的使用CSS3来代替JS实现交互全部内容,希望文章能够帮你解决使用CSS3来代替JS实现交互所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:jsjs