Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

jQuery's advanced tutorials: Animation, traversal, and event binding instances


May 30, 2021 Article blog



Learning goals:

  • Learn how to use jQuery's animation effects
  • Learn how to traverse and bind events
  • Skilled in applying jQuery for related complex operations

What to learn:

First, jQuery advanced

1, animation: three ways to display and hide elements
  • The default display and hiding elements show (speed, smuve, sfn) hide (speed, s/f,easing, smh.com.au) toggle (speed, smuve, sfn))
  • Scrolling to display and hide elements slideDown (speed, smh.com.au, smh.com.au) slideUp (speed, smh.com.au, smh.com.au) slideToggle (speed, smuve, sfn))
  • Fade-out display and hide elements fadeIn (speed, smule, sfn)) fadeOut (speed, smule, sfn)) fadeToggle (speed, smuve, sfn))
  • Parameter speed: speed, three predefined values ("slow," "normal," "fast") or millisecond values that represent the duration of the animation (e.g.: 1000) ease: used to specify the switch effect, the default is "swing", the available parameter "linear swing": the animation execution effect is "slow first, fast in the middle, slow in the middle"linear: the speed at which the animation is executed is a uniform fn: a function executed when the animation is complete, Each element is executed once
2, traversal
  • js traversal mode for (initialization value; C ycle end condition; step)
  • jq traversal way jq object.callback$.each (object, callback)) for... Versions provided after of jQuery 3.0
3, event binding
  • jQuery standard binding method jq object. E vent method (callback function); I f the event method is called and the callback function is not passed, the browser default behavior form object.submit() is triggered; Have the form submitted
  • on-binding event/off unbundling event jq object.on (event, callback function); j q object.off (event); If you do not pass the event name, untie all events
  • Event switching: togglejq object.toggle (fn1, fn2...); Click multiple times to execute the passed function in turn
4, the case
  • Ad display and hide requirements When the page is loaded, after 3 seconds, automatic display ad display 5 seconds later, automatic disappearance analysis uses a timer to complete setTimeOut() to perform a timer jQuery display and hidden animation effect is actually to control the display of ads using show/hide-related methods
  • Abstract demand click start, small photo frame quick switch click stop, small photo frame stop switch analysis to start button binding click event definition loop timer switch small picture box src property definition array, store picture resource path generate random number, array index to end button binding click event stop timer to large picture frame set src property
5, plug-in: enhance jQuery function
  • How to implement $.fn.extend (object); E nhances the tiling of getting objects through jQuery $ ("#id")$.extend (object); Enhances the functionality of the jQuery object itself $/jQuery

Learning output:

1, animation
<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
   <title>Insert title here</title>
   <script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
   <script>
       // 隐藏div
       function hideFn() {
           /*$("#showDiv").hide("slow","swing",function () {
              // alert("隐藏了。。。");
          })*/
           // 默认方式
           // $("#showDiv").hide("slow","swing");
           // 滑动方式
           // $("#showDiv").slideDown("slow","swing");
           // 淡入淡出方式
           $("#showDiv").fadeOut("slow","swing");
      }

       // 显示div
       function showFn() {
           /*$("#showDiv").show("slow","linear",function () {
              // alert("显示了。。。");
          })*/
           // 默认方式
           // $("#showDiv").show("slow","swing");
           // 滑动方式
           // $("#showDiv").slideUp("slow","swing");
           // 淡入淡出方式
           $("#showDiv").fadeIn("slow","swing");
      }

       // 切换显示隐藏div
       function toggleFn() {
           /*$("#showDiv").toggle("slow","swing",function () {
              // alert("切换显示隐藏了。。。");
          })*/
           // 默认方式
           // $("#showDiv").toggle("slow","swing");
           // 滑动方式
           // $("#showDiv").slideToggle("slow","swing");
           // 淡入淡出方式
           $("#showDiv").fadeToggle("slow","swing");
      }
   </script>
</head>
<body>
<input type="button" value="点击按钮隐藏div" onclick="hideFn()">
<input type="button" value="点击按钮显示div" onclick="showFn()">
<input type="button" value="点击按钮切换div显示和隐藏" onclick="toggleFn()">

<div id="showDiv" style="width:300px;height:300px;background:pink">
  div显示和隐藏
</div>
</body>
</html>
2, traversal
<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
   <title></title>
   <script src="../js/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script>
   <script type="text/javascript">

       $(function () {
           // 获取所有ul下面的li
           var citys = $("#city li");
           // 遍历li
           // js遍历方式
           for (var i = 0;i < citys.length; i++){
               // 获取内容
               alert(i+":"+citys[i].innerHTML);
          }

           // jq遍历方式
           citys.each(function (index,element) {
               // 获取li对象,直接用this
               // alert(this.innerHTML);
               // alert($(this).html());
               // 获取li对象,在回调函数中定义参数index(索引) element(元素对象)
               // alert(index+":"+element.innerHTML);
               // alert(index+":"+$(element).html());

               // 判断如果是上海,结束循环
               if ("上海"==$(this).html()){
                   // 如果当前回调函数返回值是false,终止循环
                   // 如果当前回调函数返回值是true,终止本次循环,继续下次循环
                   return false;
                   // return true;
              }
               alert(index+":"+$(element).html());
          });

           $.each(citys,function () {
               alert($(this).html());
          });

           for (li of citys){
               alert($(li).html());
          }

      });

   </script>
