jQuery事件与特效

目录

jQuery事件

鼠标事件

         click()方法

        dblclick()方法

        mouseover()方法

        mouseout()方法

绑定和移出事件

  on()方法

        off()方法

复合事件

        hover()方法

jQuery特效

控制元素显示与隐藏

        show()方法

        hide()方法

元素淡入、淡出

        fadeIn()和fadeOut()方法

改变元素高度显示与隐藏

        slideUp()方法

        slideDown()方法

总结


jQuery事件

鼠标事件

         click()方法

        描述:鼠标单击时触发绑定到指定元素的click事件

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>click方法</title>
		<script src="js/jQuery.js"></script>
		<style>
			div{
				width: 300px;
				height: 300px;
				border: 2px solid #000;
			}
		</style>
	</head>
	<body>
		<div></div>
	</body>
	<script>
		$(function(){
			//获取div元素并设置鼠标单击事件
			$('div').click(function(){
				//鼠标单击时获取div元素设置背景色红色
				$('div').css('background-color','#f00');
			});
		});
	</script>
</html>

        dblclick()方法

        描述:鼠标双击时触发绑定到指定元素的click事件

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>click方法</title>
		<script src="js/jQuery.js"></script>
		<style>
			div{
				width: 300px;
				height: 300px;
				border: 2px solid #000;
			}
		</style>
	</head>
	<body>
		<div></div>
	</body>
	<script>
		$(function(){
			//获取div元素并设置鼠标双击事件
			$('div').dblclick(function(){
				//鼠标双机时获取div元素设置背景色绿色
				$('div').css('background-color','#0f0');
			});
		});
	</script>
</html>

        mouseover()方法

        描述:鼠标移入时触发绑定到指定元素的mouseover事件

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>mouseover方法</title>
		<script src="js/jQuery.js"></script>
		<style>
			div{
				width: 300px;
				height: 300px;
				border: 2px solid #000;
			}
		</style>
	</head>
	<body>
		<div></div>
	</body>
	<script>
		// 获取div元素并设置mouseover方法
		$('div').mouseover(function(){
			//鼠标移入时,获取div元素设置背景色lvse
			$('div').css('background-color','#0f0');
		})
	</script>
</html>

        mouseout()方法

         描述:鼠标移出时触发绑定到指定元素的mouseover事件

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>mouseout方法</title>
		<script src="js/jQuery.js"></script>
		<style>
			div{
				width: 300px;
				height: 300px;
				border: 2px solid #000;
			}
		</style>
	</head>
	<body>
		<div></div>
	</body>
	<script>
		// 获取div元素并设置mouseout方法
		$('div').mouseout(function(){
			//鼠标移出时,获取div元素设置背景色红色
			$('div').css('background-color','#f00');
		})
	</script>
</html>

绑定和移出事件

  on()方法

        描述:在on方法中可以绑定一个或者多个事件,当on方法中绑定多个参数时写在{}里,各事件之间用逗号隔开

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>on方法</title>
		<script src="js/jQuery.js"></script>
		<style>
			div{
				width: 300px;
				height: 300px;
				border: 2px solid #000;
			}
		</style>
	</head>
	<body>
		<div></div>
	</body>
	<script>
		// $(function(){
		// 	//获取div元素并在on方法中设置click方法绑定事件
		// 	$('div').on('click',function(){
		// 		$('div').css('background-color','#f00');
		// 	});
		// });
		$(function(){
			//获取div元素并在on方法中设置click方法绑定事件
			$('div').on({'click':function(){
				$('div').css('background-color','#f00');
			},'dblclick':function(){
				$('div').css('background-color','#0f0');
			}
			});
		});
	</script>
</html>

        off()方法

        描述: off()事件移出方法,如果方法中有参数,则移出该元素上的事件;如果方法中没有参数,则移出该元素上的所有事件

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>off方法</title>
		<script src="js/jQuery.js"></script>
		<style>
			div{
				width: 300px;
				height: 300px;
				border: 2px solid #000;
			}
		</style>
	</head>
	<body>
		<div></div>
	</body>
	<script>
		$(function(){
			//获取div元素并在on方法中设置click方法绑定事件
			$('div').on({'click':function(){
				$('div').css('background-color','#f00');
			},'dblclick':function(){
				$('div').css('background-color','#0f0');
			}
			});
			// off():事件移出方法,如果方法中没有参数,则移出该事件上的所有事件
			// $('div').off();
			//移出div元素上的鼠标单击事件
			$('div').off('click');
		});
	
	</script>
