封装jquery

1.delay方法
delay()//动作延迟
2.each方法
each(索引,数组元素)优点:可以避免遍历
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        ul{
            list-style: none;
        }
        li{
            width: 200px;
            height: 200px;
            background-color: #FF6600;
            margin: 0 20px;
            float: left;
            text-align: center;
            line-height: 200px;
            font-size: 22px;
        }
    </style>
</head>
<body>
<ul>
    <li>哈哈哈</li>
    <li>呵呵哒</li>
    <li>略略略</li>
</ul>
<script src="jquery-1.12.4.js"></script>
<script>
    //设置li的透明度,从左往右依次递减
    //rgba():背景颜色额透明度  opacity:设置盒子的透明度
    $(function () {
        var $li=$("li");
       /* for (var i=0;i<$li.length;i++) {
            $li.eq(i).css("opacity",(i+1)/10)
        }*/
       //回调函数中的参数;①索引②数组元素(是一个js对象)
       $li.each(function (index,element) {
           $(element).css("opacity",(index+1)/10);
       })
    })
</script>
</body>
</html>

3.$命名冲突
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script src="jquery-1.12.4.js"></script>
<script>
    console.log($);
    //jquery函数
    var cc=$.noConflict();//将$释放,cc代替$释放前的功能
    console.log($);
    console.log(cc);
    cc(function () {

    });
</script>
</body>
</html>

4.懒加载lazyload
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        div{
            height: 4000px;
        }
    </style>
</head>
<body>
<div></div>
<img class="lazy" data-original="images/1.jpg" width="300" height="300">
<script src="jquery-1.12.4.js"></script>
<script src="js/jquery.lazyload.min.js"></script>
<script>
    $("img.lazy").lazyload();
</script>
</body>
</html>

5.插件的实质
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script src="jquery-1.12.4.js"></script>
<script>
    //jquery插件的实质:给jquery的 原型增加方法
    jQuery.prototype.sayHi=function () {
        console.log("哈哈哈");
    }
    $("div").sayHi();
</script>
</body>
</html>

6.封装
背景颜色
$.fn.setBgColor=function (color) {
    //this是jquery对象
    this.css("backgroundColor",color)
}

手风琴
$.fn.accordion=function (colors,width) {
    colors=colors || [];
    width=width || 100;
    var $li=this.find("li");
    var boxWidth=this.width();
    var maxWidth=boxWidth-width*($li.length-1)
    var evgWidth=boxWidth/$li.length ;
    $li.each(function (i,element) {
        $(element).css("backgroundColor",colors[i]);
    })
    $li.on("mouseenter",function () {
        $(this).stop().animate({width:maxWidth}).siblings().stop().animate({width:width});
    });
    $li.on("mouseleave",function () {
        $li.stop().animate({width:evgWidth});
    })
    return this;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值