JQuery事件與動畫

來源:互聯網
上載者:User

標籤:click   seo   slide   多個   color   lin   傳遞   line   效果   

事件綁用捷徑

$("button:first").click(function(){
alert(1);
});

 使用on綁定事件

 ① 使用on進行單事件綁定
$("button").on("click",function(){
 $(this) 取到當前呼叫事件函數的對象
console.log($(this).html());
});

 ② 使用on同時為多個事件,綁定同一函數
$("button").on("mouseover click",function(){
console.log($(this).html())
})*/

/③ 調用函數時,傳入自訂參數
$("button").on("click",{name:"jianghao"},function(event){
/使用event.data.屬性名稱 找到傳入的參數
console.log(event.data.name);
})

/④ 使用on進行多事件多函數綁定
$("button").on({
click:function(){
console.log("click")
},
mouseover:function(){
console.log("mouseOver")
}
});*/

⑤ 使用on進行事件委派
 >>> 將原本需要綁定到某元素上的事件,改為綁定在父元素乃至根節點上,然後委派給當前元素生效;
 eg:$("p").click(function(){});
 ↓
 $(document).on("click","p",function(){});
>>> 作用:
 預設的綁定方式,只能綁定到頁面初始時已有的p元素,當頁面新增p元素時,無法綁定到新元素上;
 使用事件委派方式,當頁面新增元素時,可以為所有新元素繫結事件



$("button").on("click",function(){
var p = $("<p>444444</p>");
$("p").after(p);
});

$("p").click(function(){
alert(1);
});

$(document).on("click","p",function(){
alert(1);
});




 off() 取消事件綁定

 1. $("p").off(): 取消所有事件
 2. $("p").off("click"): 取消點擊事件
 3. $("p").off("click mouseover"):取消多個事件
 4. $(document).off("click","p"):取消事件委派


 

 使用.one() 綁定事件,只能執行一次

$("button").one("click",function(){
alert(1);
})



.trigger("event"):自動觸發某元素的事件

 $("p").trigger("click",["haha","hehe"]); 觸發事件時,傳遞參數

$("p").click(function(event,arg1,arg2){
alert("觸發了P的click事件"+arg1+arg2);
})

$("button").click(function(){
$("p").trigger("click",["haha","hehe"]);
})*/


動畫

 .show():讓隱藏元素顯示,同時修改元素的寬度、高度、opacity屬性

 ① 不傳參:讓隱藏的元素直接顯示,不進行動畫
 ② 傳入時間:多少毫秒之內完成動畫
 ③ 傳入(時間,函數):完成動畫之後,執行回呼函數
 .hide():讓顯示元素隱藏,與show()相反;
 .slideDown(): 讓隱藏元素顯示,效果為從上往下,增加高度
.slideUp(): 讓顯示的元素隱藏,效果從下往上,減小高度
 .slideToggle():讓顯示的隱藏,讓隱藏的顯示

 .fadeOut(): 讓顯示元素隱藏,淡出
.fadeIn(): 讓隱藏元素顯示,淡入
.fadeToggle(): 讓顯示的隱藏,讓隱藏的顯示。淡入淡出
 .fadeTo(時間,透明度,函數):同fadeToggle,但是多了透明度參數,可以指定顯示的最終透明度
.animate({最終的樣式屬性索引值對},動畫時間,動畫效果("linear" "swing"),回呼函數): 自訂動畫

 

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.