jquery接触初级-----juqery 动画函数

时间:2023-03-09 18:42:04
jquery接触初级-----juqery 动画函数

1. window.onload(), 一次只能保存对一个函数的引用;如果多次调用,他会自动用后面的函数覆盖前面的函数

2.$(document).ready(); 会在现有行为上追加新的行为,这些函数行为会按照注册的顺序进行依次执行,避免了window.onload()的覆盖行为;

$(document).ready()  有三种写法:

2.1  $(document).ready(function(){});

2.2 $(function(){});

2.3 $().ready(function(){})

例子:要求:写一个事件:鼠标移入H3 标签范围,和这相关的内容显示,鼠标移出,内容消失

代码:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<title>Document</title>
<style>
div {
width: 500px;
background: #ccc;
}
h3 {
text-align: center;
}
ul {
margin: 0;
padding: 10px;
display: none;
}
</style>
<script>
$(document).ready(function() {
$("h3").bind("mouseover", function() {
$(this).next().show();
}).bind("mouseout", function() {
$(this).next().hide();
})
})
</script>
</head>
<body>
<div>
<h3>这里是标题</h3>
<ul>这里是内容,我们今天说的是关于jquery的事情,将要介绍与标题相关的显示和影藏事件,将要介绍与标题相关的显示和影藏事件,将要介绍与标题相关的显示和影藏事件,将要介绍与标题相关的显示和影藏事件,将要介绍与标题相关的显示和影藏事件,将要介绍与标题相关的显示和影藏事件,将要介绍与标题相关的显示和影藏事件,将要介绍与标题相关的显示和影藏事件,将要介绍与标题相关的显示和影藏事件,将要介绍与标题相关的显示和影藏事件</ul>
</div>
</body>
</html>

运行结果:

jquery接触初级-----juqery 动画函数

可以使用hover()事件来改写:效果还是一样的

 <script>
$(document).ready(function() {
$("h3").hover(function() {
$(this).next().show();
}, function() {
$(this).next().hide();
})
});
</script>

3.1 事件对象的属性:event.type

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<title>Document</title>
<script>
$(function() {
$('.type').click(function(event) {
alert(event.type);
});
})
</script>
</head>
<body>
<div class="box">
<input type="button" value="点击一下" class="type">
</div>
</body>
</html>

运行结果:点击,就会弹出事件的type值

jquery接触初级-----juqery 动画函数

3.2 event.preventDefault()   :阻止事件的默认行为,例如a标签的跳转;

3.3 event.stopPropagation() :阻止事件冒泡

3.4 event.target  目标事件的元素

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<title>Document</title>
<script>
$(function() {
$("a").click(function(event) {
console.log(event.target.href);
event.preventDefault();
});
})
</script>
</head>
<body>
<div class="box">
<a href="https://www.baidu.com/">百度</a>
</div>
</body>
</html>

运行结果:点击后,阻止默认行为,即阻止跳转,并且输出 目标元素a的href 属性值

jquery接触初级-----juqery 动画函数

3.5 event.pageX  , event.pageY 获取光标相对于浏览器的X坐标和Y坐标  :

 <script>
$(function() {
$(document).click(function(event) {
var Op = $("<p></p>");
Op.html("当前鼠标的位置是:" + event.pageX + "," + event.pageY);
$(".box").append(Op);
event.preventDefault();
});
})
</script>

jquery接触初级-----juqery 动画函数

3.6  event.which;  获取鼠标的左键,中间键,和右键值 ,分别对应1.2.3

  $(function() {
$(document).click(function(event) {
console.log(event.which)
event.preventDefault();
});
})

试了一下:左键有用,单击左键,会输出1

4. jquery 中的动画:

4.1  show()方法和hide()方法  ,实现对元素的显示和隐藏

4.2 fadeIn() 和fadeOut()  : 实现元素不透明度的改变,fadeIn()从无到有,fadeOut()从有到无

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<title>Document</title>
<style>
div.cc {
background: #ccc;
display: none;
}
</style>
<script>
$(function() {
$("a").on("click", function(e) {
e.preventDefault();
if ($("div.cc").is(":hidden")) {
$(this).parent().next().fadeIn();
} else {
$(this).parent().next().fadeOut();
}
});
});
</script>
</head>
<body>
<div class="box">
<a href="https://www.baidu.com/">百度</a>
</div>
<div class="cc">改变不透明度的区块</div>
</body>
</html>

jquery接触初级-----juqery 动画函数

4.3  slideUp(),slideDown()  : 改变高度的属性。slideUp(),高度从0到最大;slideDown(),相反,高度从大到0;

4.4 animate();可以改变所有属性,是一个自定义动画;

$(selector).animate(styles,speed,easing,callback)

分别代表样式,速度,动画效果,回调函数

自定义动画效果:改变div的宽,高,然后加上边框
 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<title>Document</title>
<style>
div.cc {
background: #ccc;
width: 200px;
height: 200px;
}
</style>
<script>
$(function() {
$("a").on("click", function(e) {
e.preventDefault();
$("div.cc").animate({
"width": "400px"
}, 2000).animate({
"height": "300px",
"color": "red"
}, 3000, function() {
$("div.cc").css("border", "3px solid blue")
});
});
});
</script>
</head>
<body>
<div class="box">
<a href="https://www.baidu.com/">百度</a>
</div>
<div class="cc">改变不透明度的区块</div>
</body>
</html>

运行效果:

jquery接触初级-----juqery 动画函数

4.5  stop()方法,可以停止动画队列

语法:stop([clearqueue],[gotoend]);

参数clearqueue和gotoend 都是可选参数,为Boolean 值

clearqueue:  是否要清空未执行的动画队列,

gotoend:是否直接将正在执行的动画跳转到末尾状态。

stop()方法在做轮播图的时候,会用到,具体参考jquery 轮播制作页面

动画函数总结如下:

jquery接触初级-----juqery 动画函数