</html>

复合事件

        hover()方法

        描述:hover()方法相当于mouseover与mouseout事件的组合

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>hover方法</title>
		<script src="js/jQuery.js"></script>
		<style>
			div{
				width: 300px;
				height: 300px;
				border: 2px solid #000;
			}
		</style>
	</head>
	<body>
		<div></div>
	</body>
	<script>
		$('div').hover(function(){
			$('div').mouseover(function(){
				$('div').css('background-color','#f00');
			});
		},function(){
			$('div').mouseout(function(){
				$('div').css('background-color','#0f0');
			});
		});
	</script>
</html>

jQuery特效

控制元素显示与隐藏

        show()方法

        描述:show方法控制元素的显示;在show方法中可以设置参数,参数可选,括号内可设置表示速度,默认为“0”,可能值:毫秒(如1000)、slow、normal、fast;括号内可设置show函数执行完之后,要执行的函数

        hide()方法

        描述:hide方法控制元素的隐藏;在hide方法中可以设置参数,参数可选,括号内可设置表示速度,默认为“0”,可能值:毫秒(如1000)、slow、normal、fast;括号内可设置show函数执行完之后,要执行的函数

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>元素的显示与隐藏</title>
		<script src="js/jQuery.js"></script>
		<style>
			div{
				width: 300px;
				height: 300px;
				border: 2px solid #000;
			}
		</style>
	</head>
	<body>
		<button>显示/隐藏</button>
		<div></div>
	</body>
	<script>
		//获取button元素并设置hover方法
		$('button').hover(function(){
			//当鼠标移入button元素时,显示div元素,显示速度3秒
			$('div').show(3000);
		},function(){
			//当鼠标移出button元素时,隐藏divd元素,隐藏速度3秒
			$('div').hide(3000);
		});
	</script>
</html>

元素淡入、淡出

        fadeIn()和fadeOut()方法

        描述:fadeIn()和fadeOut()通过改变元素的透明度实现淡入淡出效果;在fadeIn()和fadeOut()方法中可以设置参数,参数可选,括号内可设置表示速度,默认为“0”,可能值:毫秒(如1000)、slow、normal、fast;括号内可设置show函数执行完之后,要执行的函数

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>元素的淡入与淡出</title>
		<script src="js/jQuery.js"></script>
		<style>
			div{
				width: 300px;
				height: 300px;
				border: 2px solid #000;
			}
		</style>
	</head>
	<body>
		<button>显示/隐藏</button>
		<div></div>
	</body>
	<script>
		//获取button元素并设置hover方法
		$('button').hover(function(){
			//当鼠标移入button元素时,显示div元素,显示速度3秒
			$('div').fadeIn(3000);
		},function(){
			//当鼠标移出button元素时,隐藏divd元素,隐藏速度3秒
			$('div').fadeOut(3000);
		});
	</script>
</html>

改变元素高度显示与隐藏

        slideUp()方法

        描述:slideUp方法可以使元素逐步延伸显示;在slideUp()方法中可以设置参数,参数可选,括号内可设置表示速度,默认为“0”,可能值:毫秒(如1000)、slow、normal、fast;括号内可设置show函数执行完之后,要执行的函数

        slideDown()方法

        描述:slideUp则使元素逐步缩短直至隐藏;在slideDown()方法中可以设置参数,参数可选,括号内可设置表示速度,默认为“0”,可能值:毫秒(如1000)、slow、normal、fast;括号内可设置show函数执行完之后,要执行的函数

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>slideUp和slideDown</title>
		<script src="js/jQuery.js"></script>
		<style>
			div{
				width: 300px;
				height: 300px;
				border: 2px solid #000;
			}
		</style>
	</head>
	<body>
		<button>显示/隐藏</button>
		<div></div>
	</body>
	<script>
		//获取button元素并设置hover方法
		$('button').hover(function(){
			//当鼠标移入button元素时,显示div元素,显示速度3秒
			$('div').slideDown(3000);
		},function(){
			//当鼠标移出button元素时,隐藏divd元素,隐藏速度3秒
			$('div').slideUp(3000);
		});
	</script>
</html>

总结

以上就是本文所写的全部内容,主要写了jQuery鼠标事件和jQuery特效。鼠标事件有单击事件、双击事件、鼠标移入移出事件,绑定和移出事件。jQuery事件实现元素的显示与隐藏,有show和hide方法、faseIn和fadeOut方法、slideUp、slideDown方法实现元素的显示与隐藏效果。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值