event.relatedTarget
訪問(wèn)事件相關(guān)元素
event.pageX / event.pageY
獲取光標(biāo)相對(duì)于頁(yè)面的x坐標(biāo)和y坐標(biāo)
event.which
在鼠標(biāo)單擊事件中獲取鼠標(biāo)的左、中、右鍵;在鍵盤事件中獲取鍵盤的按鍵(返回值1=鼠標(biāo)左鍵;2=鼠標(biāo)中鍵;3=鼠標(biāo)右鍵)
event.metaKey
鍵盤事件中獲取<ctrl>按鍵
event.originalEvent
指向原始的事件對(duì)象
6 移除事件
$("selector").unbind()
移除元素上的事件,格式:$("selector").unbind([type][,data]);(如果沒(méi)有參數(shù),則刪除所有綁定的事件;如果提供了事件類型參數(shù),則只刪除該類型的綁定事件;如果把在綁定時(shí)傳遞的處理函數(shù)作為第二個(gè)參數(shù),則只有這個(gè)特定的事件處理函數(shù)會(huì)被刪除)
例:
<script>$(function(){$('#btn').bind("click",myFun1=function(){ //先綁定$('#test').append("...");});})</script><script>$('#delOne').click(function(){$('#btn').unbind("click",myFun1); //后刪除})</script>$("selector").one()
綁定一個(gè)觸發(fā)一次即被刪除的事件,格式:$("selector").one(type[,data],fn);
7 模擬操作
$("selector").trigger("type");
模擬用戶交互動(dòng)作,簡(jiǎn)寫方法:$("#selector").type(); 格式:$("selector").trigger(type[,data])
例:用單擊替代鼠標(biāo)經(jīng)過(guò)
<script>$("selector").mouseover(function{//...});$("selector2").click(function(){$("selector").trigger("mouseover"); //或者$("selector").mouseover()})</script>自定義事件的例子
<script>$("selector").bind("myClick",function(){//...}); //綁定自定義事件$("selector").trigger("myClick"); //觸發(fā)自定義事件</script>$("selector").trigger(type[,data])
可以數(shù)組形式傳遞參數(shù)給回調(diào)函數(shù)
P119例:
<script>$("#btn").bind("myClick",function(event,message1,message2){$("#test").append("<p>"+message1+message2+"</p>");});$("#btn").trigger("myClick", ["我的自定義","事件"]);</script>
8 其他用法
$("selector").bind("type1 type2",function(){//...})
一次性綁定多個(gè)事件類型
P119值得一看的例子
<script>$(function(){$("div").bind("mouseover mouseout",function(){$(this).toggleClass("over"); //切換class});})</script>$("selector").bind("type.命名空間",function(){//...})
為多個(gè)事件添加事件命名空間,便于管理,刪除命名空間后,命名空間下的事件同時(shí)刪除,如:
$("div").bind("mouseover.plugin",function(){//...})
$("div").bind("click.plugin",function(){//...})
$("div").unbind(".plugin");
$("selector").trigger("type!")
"!"用來(lái)選擇匹配不包含在命名空間中的type方法
二、動(dòng)畫
1 show()方法和hide()方法
$("selector").show()
從display:none還原元素默認(rèn)或已設(shè)置的display屬性
$("selector").hide()
設(shè)置元素的display樣式為none,等于$("selector").css("display","none")
(注:傳入?yún)?shù)后,.show()和.hide()方法同時(shí)動(dòng)畫改變?cè)氐膚idth,height和透明屬性;傳入?yún)?shù)控制顯隱速度,單位毫秒,如.show(600),也可傳入fast,normal,slow,fast為200毫秒,normal為400毫秒,slow為600毫秒)
2 fadeIn()方法和fadeOut()方法
$("selector").fadeIn()
控制透明度在指定時(shí)間內(nèi)從display:none提高至完全顯示
$("selector").fadeOut()
控制透明度在指定時(shí)間內(nèi)降低至display:none;
3 slideUp()方法和slideDown()方法
$("selector").slideUp()
控制元素高度在指定時(shí)間內(nèi)從下到上縮短至display:none;
$("selector").slideDown()
控制元素高度在指定時(shí)間內(nèi)從display:none延伸至完整高度
4 自定義動(dòng)畫方法animate()
$("selector").animate(params,speed,callback);
params:一個(gè)包含樣式屬性及值的映射,比如{property1:"value1",property2:"value2",...}
speed:速度參數(shù),可選
callback:在動(dòng)畫完成時(shí)執(zhí)行的參數(shù)(即回調(diào)函數(shù)),可選
常見(jiàn)的動(dòng)畫例子
<script>//自定義動(dòng)畫的例子$(function(){$("selector").click(function(){$(this).animate({left:"500px"},3000); //selector在3秒內(nèi)向右移動(dòng)500px});})</script><script>//累加、累減動(dòng)畫的例子$(function(){$("selector").click(function(){$(this).animate({left:"+=500px"},3000); //連續(xù)觸發(fā)click事件時(shí),在當(dāng)前位置累加500px});})</script><script>//多重動(dòng)畫的例子$(function(){$("selector").click(function(){$(this).animate({left:"500px",top:"300px",height:"+=100px"},3000); //向右下30度方向運(yùn)動(dòng),同時(shí)增加高度});})</script><script>//按順序執(zhí)行多個(gè)動(dòng)畫的例子$(function(){$("selector").click(function(){$(this).animate({left:"500px"},3000).animate({top:"300px"},3000); //動(dòng)畫隊(duì)列});})</script>5 動(dòng)畫回調(diào)函數(shù)
因css()方法不會(huì)加入動(dòng)畫隊(duì)列中,則會(huì)馬上執(zhí)行。如若要在動(dòng)畫最后改變selector的css,需要利用回調(diào)函數(shù)
例:
<script>$("selector").click(function(){$(this).animate({property1:"value1"},time).animate({property2:"value2"},time,function(){$(this).css("property3","value3"); //css()方法利用回調(diào)函數(shù)加入動(dòng)畫隊(duì)列});})</script>(注:動(dòng)畫回調(diào)函數(shù)適用于jQuery所有的動(dòng)畫效果方法)
6 停止動(dòng)畫和判斷是否處于動(dòng)畫狀態(tài)
$("selector").stop()
結(jié)束當(dāng)前動(dòng)畫,如隊(duì)列中存在下一個(gè)動(dòng)畫則立即執(zhí)行下一個(gè)動(dòng)畫,格式$("selector").stop([clearQueue][,gotoEnd])
切換動(dòng)畫的例子:
<script>$("selector").hover(function(){$(this).stop().animate();},function(){$(this).stop().animate();})</script>clearQueue參數(shù)設(shè)置為true時(shí),將清空當(dāng)前元素接下來(lái)尚未執(zhí)行完的動(dòng)畫隊(duì)列
例:
<script>$("selector").hover(function(){$(this).stop(true).animate().animate() //如此時(shí)觸發(fā)光標(biāo)移出事件,直接跳過(guò)后面的動(dòng)畫隊(duì)列,避免執(zhí)行本隊(duì)列第二個(gè)動(dòng)畫},function(){$(this).stop(true).animate().animate()})</script>
gotoEnd參數(shù)設(shè)置為true時(shí),可將正在執(zhí)行的動(dòng)畫直接到達(dá)結(jié)束時(shí)刻的狀態(tài)
is(":animated")
判斷元素是否處于動(dòng)畫狀態(tài),可用于防止動(dòng)畫累積
例:
<script>if(!$("selector").is(":animated")){ //判斷元素是否正處于動(dòng)畫狀態(tài) //如果當(dāng)前沒(méi)有進(jìn)行動(dòng)畫,則添加新動(dòng)畫}</script>
7 其他動(dòng)畫方法
3個(gè)專門用于交互的動(dòng)畫方法:toggle(speed,[callback]); slideToggle(speed,[callback]); fadeTo(speed,opacity,[callback])
$("selector").toggle()
切換元素的可見(jiàn)狀態(tài),如元素隱藏則切換為可見(jiàn),反之亦然
$("selector").slideToggle()
通過(guò)高度變化來(lái)切換元素的可見(jiàn)性
$("selector").fadeTo()
把元素的不透明度以漸進(jìn)方式調(diào)整到指定的值,如$("selector").fadeTo(600,0.2);以600毫秒速度將內(nèi)容調(diào)整到20%透明度
8 動(dòng)畫方法概括
toggle()用來(lái)代替hide()和show()
slideToggle()用來(lái)代替slideUp()和slideDown()
animate()可代替所有動(dòng)畫方法