平滑過渡效果的開關(guān)

示例

這里是一個 Demo.

現(xiàn)在開始吧...

準備材料

  1. 編輯器:sublime text3,隨自己喜歡~
  2. 谷歌瀏覽器: chrome,不是IE都好說,Edge也是可以的

html代碼

<pre><code>

<div class="container">   //這是一個大容器,里面可以放很多的div.col
    <div class="col">   //這是橫向整列的一個容器
        <div class="choice">    //這才是按鈕本身
            <a href="#" class="button" id="button">
                <span class="icon"></span>
            </a>
        </div>
    </div>
</div>```
</code></pre>

####css代碼
>body,html{
    background-color: #eee;
    margin: 0;
    padding: 0;
}  
header{
    width: 100%;
    height: 150px;
    background-color: #2A324B;
}
.container{
    width: 960px;
    margin: 0 auto;
    background-color: #fff;
    padding: 30px 0;
}
.col{
    width: 100%;
    height: auto;
    padding: 0;
    margin: 20px 0;
    padding: 50px 0;
    background-color: #2A324B;
}
.choice{
    width: 150px;
    height: 50px;
    border-radius: 25px;
    margin: 0 auto;
    position: relative;
    border:2px solid #fff;
    background-color: #0096cf;
    text-align: center;
    line-height: 50px;
    font-size: 26px;
    color: #fff;
}
.button{
    width: 50px;
    height: 50px;
    border-radius: 100%;
    background-color: #00A8E8;
    /* #d9534f;*/
    display: block;
    box-shadow: 2px 3px 8px rgba(0,0,0,.7);
    transition: all 0.5s;
    text-align: center;
    margin-top: 0;
    position: absolute;
    top: 0
}
.icon{  
    width: 0;height: 0;
    position: absolute;
    margin: 22px -13px;
}
.icon:before{
    content: '';
    width: 19px;
    height: 3px;
    background-color: #fff;
    display: block;
    position: absolute;
    -webkit-transform: translate(-50%, 5px) rotate(40deg);
              transform: translate(-50%, 5px) rotate(40deg);
    border-radius: 3px;
    transition:  all .5s;
}
.icon:after{
    content: '';
    width: 3px;
    height: 33px;
    background-color: #fff;
    display: block;
    position: absolute;
    -webkit-transform: translate(16px, -15px) rotate(45deg);
              transform: translate(16px, -15px) rotate(45deg);
    border-radius: 3px;
    transition:  all .5s;
}
.choice.active .button{
    margin-left: 100px;
    box-shadow: -2px 3px 8px rgba(0,0,0,.7);
}
.choice.active .icon:before{
    width: 33px;
    height: 3px;
    -webkit-transform: translate(-5px, 3px) rotate(45deg);
              transform: translate(-5px, 3px) rotate(45deg);
}
.choice.active .icon:after{
    width: 3px;
    height: 33px;
    -webkit-transform: translate(10px, -12px) rotate(45deg);
              transform: translate(10px, -12px) rotate(45deg);
}


####然后你就可以在瀏覽器中看見


![right.png](http://upload-images.jianshu.io/upload_images/1116060-92ea9104a03819bd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

####接下來我們給他把效果加上去
<pre><code>

<script src="js/jquery.min.js"></script> //這里我引用了jquery,要換成自己的路徑哦~
<script>
$("#button").click(function(){//按鈕點擊的時候觸發(fā)事件
$(".choice").toggleClass("active"); //toggleClass函數(shù)的作用是,添加或移除active,
//有就移除,沒有就添加。
})
</script>
```

</code></pre>

刷新頁面,然后我們點擊按鈕就會得到...

error.png

而且他在變換時候的效果也是漸變過來的喲,怎么樣,是不是比用替換圖片完成效果好多了呢?
這里的代碼還有很多沒有優(yōu)化的地方,就交給你們自己完成啦~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 各種純css圖標(biāo) CSS3可以實現(xiàn)很多漂亮的圖形,我收集了32種圖形,在下面列出。直接用CSS3畫出這些圖形,要比...
    劍殘閱讀 9,977評論 0 8
  • 1.長方形 #Rectangle{ width: 200px; height: 50px; background-...
    一直以來都很好閱讀 553評論 0 0
  • 1、垂直對齊 如果你用CSS,則你會有困惑:我該怎么垂直對齊容器中的元素?現(xiàn)在,利用CSS3的Transform,...
    kiddings閱讀 3,287評論 0 11
  • 5.橢圓 #ellipse{ width: 200px; height: 100px; background-co...
    小茶葉葉閱讀 2,203評論 0 1
  • 看了,一粟爸爸寫的日記,我也深有體會。我們是個體自己干,給個人和公司、酒店送大桶水和瓶裝水的。暑假的一天,我給他留...
    45cbff51831c閱讀 203評論 1 3

友情鏈接更多精彩內(nèi)容