</head>
<body>
<ul id="city">
   <li>北京</li>
   <li>上海</li>
   <li>天津</li>
   <li>重庆</li>
</ul>
</body>
</html>
3, event binding and unbundling
<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
   <title></title>
   <script src="../js/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script>
   <script type="text/javascript">
       $(function () {
           // 获取name对象,绑定click事件
           // $("#name").click(function () {
           //     alert("我被点击了!");
           // });

           // 给name对象绑定鼠标移动元素之上事件与鼠标移除事件
           // $("#name").mouseover(function () {
           //     alert("鼠标来了!");
           // });
           // $("#name").mouseleave(function () {
           //     alert("鼠标走了!");
           // });

           // 简化操作,链式编程
           // $("#name").mouseover(function () {
           //     alert("鼠标来了");
           // }).mouseleave(function () {
           //     alert("鼠标走了");
           // })


           $("#name").focus();// 让文本输入框获得焦点
           // 表单对象.submit()   // 让表单提交
      })

   </script>
</head>
<body>
<input id="name" type="text" value="绑定点击事件">
</body>
</html>

<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
   <title></title>
   <script src="../js/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script>
   <script type="text/javascript">
       $(function () {
           // 使用on绑定事件
           $("#btn").on("click",function () {
               alert("我被点击了!");
          });

           // 使用off解绑事件
           $("#btn2").click(function () {
               // 解除btn按钮的click绑定事件
               $("#btn").off("click");
          });
      });



   </script>
</head>
<body>
<input id="btn" type="button" value="使用on绑定点击事件">
<input id="btn2" type="button" value="使用off解绑点击事件">
</body>
</html>

<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
   <title></title>
   <script src="../js/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script>
   <script src="../js/jquery-migrate-1.0.0.js" type="text/javascript" charset="utf-8"></script>
   <script type="text/javascript">
       $(function () {
           $("#btn").toggle(function () {
               // 改变背景色为绿色
               $("#myDiv").css("backgroundColor","green");
          },function () {
               // 改变背景色为黄色
               $("#myDiv").css("backgroundColor","yellow");
          })
      })
   </script>
</head>
<body>

   <input id="btn" type="button" value="事件切换">
   <div id="myDiv" style="width:300px;height:300px;background:pink">
      点击按钮变成绿色,再次点击黄色
   </div>
</body>
</html>
4, Case 1: Advertising display and hiding
<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
   <title>广告的自动显示与隐藏</title>
   <style>
       #content{width:100%;height:500px;background:#999}
   </style>

   <!--引入jquery-->
   <script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
   <script>
       $(function () {
           // 定义定时器,3秒之后显示广告
           setTimeout(adShow,3000);
           // 定义定时器,5秒之后隐藏广告
           setTimeout(adHide,8000);
      });
       // 显示广告
       function adShow() {
           $("#ad").show("slow");
      }
       // 隐藏广告
       function adHide() {
           $("#ad").hide("slow");
      }
   </script>
</head>
<body>
<!-- 整体的DIV -->
<div>
   <!-- 广告DIV -->
   <div id="ad" style="display: none;">
       <img style="width:100%" src="../img/adv.jpg" />
   </div>

   <!-- 下方正文部分 -->
   <div id="content">
      正文部分
   </div>
</div>
</body>
</html>
5, Case 2: Sweepstakes
<!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 img = ["../img/man00.jpg",
           "../img/man01.jpg",
           "../img/man02.jpg",
           "../img/man03.jpg",
           "../img/man04.jpg",
           "../img/man05.jpg",
           "../img/man06.jpg"];
       $(function () {
           $("#startID").prop("disabled",false);
           $("#stopID").prop("disabled",true);

           var startId;
           var index;
           $("#startID").click(function () {
               $("#startID").prop("disabled",true);
               $("#stopID").prop("disabled",false);
               startId = setInterval(function () {
                   index = Math.floor(Math.random()*7);
                   $("#img1ID").prop("src",img[index])
              },20);
          });
           $("#stopID").click(function () {
               $("#startID").prop("disabled",false);
               $("#stopID").prop("disabled",true);
               clearInterval(startId);
               $("#img2ID").prop("src",img[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>
6, 01-jQuery object for method extension
<!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">
       // 使用jQuery插件,给jq添加两个方法,check()选中所有复选框,uncheck()取消所有选中
       // 定义jQuery对象的插件
       $.fn.extend({
           // 定义一个check方法,所有的jq对象都可以调用该方法
           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>
7, 02-jQuery global method extension
<!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">
       //对全局方法扩展2个方法,扩展min方法:求2个值的最小值;扩展max方法:求2个值最大值
       $.extend({
           max:function (a,b) {
               return a >= b ? a : b;
          },
           min:function (a,b) {
               return a <= b ? a : b;
          }
      });

       var max = $.max(2,3);
       alert(max);
       var min = $.min(1,8);
       alert(min);
   </script>
</head>
<body>
</body>
</html>