JQuery高级
1.动画
三种方式显示和隐藏元素
A.默认显示和隐藏方式
参数:
speed:动画的速度。三个预定义的值("slow
","normal
", “fast
”)或表示动画时长的毫秒数值(如:1000
)
- 显现
show([speed,[easing],[fn]])
$("#showDiv").show("fast",function () {alert("show Div")})
- 隐藏
hide([speed,[easing],[fn]])
1.默认隐藏
$("#showDiv").hide("fast",function () {})
- 切换显现和隐藏
toggle([speed],[easing],[fn])
$("#showDiv").toggle("fast",function () {alert("toggle Div")})
B.滑动显示和隐藏方式
- 滑动出现:
slideDown([speed],[easing],[fn])
$("#showDiv").slideDown(500);
- 滑动收起:
slideUp([speed,[easing],[fn]])
$("#showDiv").slideUp(500);
- 滑动收起出现切换:
slideToggle([speed],[easing],[fn])
$("#showDiv").slideToggle(500);
C.淡入淡出显示和隐士方式
- 淡入:
fadeIn([speed],[easing],[fn])
$("#showDiv").fadeOut(500);
- 淡出:
fadeOut([speed],[easing],[fn])
$("#showDiv").fadeIn(500);
- 淡入淡出切换:
fadeToggle([speed,[easing],[fn]])
$("#showDiv").fadeToggle(500);
2.遍历
A.JS方式的遍历
- for(初始化值;循环结束条件;步长)
$(function () {
var cities = $("#city li");
for (var i = 0; i < cities.length; i++) {
alert(i+" : "+cities[i].innerHTML)
}
})
B.JQuery的遍历方式
样式信息:
<ul id="city">
<li>北京</li>
<li>上海</li>
<li>天津</li>
<li>重庆</li>
</ul>
jq对象.each(callback)
callback
类型 :function()函数
$(function () {
var cities = $("#city li");
cities.each(function (index,element) {
if ($(element).html()=="上海"){
return false;
}
alert(index+":"+$(element).html())
})
})
$.each(object, [callback])
jQuery(function () {
var cities = $("#city li");
cities.each(function (index,element) {
if ($(element).html()=="上海"){
return false;
}
alert(index+":"+$(element).html())
})
})
for..of
:jquery 3.0
版本之后提供的方式
$(function () {
var cities = $("#city li");
for (li of cities){
alert($(li).html())
}
})
3.事件绑定
A. jquery标准的绑定方式
jq对象.事件方法(回调函数)
;
注:如果调用事件方法,不传递回调函数
,则会触发浏览器默认行为
。
表单对象.submit();//让表单提交
<input id="name" type="text" placeholder="绑定点击事件">
alert("我要获得焦点")
//自动会把焦点放在文本框中
$("#name").focus();
B.on绑定事件/off解除绑定
<input id="btn" type="button" value="使用on绑定点击事件">
<input id="btn2" type="button" value="使用off解绑点击事件">
$(function () {
//1.使用on绑定点击事件 click
$("#btn").on("click",function () {
alert("我被点击了...")
})
//2.使用off解绑点击事件
$("#btn2").click(function () {
$("#btn").off("click");
})
})
C.事件切换:toggle
jq对象.toggle(fn1,fn2...)
当单击jq对象对应的组件后,会执行fn1.第二次点击会执行fn2…
<input id="btn" type="button" value="事件切换">
<div id="myDiv" style="width:300px;height:300px;background:pink">
点击按钮变成绿色,再次点击红色,再次点击变成红色
</div>
<script type="text/javascript">
$(function () {
//事件切换
$("#btn").toggle(function () {
$("#myDiv").css("backgroundColor","green")
},function () {
$("#myDiv").css("backgroundColor","pink")
},function () {
$("#myDiv").css("backgroundColor","red")
})
})
</script>
注意:1.9版本 .toggle()
方法删除,jQuery Migrate
(迁移)插件可以恢复此功能。
<script src="../js/jquery-migrate-1.0.0.js" type="text/javascript" charset="utf-8"></script>
4.案例
A.广告的自动显示与隐藏案例
需求:
1. 当页面加载完,3秒后。自动显示广告
2. 广告显示5秒后,自动消失。
分析:
1. 使用定时器来完成。setTimeout (执行一次定时器)
2. 分析发现JQuery的显示和隐藏动画效果其实就是控制display
3. 使用 show/hide方法来完成广告的显示
4. 页面加载完成之后,定义定时器,调用这两个方法
<!-- 整体的DIV -->
<div>
<!-- 广告DIV -->
<div id="ad" style="display: none;">
<img style="width:100%" src="../img/adv.jpg" />
</div>
<!-- 下方正文部分 -->
<div id="content">
正文部分
</div>
</div>
<script>
//步入函数入口
$(function(){
//定义定时器
setTimeout(addShow,3000);
setTimeout(moveShow,3000+5000);
})
function addShow() {
//1.显示广告,获取广告div,调用显示方法方法
$("#ad").show("slow");
}
function moveShow() {
//2.隐藏广告
$("#ad").hide("slow");
}
</script>
B.抽奖案例
分析:
1. 给开始按钮绑定单击事件
1.1 定义循环定时器
1.2 切换小相框的src属性
* 定义数组,存放图片资源路径
* 生成随机数。数组索引
2. 给结束按钮绑定单击事件
1.1 停止定时器
1.2 给大相框设置src属性
<!-- 小像框 -->
<div style="border-style:dotted;width:160px;height:100px">
<img id="img1ID" src="../img/man00.jpg" style="width:160px;height:100px"/>
</div>
<!-- 大像框 -->
<div
style="border-style:double;width:800px;height:500px;position:absolute;left:500px;top:10px">
<img id="img2ID" src="../img/man00.jpg" width="800px" height="500px"/>
</div>
<!-- 开始按钮 -->
<input
id="startID"
type="button"
value="点击开始"
style="width:150px;height:150px;font-size:22px"
onclick="imgStart()">
<!-- 停止按钮 -->
<input
id="stopID"
type="button"
value="点击停止"
style="width:150px;height:150px;font-size:22px"
onclick="imgStop()">
<script language='javascript' type='text/javascript'>
//准备一个一维数组,装用户的像片路径
var imgs = [
"../img/man00.jpg",
"../img/man01.jpg",
"../img/man02.jpg",
"../img/man03.jpg",
"../img/man04.jpg",
"../img/man05.jpg",
"../img/man06.jpg"
];
//1.开始按钮
var start;
var index;
//先默认初始化关闭按钮不能使用
$(function () {
$("#startID").prop("disabled",false)
$("#stopID").prop("disabled",true)
})
function imgStart() {
$("#startID").prop("disabled",true)
$("#stopID").prop("disabled",false)
//1.1定义循环定时器
start = setInterval(function () {
//1.2生成随机角标 0-6
index = Math.floor(Math.random() * 7);
//1.3设置相框的src属性
$("#img1ID").prop("src",imgs[index])
},20);
}
//2.结束按钮
function imgStop() {
$("#startID").prop("disabled",false)
$("#stopID").prop("disabled",true)
//2.1结束定时器
clearInterval(start)
//2.2给大相框设置src属性
$("#img2ID").prop("src",imgs[index]).hide().show(2000)
}
</script>
5.插件增强JQuery的功能
A.增强通过Jquery获取的对象的功能 $("#id")
$.fn.extend(object)
<input id="btn-check" type="button" value="点击选中复选框" onclick="checkFn()">
<input id="btn-uncheck" type="button" value="点击取消复选框选中" onclick="uncheckFn()">
<br/>
<input type="checkbox" value="football">足球
<input type="checkbox" value="basketball">篮球
<input type="checkbox" value="volleyball">排球
$.fn.extend({
check:function () {
//1.让复选框选中
this.prop("checked",true);
},
uncheck:function () {
//2.让复选框不选中
this.prop("checked",false);
}
})
$(function () {
$("#btn-check").click(function () {
//获取复选框对象
$("input[type=checkbox]").check();
})
$("#btn-uncheck").click(function () {
//获取复选框对象
$("input[type=checkbox]").uncheck();
})
})
B.增强JQeury对象自身的功能 $/jQuery
- 语法:
$.extend(object)
<script type="text/javascript">
//对全局方法扩展2个方法,扩展min方法:求2个值的最小值;扩展max方法:求2个值最大值
$.extend({
max:function (a, b) {
return a >= b?a : b;
},
min:function (a, b) {
return a <= b?a : b;
}
})
alert($.max(2,3))
alert($.min(2,3))
</script>
转载:https://blog.csdn.net/weixin_44954070/article/details/105254817
查看评论