js插件封装方法(一)

利用原型和原型链实现的封装方法,比较好用,以下是封装的提示框小插件(显示几秒后自动消失)

/**
 * 提示框组件
 * created by lixn
 * 2019/10/31
 */

tipsPop = (function(){

    var obj = function (options){
        //处理参数
        this._setPara(options);	
        // 入口函数
        this.init();
    };
    obj.prototype = {
        //修正指针
        constructor: obj,

        //设置参数
        _setPara: function (option){
            this.el = document.querySelector(option.el) || 'body';//节点对象 //必传参数 css选择器
            this.creatId = 'tips'+ (new Date()).getTime();
            this.left = option.left || null;
            this.top = option.top || null;
            this.text = option.text || '操作成功!';  //提示语
            this.color = option.color || '#47c7ea';  //主题颜色
            this.time = option.time || '1000';  //延迟关闭得时间
            //回调函数
            this.callback = option.callback || this.callback;
        },
        // 生成Dom元素
        _creatDom: function(){
            var self = this;
            this.creatDom = '<div id="'+this.creatId+'_pop" class="pop"></div><div id='+this.creatId+' class="tips-pop" style="left:'+this.left+';top:'+this.top+'">\
            <div class="tips-head" style="background-color:'+this.color+'">提示<span id="tips_close" class="tips-close">&times;</span></div>\
            <div class="tips-body">'+this.text+'</div>\
            </div>';

            $(this.el).append(this.creatDom);
            // 点击关闭
            $('#tips_close').click(function(){
                self._removeDom();
            })
        },
        _removeDom: function(){
            var self = this;
            $('#'+self.creatId).slideUp();
            $('#'+self.creatId+'_pop').remove();
            setTimeout(function(){
                $('#'+self.creatId).remove();
            },300)
        },
        callback: function(){
           
        },
        //入口函数
        init: function (){
            var self = this;
            // 创建节点
            this._creatDom();
            // 关闭弹窗
            setTimeout(function(){
                self._removeDom();
            },this.time)
            
        }
    };
    return obj;
})();

使用方法:

var tips = new tipsPop({text: '新增成功!'});

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值