初学jQuery之jQuery事件与动画

时间:2023-03-09 04:26:15
初学jQuery之jQuery事件与动画

今天我们就谈谈jquery中的事件和简单动画吧,它们毕竟基础是进阶华丽的根本!!

1.事件

1.window事件

ready   准备就绪

2.鼠标事件

方法                        执行时机

click(fn)                单击鼠标

$(document).ready(function(){
$("dd>img").click(function(){
$("dt>img").show();
});

mouseover(fn)     鼠标指针移过时

mouseout(fn)       鼠标指针移出时

 $("#nav .navsBox ul li").mouseover(function(){
$(this).addClass("onbg"); //为该元素添加类样式.onbg
}).mouseout(function(){
$(this).removeClass("onbg");//为该元素移除类样式.onbg
});

hover()

     

 $(".top").hover(function(){
$(this).addClass('bgreen');
},function(){
$(this).removeClass('bgreen');
});

3.键盘事件

keydown()        按下键盘时

keyup()             释放按键时

keypress()        产生可打印的字符时


$(function(){
   $("[type=password]").keyup(function(){
    $("#e").append("keyup");
   }).keydown(function(){
     $("#e").append("keydown");
   }).keypress(function(){
      $("#e").append("keypress");
   });
  
   $(document).keydown(function(event){
     if(event.keyCode=="13"){
        alert("确认要提交么???");
     }
   });
});
 

4.表单事件

focus()             获得焦点

blur()                失去焦点

 $(function(){

       $("input").focus(function(){

          $(this).next().css("color","red");
//alert("1123");
}); $("input").blur(function(){
$(this).next().css("color","");
});
});

绑定事件与移除事件

bind(type,[data],fn)   (绑定)

type     主要包括blur,focus,click,mouseout等基础事件,此外,还可以是自定义事件

[data]   作为event.data属性值传递给事件对象的额外数据对象,该参数不是必需的

fn         用来绑定处理函数

unbind([type],[fn])    (移除)

type    主要包括blur,focus,click,mouseout等基础事件,此外,还可以自定义事件

fn        用来解除绑定的处理函数

$(function(){
$("li").bind({
mouseover:function(){
$(this).css("background-color","green");
},mouseout:function(){
$(this).css("background-color","");
}
});
$("li").unbind(); });

2.动画

1.控制元素显示与隐藏  $(selector).show([speed],[callback])

$(selector).hide([speed],[callback])

speed:可选。规定元素从隐藏(显示)到可见(不可见)的速度

callback :可选。show函数执行完了之后,要执行的函数

$(function(){
$("p:visible").hide(100);
}); //$("p:hidden").show(100);

2.改变元素的透明度

  $(function(){
    $("input").click(function(){
      $("img").fadeOut(100);    //浅出
      //$("img").fadeIn(100);   淡入
    });
  })

3.改变元素的高度

$(function(){
$("h2").click(function(){
// $(".txt").slideup();
$(".txt").slideDown();
});
});

4.自定义动画

$(function(){
$("img").animate({width:"90%"},5000)
.animate({height:"90%"},{queue:false,duration:5000})
.animate({borderwidth:5},{queue:false,duration:5000});
});

queue:队列