首页 技术 正文
技术 2022年11月7日
0 收藏 613 点赞 347 浏览 4525 个字

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/" rel="external nofollow" rel="external nofollow" rel="external nofollow" >百度</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/" rel="external nofollow" rel="external nofollow" rel="external nofollow" >百度</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/" rel="external nofollow" rel="external nofollow" rel="external nofollow" >百度</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 动画函数


    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,149
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,619
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,462
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,237
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,872
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,040