使用CSS3來(lái)代替JS實(shí)現(xiàn)交互
【CSS3和JS】
對(duì)于CSS了解的同學(xué)都知道,CSS的實(shí)現(xiàn)是最底層的,在實(shí)現(xiàn)方式和性能上都不是,JS這種提供接口的腳本可比的;從CSS3的動(dòng)畫和JS動(dòng)畫對(duì)比角度來(lái)看兩者,會(huì)更清晰;而且隨著前端框架的使用,頁(yè)面動(dòng)畫會(huì)越來(lái)越多的應(yīng)用CSS3
【CSS3的其他用法】
除了動(dòng)畫的代替,還有就是對(duì)于各種交互的實(shí)現(xiàn)上,也體現(xiàn)了CSS的強(qiáng)大,更多的是使用CSS3提供的選擇器;
先來(lái)看個(gè)例子:可以更好的體現(xiàn)我這次對(duì)于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>啥地方垃圾費(fèi)</label>
</div>
</div>

【說(shuō)明】
上面包括了諸多的交互,hover的交互、checked的交互;
對(duì)于上面的實(shí)現(xiàn)方法相信,早已經(jīng)有很多人實(shí)現(xiàn)了;這里主要說(shuō)說(shuō)我對(duì)于這次的感悟和對(duì)于技術(shù)的憧憬,到現(xiàn)在工作也有近四年了,在這些年中,成長(zhǎng)快慢自知,但相同的一點(diǎn)就是,很多知識(shí)都會(huì)用,也知道怎么用;可這次才真正明白,什么叫只知其意,不知其真意,為何!
上面的交互效果,在之前我都是用css和js結(jié)合的方式實(shí)現(xiàn);期間也嘗試著全部使用css實(shí)現(xiàn)交互效果,不過(guò)卻在探索的時(shí)候故步自封導(dǎo)致到現(xiàn)在才真正的實(shí)現(xiàn)當(dāng)初的想法;
使用CSS完全實(shí)現(xiàn)的好處,提高了可復(fù)用性和可維護(hù)性,對(duì)于實(shí)現(xiàn)相應(yīng)組件提供了更好的實(shí)現(xiàn)方式,同時(shí)也提高了性能;
.list__con .input:checked+label
如上面這種用法,我們可以使用CSS3增加的各種強(qiáng)大的選擇器,實(shí)現(xiàn)更多更好的交互效果;從此離開使用JS改變DOM的悲劇,更多的傾向于CSS
相關(guān)文章

JS+CSS3實(shí)現(xiàn)鼠標(biāo)可拖動(dòng)的交互式立方體旋轉(zhuǎn)特效源碼
是一段實(shí)現(xiàn)了立方體圖塊可根據(jù)鼠標(biāo)的拖動(dòng)方向進(jìn)行旋轉(zhuǎn)的交互式效果代碼,純?cè)С质髽?biāo)拖動(dòng)展示,本段代碼適應(yīng)于所有網(wǎng)頁(yè)使用,有興趣的朋友們可以前來(lái)下載使用2016-08-18
CSS3+JS實(shí)現(xiàn)的不同交互彈出框帶關(guān)閉按鈕動(dòng)態(tài)特效源碼
是一段實(shí)現(xiàn)了多款從不同角度彈出的擁有不同風(fēng)格的無(wú)刷新效果的遮罩提示框帶關(guān)閉按鈕效果代碼,本段代碼適用于所有網(wǎng)頁(yè)使用,有需要的朋友們可以選擇其中一款或多款交互彈出2015-01-28



