先給大家推薦animate.css庫,里面有一些效果很不錯的過度樣式,不想自己寫的也可以直接安裝這個庫來使用,如果不想安裝這個庫也可以去https://daneden.github.io/animate.css/挑選自己喜歡的樣式之后F12復制相應的樣式代碼或者該網站里面也有源碼可以復制。這個庫的安裝及用法直接百度結果有很多。
樣式一:效果如下
<div class="ballon"></div>
/*css部分*/
@keyframes scaleDraw { /*定義關鍵幀、scaleDrew是需要綁定到選擇器的關鍵幀名稱*/ 0%{ transform: scale(1); /*開始為原始大小*/ } 25%{ transform: scale(1.1); /*放大1.1倍*/ } 50%{ transform: scale(1); } 75%{ transform: scale(1.1); } } .ballon{ width: 150px; height: 200px; background: url("images/balloon.png"); background-size: 150px 200px; -webkit-animation-name: scaleDraw; /*關鍵幀名稱*/ -webkit-animation-timing-function: ease-in-out; /*動畫的速度曲線*/ -webkit-animation-iteration-count: infinite; /*動畫播放的次數*/ -webkit-animation-duration: 5s; /*動畫所花費的時間*/ }
上面的幾個屬性也可以合在一起寫
-webkit-animation: scaleDraw 5s ease-in-out infinite;
樣式二:效果如下
實質就是就是利用了動畫的延遲屬性,兩層圓的動畫相關的屬性基本相同,除了最外層的圓多設置了animation-delay屬性
<div class="live"> <img src="images/live.png" alt=""> <span></span> <span></span> </div>
.live{ position: relative; width: 100px; height: 100px; } .live img{ width: 100px; height: 100px; z-index: 0; } @keyframes living { 0%{ transform: scale(1); opacity: 0.5; } 50%{ transform: scale(1.5); opacity: 0; /*圓形放大的同時,透明度逐漸減小為0*/ } 100%{ transform: scale(1); opacity: 0.5; } } .live span{ position: absolute; width: 100px; height: 100px; left: 0; bottom: 0; background: #fff; border-radius: 50%; -webkit-animation: living 3s linear infinite; z-index: -1; } .live span:nth-child(2){ -webkit-animation-delay: 1.5s; /*第二個span動畫延遲1.5秒*/ }
樣式三:效果如下
思路:將第二張圖片用絕對定位疊加在第一張圖片上,通過在動畫函數里設置透明度來控制圖片的顯示隱藏
<div class="pics"> <img src="images/avatar1.png" alt="" class="pic1"> <img src="images/avatar2.png" alt="" class="pic2"> </div>
.pics{ position: relative; width: 400px; height: 400px; } .pic1{ width: 400px; height: 400px; } @keyframes picDraw { 0%{ opacity: 0; } 50%{ opacity: 1; } 100%{ opacity: 0; } } .pic2{ position: absolute; width: 400px; height: 400px; left: 0; top: 0; -webkit-animation: picDraw 5s ease-in-out infinite; }
原文鏈接:https://blog.csdn.net/yujin0213/article/details/79262825