jquery中的事件和动画

本文详细介绍了jQuery中的事件绑定、合成事件及事件对象属性,并演示了如何使用jQuery进行动画效果的实现,包括显示隐藏、渐显渐隐及自定义动画等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1jQuery中的事件

    (1)绑定事件 bind()

        $(".head").bind("click",function(){

                            $("div.content").hide("slow");

                     }).bind("click",function(){

                            $("div.content").show("slow");

                     });

(2)合成事件

hover()  光标移入时触发第一个函数;光标移出时触发第二个函数

$("h5.head").hover(function(){

                              $("div.content").hide();

                       },function(){

                              $("div.content").show();

                       });

toggle()    第一次单击触发第一个函数,再次单击触发第二个函数

             $("h5.head").toggle(function(){

                            $("div.content").hide();

                     },function(){

                            $("div.content").show();

                     });

3)事件对象的属性

event.type()  事件类型

  $("h5.head").click(function(e){

                       alert(e.type);

                });

event.pageX()   event.pageY()鼠标的X Y

   $("h5.head").click(function(e){

                       $("#cord").html("X:"+e.pageX+"Y:"+e.pageY);

                });

event.which()  鼠标单击事件中获得鼠标的左中右键  键盘事件中获得键盘的按键

      $("h5.head").mousedown(function(e){

                            alert(e.which);

                     });

                     $("body").keydown(function(e){

                            alert(e.which);

                     });

4)事件模拟操作 trigger()

①常用模拟操作 trigger()

  $("h5.head").trigger("click");

②触发自定义事件

   $("h5.head").bind("myClick",function(){

                            $("div.content").hide(5000);

                     });

              $("h5.head").trigger("myClick");

             

③传递数据   参数1,触发的事件类型;   参数2,传递给事件处理函数的附加数据

        $("h5.head").bind("myClick",function(event,mes1,mes2,mes3){

                            alert(mes1+mes2+mes3);

                            $("div.content").hide(5000);

                     });

                     //附加数据的格式是一个数组的形式[]

              $("h5.head").trigger("myClick",['name','age','gender']);

 

2jQuery中的动画

1show()    hide()

     $("h5.head").toggle(function(){

              $("div.content").hide(5000);

                     },function(){

                            $("div.content").show("fast");

                     });

2fadeIn()fadeOut()

    $("h5.head").toggle(function(){

                            $("div.content").fadeIn(3000);

                     },function(){

                            $("div.content").fadeOut("fast");

                     });

3slideUp() slideDown()

    $("h5.head").hover(function(){

                            $("div.content").slideUp("slow");

                     },function(){

                            $("div.content").slideDown(3000);

                     });

(4)自定义动画:animate()

     $("div.content")

                     .animate({width:500,left:"+=100px",opacity:0.3},3000,function(){

                            $(this).toggle();

                     })

                     .css("background","red")

                     .click(function(){

                            $(this).fadeOut();

                     });

注意:

   ①动画都有速度参数 fast slow normal 自定义

   ②都有回调函数callback

   animate(params,speed,callback)需要相对定位

(5)停止动画  stop()

   $("h5.head").hover(function(){

                            $(this).addClass("high");

                            $(this).next().show(2000);

                     },function(){

                            $(this).removeClass("high");

                            $(this).next().stop().hide();

                     });

 

(6)判断是否处于动画状态is(":animated")

   if(!$("h5.head").is(":animated")){

                            $("h5.head").hover(function(){

                            $("div.content").show(5000);

                            },function(){

                                   $(this).next().stop().hide(3000);

                            });

                     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值