標籤:style blog color io 使用 ar strong div sp
一、jQuery事件
1,載入事件
$(document).ready(function(){...}) //等同於$(function(){..})$(window).load(function(){...}) //等同於window.onload = fn
2,基本事件綁定
bind(type,[.data],fn) //可綁定多個事件:bind("mouseover mouseout",fn)unbind(type,fn) //解除綁定one(type,[.data],fn) //綁定的事件只執行一次
//簡寫綁定
click(fn)/dblclick(fn)mouseover(fn)/mouseout(fn)mouseenter(fn)/mouseleave(fn) //不包括移出元素內部的子項目
scroll(fn)
//常用表單事件
focus(fn)/blur(fn)
keyup(fn)/keydown(fn) //按下和松下按鍵
keypress(fn) //按了個索引值時候
change(fn) //表單文本域(input:text,textarea,select)值改變時觸發
//合成事件hover(fn1,fn2) //相當於bind("mouseenter")和bind("mouseleave")toggle(fn1,fn2,fn3...) //連續單擊事件,輪番調用多個函數trigger(type) //觸發對應type的事件,會執行瀏覽器預設操作,type可為自訂名稱triggerHandler(type) //綁定type事件,且阻止瀏覽器預設操作
3,事件的命名空間
bind("click.name",fn);bind("mouseover.name",fn);bind("click",fn);unbind(".name"); //命名空間解除綁定trigger("click!"); //匹配不包含命名空間的click,即第三個
4,event事件對象
bind(type,function(event){....}) //添加事件對象參數,推薦命名為event或e,原理上可隨便命名//event屬性event.type event.target event.relatedTarget //觸發事件的相關元素event.pageX/event.pageY //相對於頁面的x和y座標
event.which //鍵盤事件:返回按鍵的ASCALL碼,點擊事件:1(左)2(中)3(右)。jq中整合了keyCode和charCode的相容及滑鼠事件
event.keyCode //返回按鍵的ASCALL碼,建議用which//event方法,兩個同時使用,相當於事件裡邊添加"return false;"event.stopPropagation(); //阻止冒泡event.preventDefault(); //阻止瀏覽器事件預設行為
注意:jQuery中,擷取和設定類似disabled和checked屬性時,用prop()方法代替attr(),只有true/false值。
二、jQuery動畫
/* *params:動畫的最終樣式或值的效果,如{left:"+=500px"},left會進行累加 *speed:動畫時間,單位ms。slow為600,normal為400,fast為200 *callback:動畫完成後執行的回呼函數 */animate(params,speed,callback);$div.animate().animate()... //按順序執行動畫.stop([clearQueue],[goEnd]) //兩個布爾值參數可選,無參數僅停止當前動畫.delay() //延遲執行//動畫簡寫hide()/show() //相當於css("display":"none/block/inline")slideUp()/slideDown() //通過高度上下展開隱藏fadeIn()/fadeOut() //淡進、淡出fadeTo(speed,0~1) //漸進方式調到指定透明度//組合動畫toggle()slideToggle()fadeToggle()
//animate(params,500),常用的params
{scrollTop:"-=50"}
{height:"+=50"}
【學習筆記】鋒利的jQuery(三)事件和動畫