這篇文章主要介紹了基于jQuery創建鼠標懸停效果的方法,實例分析了jQuery實現鼠標特效的原理與詳細步驟,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了基于jQuery創建鼠標懸停效果的方法。分享給大家供大家參考。具體實現方法如下:
1. 創建HTML:
- <ul>
- <li><a href="/tv"><img src="images/tv_off.gif" class="mainnav"></a></li>
- </ul>
2. 選擇.mainnav的class:
- $(".mainnav").hover(
- function () {
- },
- function () {
- }
- );
3. 建立變量imgPath,指定圖片SRC:
- $(".mainnav").hover(
- function () {
- // Grab the source
- var imgPath = $(this).attr("src");
- },
- function () {
- // Grab the source
- var imgPath = $(this).attr("src");
- }
- );
4. 找到字符串off,用on替換:
- $(".mainnav").hover(
- function () {
- // Grab the source
- var imgPath = $(this).attr("src");
- // Replace the path in the source
- $(this).attr("src",imgPath.replace("off", "on"));
- },
- function () {
- // Grab the source
- var imgPath = $(this).attr("src");
- // Replace the path in the source
- $(this).attr("src",imgPath.replace("on", "off"));
- }
- );
希望本文所述對大家的jQuery程序設計有所幫助。
新聞熱點
疑難解答
圖片精選