小微型库(5.类名添加addClass和移除removeClass,判断有无类名hanClass及toggle功能)

类名的添加addClass
//测试
//html文档中
    <div class="box4 box1">1111</div>
    <div class=" box2 box3 ">2222</div>
//script中
    $("div").addClass("box1 box2 box3 box4");
复制代码
类名的移出removeClass
//测试
//html文档中
    <div class="box1 box2 box3 box4 box5">1111</div>
    <div class=" box1 box1 box2 box2 box3 ">2222</div>
//script中
$("div").addClass("box1 box2");
复制代码

//在原型上继续添加
Liang.prototype = {
    constructor : Liang,
    init : function(){}, //(1)
    html : function(){}, //(2)
    on : function(){}, //(3)
    off : function(){}, //(3)
    text : function(){}, //(4)
    val : function(){}, //(4)
    eq :  function(){}, //(4)
    //类名添加addClass
    addClass : function(cName){
        if(typeof cName === "string"){ 
            //要添加的类名的数组
            var arrCName = cName.trim().split(/\s+/); 
            if( arrCName.toString() === "") return this; //说明输入的是空字符串
            Liang.each(this, function(v){
                //得到类名集合的数组,并与添加的类名数组拼接
                var arrEleClass = v.className.trim().split(/\s+/).concat(arrCName);
                //取出
                for(var i = 0; i < arrEleClass.length; i++){
                    for(var j = arrEleClass.length - 1; j > i; j--){
                        if(arrEleClass[i] === arrEleClass[j]){
                          arrEleClass.splice(j, 1);
                        }
                    }
                }
                v.className = arrEleClass.join(" ");
            });
        }
        return this;
    },
    //类名移出removeClass
    removeClass : function(cName){
        //如果不写,表示移出全部 $("div").removeClass()
        if(typeof cName === "undefined"){
            Liang.each(this, function(v){
                v.className = "";
            });
            return this;
        }else if(typeof cName === "string"){
            //要移出的类名数组
            var arrCName = cName.trim().split(/\s+/);
            if(arrCName.toString() === "") return;
            //移出对应的类名
            Liang.each(this, function(v){
                //元素自身已经存在的类名
                var arrEleClass = v.className.trim().split(/\s+/);
                for(var i = 0, len = arrCName.length; i < len; i++){
                    //对元素本身已经存在的类名数组遍历,删除符合条件的类名
                    for(var j = arrEleClass.length - 1; j >= 0; j--){
                        if(arrCName[i] === arrEleClass[j]){
                            arrEleClass.splice(j, 1);
                        }
                    }
                }
                v.className = arrEleClass.join(" ");
            });
        }
        return this;
    },
    //判断类名是否存在,返回布尔值
    hasClass : function(cName){
        var isClass = false;
        Liang.each(this,function(v){
            cName = cName.trim();
            var reg = new RegExp("\\b" + cName + "\\b");
            if(reg.test(v.className)){
                isClass = true; //有类名,设置为true
                return false; //结束当前的遍历
            }
        });
        return isClass; //返回布尔值确定有无类名
    },
    //判断类名是否存在,存在就删除,不存在就添加
    toggleClass : function(cName){
        Liang.each(this, function(v){
            var that = Liang(v); //将单个节点对象包装成jq对象
            if(that.hasClass(cName)){ //有类名,删除
                that.removeClass(cName);
            }else{ //无类名,添加
                that.addClass(cName);
            }
        });
    },
    //利用静态方法each实现
    each :function(fn){
        Liang.each(this, function(v, i, arr){
            var bool = fn.call(v, v, i, arr);
            if(bool !== "undefined"){
                return;
            }
        });
    }
}
复制代码

转载于:https://juejin.im/post/5be126f8518825514e2d3886

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里给你提供一种使用自定义类名和animate.css动画给一张图片添加入场动画的示例。使用animate.css可以方便地实现CSS动画效果,具体可以根据需求自定义。 首先,需要在HTML文件中引入animate.css的CDN链接: ```html <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" /> ``` 然后,HTML代码如下: ```html <div class="image-wrapper animate__animated"> <img src="image.jpg" alt="图片"> </div> ``` CSS代码如下: ```css .image-wrapper { position: relative; display: inline-block; } .image-wrapper.animate__animated { animation-duration: 1s; /* 设置动画持续时间 */ animation-delay: 0.5s; /* 设置动画延迟时间 */ animation-fill-mode: both; /* 设置动画结束后保持最后状态 */ animation-name: slideInLeft; /* 设置动画名称 */ } @keyframes slideInLeft { from { transform: translateX(-100%); } to { transform: translateX(0); } } .image-wrapper img { display: block; max-width: 100%; height: auto; } ``` 这段代码使用了animate.css中的slideInLeft动画效果,通过给`.image-wrapper`添加类名`.animate__animated`来触发动画效果。其中,`.image-wrapper.animate__animated`使用了CSS3的动画属性animation,通过设置animation-duration、animation-delay、animation-fill-mode、animation-name属性来定义动画效果。同时,利用@keyframes定义了从左侧平移进入的动画效果。最后,`.image-wrapper img`设置了图片的基本样式,其中max-width: 100%可以确保图片的宽度不会超出其容器的宽度。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值