今天分享使用html+css3+少量jquery實現鼠標移入移出圖片生成隨機動畫,我們先看最終效果圖(截圖為靜態效果,做出來可是動態的喲)
左右旋轉
上下移動
縮放
由於時間關系我就不一步步解析各段代碼所代表含義,我這里就給出一些思路及關鍵代碼:
1、首先使用ul li展現4張圖片
本示例中不僅使用了圖片,在圖片表面還放置了一段“WEB”字樣文字,用於與圖片實現隱藏或顯示效果,故html中每張圖片上方加入:
<div class="mytext">WEB</div>
2、CSS控制圖片及文字透明度
本示例中一組圖片與文字同時放在一個li里面,高度與寬度設置與li一樣大,並使用絕對定位固定它們的位置:
#myimg ul li a div { position: absolute; top: 0; left: 0; width: 100%; height: 100%; text-align: center; font-size: 40px;
}
默認將隱藏圖片,只顯示文字,鼠標放入li時顯示圖片,這里使用opacity透明度屬性控制:
#myimg ul li a div.pic { opacity: 0;
} #myimg ul li:hover a div.pic { opacity: 1;
}
3、CSS3自定義動畫
本示例中用css3定義了3種動畫:
@keyframes rot /*自定義 旋轉動畫*/
{ 0% 20% 40% 60% 80% 100% { transform-origin: top center;
} 0% { transform: rotate(0deg) } 20% { transform: rotate(-20deg) } 40% { transform: rotate(15deg) } 60% { transform: rotate(-10deg) } 80% { transform: rotate(5deg) } 100% { transform: rotate(0deg) } } @keyframes top /*自定義 上下動畫*/
{ 0% { top: 0 } 20% { top: 20px } 40% { top: -15px } 60% { top: 10px } 80% { top: -5px } 100% { top: 0px } } @keyframes sca /*自定義 縮放動畫*/
{ 0% { transform: scale(1) } 20% { transform: scale(1.1) } 40% { transform: scale(0.9) } 60% { transform: scale(1.05) } 80% { transform: scale(0.95) } 100% { transform: scale(1) } }
使用css執行上述自定義動畫:
#myimg ul li.rot { animation: rot 1s;
} #myimg ul li.top { animation: top 1s;
} #myimg ul li.sca { animation: sca 1s;
}
4、Jquery生成隨機動畫
當鼠標移入ul li時,使用jquery隨機產生上述3種自定義動畫,這里使用hover事件
HTML代碼如下:
<div id="myimg">
<ul>
<li>
<a href="#">
<div class="mytext">WEB</div>
<div class="pic"><img src="img/5.png"></div>
</a>
</li>
<li>
<a href="#">
<div class="mytext">WEB</div>
<div class="pic"><img src="img/5.png"></div>
</a>
</li>
<li>
<a href="#">
<div class="mytext">WEB</div>
<div class="pic"><img src="img/5.png"></div>
</a>
</li>
<li>
<a href="#">
<div class="mytext">WEB</div>
<div class="pic"><img src="img/5.png"></div>
</a>
</li>
</ul>
</div>
CSS代碼如下:
<style type="text/css"> * { padding: 0; margin: 0;
} #myimg { width: 800px; margin: 20px auto;
} #myimg ul li { list-style-type: none; position: relative; float: left; width: 350px; height: 200px; line-height: 200px; margin: 20px;
} #myimg ul li.rot { animation: rot 1s;
} #myimg ul li.top { animation: top 1s;
} #myimg ul li.sca { animation: sca 1s;
} #myimg ul li:hover a div.pic { opacity: 1;
} #myimg ul li a { text-decoration: none; color: white;
} #myimg ul li a div { position: absolute; top: 0; left: 0; width: 100%; height: 100%; text-align: center; font-size: 40px;
} #myimg ul li a div.pic { opacity: 0;
} #myimg ul li:nth-child(1) a div.mytext { background: black;
} #myimg ul li:nth-child(2) a div.mytext { background: blue;
} #myimg ul li:nth-child(3) a div.mytext { background: darkred;
} #myimg ul li:nth-child(4) a div.mytext { background: orange;
} @keyframes rot /*自定義 旋轉動畫*/
{ 0% 20% 40% 60% 80% 100% { transform-origin: top center;
} 0% { transform: rotate(0deg) } 20% { transform: rotate(-20deg) } 40% { transform: rotate(15deg) } 60% { transform: rotate(-10deg) } 80% { transform: rotate(5deg) } 100% { transform: rotate(0deg) } } @keyframes top /*自定義 上下動畫*/
{ 0% { top: 0 } 20% { top: 20px } 40% { top: -15px } 60% { top: 10px } 80% { top: -5px } 100% { top: 0px } } @keyframes sca /*自定義 縮放動畫*/
{ 0% { transform: scale(1) } 20% { transform: scale(1.1) } 40% { transform: scale(0.9) } 60% { transform: scale(1.05) } 80% { transform: scale(0.95) } 100% { transform: scale(1) } } </style>
Jquery代碼如下:
<script type="text/javascript"> $(function() { var anim = ['rot', 'top', 'sca']; var a, b; $("#myimg ul li").hover(function() { //向下取0-2整數
a = anim[Math.floor(Math.random() * 3)]; while (b == a) { a = anim[Math.floor(Math.random() * 3)]; } $(this).addClass(a); b = a; }, function() { $(this).removeClass(a); }) }); </script>
注意事項:需要引入jquery文件,可以自行選擇jquery版本
好了,今天分享就到這里,以后還有更多喲,請大家一起來交流下