jQuery hover事件
公司主營業(yè)務(wù):成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)建站是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)建站推出七星關(guān)區(qū)免費(fèi)做網(wǎng)站回饋大家。
hover(over,out)一個(gè)模仿懸停事件(鼠標(biāo)移動到一個(gè)對象上面及移出這個(gè)對象)的方法。這是一個(gè)自定義的方法,它為頻繁使用的任務(wù)提供了一種“保持在其中”的狀態(tài)。
當(dāng)鼠標(biāo)移動到一個(gè)匹配的元素上面時(shí),會觸發(fā)指定的第一個(gè)函數(shù)。當(dāng)鼠標(biāo)移出這個(gè)元素時(shí),會觸發(fā)指定的第二個(gè)函數(shù)。而且,會伴隨著對鼠標(biāo)是否仍然處在特定元素中的檢測(例如,處在div中的圖像),如果是,則會繼續(xù)保持“懸?!睜顟B(tài),而不觸發(fā)移出事件(修正了使用mouseout事件的一個(gè)常見錯(cuò)誤)。
參數(shù) :
over (Function) : 鼠標(biāo)移到元素上要觸發(fā)的函數(shù)
out (Function): 鼠標(biāo)移出元素要觸發(fā)的函數(shù)
示例 :
鼠標(biāo)懸停的表格加上特定的類
jQuery 代碼:
$("td").hover(
function () {
$(this).addClass("hover");
},
function () {
$(this).removeClass("hover");
}
);
實(shí)例如下:
body{
font-size:12px;
margin:0px;
}
#box{
width:150px;
margin:auto;
}
.menu{
width:150px;
line-height:25px;
background:#fcc;
}
.level1{
border-color:#fba;
border-style:solid;
border-width:0px1px 1px;
}
ul,li {list-style-type:none;margin:0;padding:0;}
.menuli ul{overflow:hidden; display:none;}
.menuli.level1 a{
display: block;
height: 28px;
line-height: 28px;
color:#42556B;
text-decoration:none;
}
.level2{
background-color:white;
}
.level2li a {
display:block;
height: 28px;
line-height: 28px;
color:#888;
background-color:white;
}
.level2li a:hover {
color:#f00;
}
.current{
overflow:hidden;
background-color:#fba;
}
function dropMenu(obj){
$(obj).each(function(){ //遍歷當(dāng)前元素下的每個(gè)元素
vartheSpan = $(this);
vartheMenu = theSpan.find(".level2"); //查找類名為".level2"的每個(gè)元素
vartarHeight = theMenu.height();
theMenu.css({height:0,opacity:0});
theSpan.hover(
function(){
$(this).addClass("current");
theMenu.stop().show().animate({height:tarHeight,opacity:1},500);
},
function(){
$(this).removeClass("current");
theMenu.stop().animate({height:0,opacity:0},500,function(){
$(this).css({display:"none"});
});
}
);
});
}
$(document).ready(function(){
dropMenu(".level1");
});
效果圖如下: