封装基于jq弹窗插件
2024-08-28 13:25:29
相信码友们对于$.fn.extexd();$.extend()以及$.fn.custom和$.custom都有一定的了解;我阐述一下我自己对于$.fn.custom和$.custom的理解、有理解错误或是有更好的建议直接喷我就好!
下面咱们进行简单插件的封装;
Jquery为开发插件提供了两个方法,分别是:
$.fn.INFOplug= $.INF_Oplug=function(){}
先简单的阐述下这两种方法:
这两个分别是:
$.INFplug;是为了扩展jquery本身,为类添加新的工具方法;
$.fn.INFOplug;给jquery对象添加方法。
例子:
$.fn.INFOplug=function(a,b){
alert(a+b)
};
$.INF_Oplug=function(a,b){
alert(a+b)
}
该两种方法分别:
在jquery对象上创建了一个INFOplug方法;该方法中打印a+b;
在jquery上拓展了工具INF_Oplug方法;打印如上a+b;
两者的使用方法是不同的,下面举个实例:
INFOplug方法:
$('div).INFOplug(4,5);
INF_Oplug方法:
$.INF_Oplug(4,5)
这是两者大致的意思,下面我们用这两个方法进行封装一个基于jquery的弹窗插件,代码如下:
(function($){
var __INFO__ = {
plug:'jquerySpwialog',
ver:'1.0.1.2',
author:'sunpengwei'
};
var defaults = {
title:"",
tips:"",
txt:null,
ok:"确定",
cancel:"",
callback:function(){},
htmls:'<div id="jquery_dn_dialog_mask" style="background:rgba(0,0,0,0.3);width:100%;height:100%;position:fixed;top:0;left:0;z-index:2147483647">\
<div id="jqurey_dn_dialog_main" style="background:#dedede;left:50%;top:50%;position:absolute;transform:translate(-50%,-50%);border-radius:10px;overflow:hidden;min-width:270px">\
<h1 id="jqdndialog_title" style="text-align:center;font-size:22px;padding:10px 0 0 0;magring:10px 5px">弹窗标题</h1>\
<div id="jqdndialog_tips" style="font-size:18px;margin:0 17px;padding:0 0 20px 0">弹窗内容</div>\
<p style="margin:5px 20px">\
<input id="jqdndialog_txt" type="text" style="border-radius:5px;width:100%;line-height:30px;font-size:20px;border:0px">\
</p>\
<div id="jqdndialog_button" style="border-top:1px solid #b8b8b8;display:flex;justify-content:space-around;">\
<a href="javascript:;" style="color:#007aff;font-weight:bold;display:inline-block;height:44px;line-height:44px;text-align:center;text-decoration:none;width:100%">按钮</a>\
<a href="javascript:;" style="color:#007aff;font-weight:bold;display:inline-block;height:44px;line-height:44px;text-align:center;text-decoration:none;width:100%;border-left: 1px solid #b8b8b8" >按钮</a>\
</div>\
</div>\
</div>'
}; $.fn[__INFO__.plug] = $[__INFO__.plug] = function(options){
var settings = $.extend({},defaults,options); //初始化
var initDOM = function(conf){
if(conf) settings = $.extend({},settings,conf);
//获取元素
var $mask = $(settings.htmls).appendTo(document.body);
var $main = $mask.children("#jqurey_dn_dialog_main");
var $title = $main.children("#jqdndialog_title");
var $tips = $main.children("#jqdndialog_tips");
var $txt = $main.find("#jqdndialog_txt");
var $ok = $main.find("#jqdndialog_button a:first");
var $cancel = $main.find("#jqdndialog_button a:last");
//赋值
$title.html(settings.title);
$tips.html(settings.tips);
if(settings.txt === null){
$txt.hide();
}else{
$txt.val(settings.txt).focus();
} $ok.html(settings.ok);
if(settings.cancel){
$cancel.html(settings.cancel);
}else{
$cancel.hide();
} $main.find("#jqdndialog_button a").bind("touchstart click",function(e){
console.log(e.type);
e.preventDefault();
e.stopPropagation();
var res = {result:$(this).text()};
if(settings.txt !== null) res.texts = $txt.val(); if(settings.callback) settings.callback(res);
$mask.remove(); }).hover(function(){
$(this).css("background","#d2d2d2");
}, function(){
$(this).css("background","transparent");
});
}; this.bind("touchstart click",function(e){
e.preventDefault();
e.stopPropagation();
initDOM();
}); if($.isFunction(this)) initDOM();
this.showSpwialog = function(options){
initDOM(options);
} return this;
};
})(jQuery);
最新文章
- IBM云的商务动作之我见(2):IBM 和 VMware 战略合作推进混合云
- ASP.NET 多语言的实现(后台消息+前台消息+页面自动绑定)
- mongo数据备份及恢复脚本
- CSS3的REM设置字体大小
- CABasicAnimation 按home键后台之后,再切回来动画就停止了
- ubuntu 下dbus的环境搭建和使用
- C#_Fileuploadify_notMvc
- SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-007-给flowl加权限控制<;secured>;
- Understanding Network Class Loaders
- RelativeLayout与LinearLayout的区别
- Love myself...
- rabbitmq——镜像队列
- Kafka基本知识回顾及复制
- html备战春招の一
- webuploader 上传文件 生成链接下载文件
- 在Django中使用logging模块
- yum clear all无反应
- 【实战问题】【3】iPhone无法播放video标签中的视频
- 基于MNIST数据集使用TensorFlow训练一个包含一个隐含层的全连接神经网络
- Educational Codeforces Round 42 (Rated for Div. 2) E. Byteland, Berland and Disputed Cities