JQ封装简易弹框插件

使用JQ封装简易弹框插件

自己瞎写,用jq写了一个弹框插件。代码不规范,写法上也有诸多问题,望不吝赐教

首先对jquery扩展一个myplug方法:

; (function ($) {
  $.fn.myPlug = function (opt) {

  }

})(jQuery);

设置插件的默认属性:

var defOpt = {   // 默认参数
    bg: 'white', // 背景颜色
    title: '标题',
    content: '内容',
    vanish: false  // 点击空白处是否消失
  }

如果用户设置了自定义属性,则覆盖它的默认属性,有两种写法,如下:

$.fn.myPlug = function (opt) {
    opt = opt || {}
    // 用自定义属性覆盖默认属性
    // 写法1:
    // for (var d in defOpt) {
    //   if (opt.hasOwnProperty(d))
    //     continue
    //   opt[d] = defOpt[d];
    // }

    // 写法2:
    opts = $.extend({}, defOpt, opt);  // 用自定义属性覆盖默认属性
  }

myPlug插件里面可以有多个方法,为了收集这些方法,可以新建一个对象来保存:

  function AlertObj(elm, opt) {  // elm:元素  opt:配置
    this.options = opt
    this.$elm = $(elm)
  }
  var that = new AlertObj(this, opts)

开始往AlertObj里面添加方法,我只写了一个show()方法,把弹框出现和消失全写在里面:

show: function () {
      var o = this.options
      var dom = '<div class="alert"><div class="alert-in"><div class="alert-close" id="close">×</div><div class="alert-title">' + o.title + '</div><div class="alert-content">' + o.content + '</div></div></div>'
      $('body').append(dom)

      // 弹框样式
      var w = $(window).width()
      var h = $(window).height()
      $('.alert').css({
        width: w + 'px',
        height: h + 'px',
        background: 'rgba(0,0,0,0.5)',
        'padding-top': '20%',
        'box-sizing': 'border-box',
        position: 'absolute',
        top: 0,
        left: 0,
        'z-index': 1000
      })
      $('.alert-close').css({
        'position': 'absolute',
        'top': '-10px',
        'right': '-10px',
        'width': '30px',
        'height': '30px',
        'border-radius': '50%',
        'font-size': '25px',
        'text-align': 'center',
        'line-height': '30px',
        'border': '1px solid #333',
        'background': 'white'
      })
      $('.alert-in').css({
        'border': '1px solid #333',
        'border-radius': '5px',
        'margin-left': '30%',
        "background": 'white',
        'width': '40%',
        'position': 'relative',
      })
      $('.alert-title').css({
        'font-size': '17px',
        'border-bottom': '1px dotted #666',
        'padding': '10px',
        title: '标题'
      })
      $('.alert-content').css({
        'padding': '20px',
        title: '标题'
      })
      $('.alert').css({
        display: 'block'
      })
      $('#close').click(function () {
        $('.alert').remove()
      })
      // 点击空白黑色区域清除弹框
      $(document).click(function (e) {
        if (($(e.target).attr('class') == 'alert')) {
          if (o.vanish) {
            $('.alert').remove()
          }
        }
      })
    }

在html里面:

$('button').click(function () {
      $('#test').myPlug({
        title: 'hah',
        content: 'hah',
        vanish: true
      })
    })

<button id="test">alert</button>

完整代码如下:

; (function ($) {
  var defOpt = {   // 默认参数
    bg: 'white', // 背景颜色
    title: '标题',
    content: '内容',
    vanish: false  // 点击空白处是否消失
  }
  $.fn.myPlug = function (opt) {
    opt = opt || {}
    // 用自定义属性覆盖默认属性
    // 写法1:
    // for (var d in defOpt) {
    //   if (opt.hasOwnProperty(d))
    //     continue
    //   opt[d] = defOpt[d];
    // }

    // 写法2:
    opts = $.extend({}, defOpt, opt);  // 用自定义属性覆盖默认属性

    var that = new AlertObj(this, opts)
    that.show()
  }

  function AlertObj(elm, opt) {  // elm:元素  opt:配置
    this.options = opt
    this.$elm = $(elm)
  }

  AlertObj.prototype = {
    show: function () {
      var o = this.options
      var dom = '<div class="alert"><div class="alert-in"><div class="alert-close" id="close">×</div><div class="alert-title">' + o.title + '</div><div class="alert-content">' + o.content + '</div></div></div>'
      $('body').append(dom)

      // 弹框样式
      var w = $(window).width()
      var h = $(window).height()
      $('.alert').css({
        width: w + 'px',
        height: h + 'px',
        background: 'rgba(0,0,0,0.5)',
        'padding-top': '20%',
        'box-sizing': 'border-box',
        position: 'absolute',
        top: 0,
        left: 0,
        'z-index': 1000
      })
      $('.alert-close').css({
        'position': 'absolute',
        'top': '-10px',
        'right': '-10px',
        'width': '30px',
        'height': '30px',
        'border-radius': '50%',
        'font-size': '25px',
        'text-align': 'center',
        'line-height': '30px',
        'border': '1px solid #333',
        'background': 'white'
      })
      $('.alert-in').css({
        'border': '1px solid #333',
        'border-radius': '5px',
        'margin-left': '30%',
        "background": 'white',
        'width': '40%',
        'position': 'relative',
      })
      $('.alert-title').css({
        'font-size': '17px',
        'border-bottom': '1px dotted #666',
        'padding': '10px',
        title: '标题'
      })
      $('.alert-content').css({
        'padding': '20px',
        title: '标题'
      })
      $('.alert').css({
        display: 'block'
      })
      $('#close').click(function () {
        $('.alert').remove()
      })
      // 点击空白黑色区域清除弹框
      $(document).click(function (e) {
        if (($(e.target).attr('class') == 'alert')) {
          if (o.vanish) {
            $('.alert').remove()
          }
        }
      })
    }
  }
})(jQuery);

效果如图:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值