【學習筆記】鋒利的jQuery(三)事件和動畫

來源:互聯網
上載者:User

標籤: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(三)事件和動畫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.