21.4 JQuery_案例_ 广告显示与隐藏
- 需求:
- 1.当页面加载完,3秒后。自动显示广告 ;
- 2.广告显示5秒后,自动消失。
- 分析:
- 1.使用定时器来完成。setTimeout (执行一次定时器);
- 2.分析发现JQuery的显示和隐藏动画效果其实就是控制display;
- 3.使用 show/hide方法来完成广告的显示。
- 示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>广告的自动显示与隐藏</title>
<style>
#content{width:100%;height:500px;background:#999}
</style>
<script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
<script>
$(function () {
setTimeout(adShow,3000);
setTimeout(adHide,8000);
});
function adShow() {
$("#ad").show("slow");
}
function adHide() {
$("#ad").hide("slow");
}
</script>
</head>
<body>
<div>
<div id="ad" style="display: none;">
<img style="width:100%" src="../img/adv.jpg" />
</div>
<div id="content">
正文部分
</div>
</div>
</body>
</html>
21.5 JQuery_案例_抽奖
- 分析:
- 1.给开始按钮绑定单击事件:
- 1.1 定义循环定时器;
- 1.2 切换小相框的src属性
- 定义数组,存放图片资源路径;
- 生成随机数。数组索引 。
- 2.给结束按钮绑定单击事件 :
- 2.1 停止定时器 ;
- 2.2 给大相框设置src属性。
- 示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jquery案例之抽奖</title>
<script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
<script>
var imgs = ["../img/man00.jpg",
"../img/man01.jpg",
"../img/man02.jpg",
"../img/man03.jpg",
"../img/man04.jpg",
"../img/man05.jpg",
"../img/man06.jpg",
];
var startId;
var index;
$(function () {
$("#startID").prop("disabled",false);
$("#stopID").prop("disabled",true);
$("#startID").click(function () {
startId = setInterval(function () {
$("#startID").prop("disabled",true);
$("#stopID").prop("disabled",false);
index = Math.floor(Math.random() * 7);
$("#img1ID").prop("src",imgs[index]);
},20);
});
$("#stopID").click(function () {
$("#startID").prop("disabled",false);
$("#stopID").prop("disabled",true);
clearInterval(startId);
$("#img2ID").prop("src",imgs[index]).hide();
$("#img2ID").show(1000);
});
});
</script>
</head>
<body>
<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"
];
</script>
</body>
</html>
21.6 JQuery_插件机制
- 插件: 增强JQuery的功能。
- 实现方式:
$.fn.extend(object)
- 增强通过Jquery获取的对象的功能 $("#id")
$.extend(object)
- 示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>01-jQuery对象进行方法扩展</title>
<script src="../js/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$.fn.extend({
check:function () {
this.prop("checked",true);
},
uncheck:function () {
this.prop("checked",false);
}
});
$(function () {
$("#btn-check").click(function () {
$("input[type='checkbox']").check();
});
$("#btn-uncheck").click(function () {
$("input[type='checkbox']").uncheck();
});
});
</script>
</head>
<body>
<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">排球
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>01-jQuery对象进行方法扩展</title>
<script src="../js/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$.extend({
max:function (a,b) {
return a>=b ? a:b;
},
min:function (a,b) {
return a<=b ? a:b;
}
});
var max = $.max(4,5);
alert(max);
var min = $.min(2,3);
alert(min);
</script>
</head>
<body>
</body>
</html>