首页 > 代码库 > 自定义插件学习-弹框2
自定义插件学习-弹框2
;(function ($) { //默认参数 var PARAMS; var DEFAULTPARAMS = { Title: "素材家园", Content: "", Width: 400, Height: 300, URL: "", ConfirmFun: new Object, CancelFun: new Object }; var ContentWidth = 0; var ContentHeight = 0; $.DialogBySHF = { //弹出提示框 Alert: function (params) { Show(params, "Alert"); }, //弹出确认框 Confirm: function (params) { Show(params, "Confirm"); }, //弹出引用其他URL框 Dialog: function (params) { Show(params, "Dialog") }, //关闭弹出框 Close: function () { $("#DialogBySHFLayer,#DialogBySHF").remove(); } }; //初始化参数 function Init(params) { if (params != undefined && params != null) { PARAMS = $.extend({},DEFAULTPARAMS, params); } ContentWidth = PARAMS.Width - 10; ContentHeight = PARAMS.Height - 40; }; //显示弹出框 function Show(params, caller) { Init(params); var screenWidth = $(window).width(); var screenHeight = $(window).height(); //在屏幕中显示的位置(正中间) var positionLeft = (screenWidth - PARAMS.Width) / 2 + $(document).scrollLeft(); var positionTop = (screenHeight - PARAMS.Height) / 2 + $(document).scrollTop(); var Content = []; Content.push("<div id=\"DialogBySHFLayer\"></div>"); Content.push("<div id=\"DialogBySHF\" style=\"width:" + PARAMS.Width + "px;height:" + PARAMS.Height + "px;left:" + positionLeft + "px;top:" + positionTop + "px;\">"); Content.push(" <div id=\"Title\"><span>" + PARAMS.Title + "</span><span id=\"Close\">✕</span></div>"); Content.push(" <div id=\"Container\" style=\"width:" + ContentWidth + "px;height:" + ContentHeight + "px;\">"); if (caller == "Dialog") { Content.push("<iframe frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" src=http://www.mamicode.com/"" + PARAMS.URL + "\" ></iframe>"); } else { var TipLineHeight = ContentHeight - 60; Content.push(" <table>"); Content.push(" <tr><td id=\"TipLine\" style=\"height:" + TipLineHeight + "px;\">" + PARAMS.Content + "</td></tr>"); Content.push(" <tr>"); Content.push(" <td id=\"BtnLine\">"); Content.push(" <input type=\"button\" id=\"btnDialogBySHFConfirm\" value=http://www.mamicode.com/"确定\" />"); if (caller == "Confirm") { Content.push(" <input type=\"button\" id=\"btnDialogBySHFCancel\" value=http://www.mamicode.com/"取消\" />"); } Content.push(" </td>"); Content.push(" </tr>"); Content.push(" </table>"); } Content.push(" </div>"); Content.push("</div>"); $("body").append(Content.join("\n")); SetDialogEvent(caller); } //设置弹窗事件 function SetDialogEvent(caller) { //添加按钮关闭事件 $("#DialogBySHF #Close").click(function () { $.DialogBySHF.Close();}); //添加ESC关闭事件 $(window).keydown(function(event){ var event = event||window.event; if(event.keyCode===27){ $.DialogBySHF.Close(); } }); //添加窗口resize时调整对话框位置 $(window).resize(function(){ var screenWidth = $(window).width(); var screenHeight = $(window).height(); var positionLeft = parseInt((screenWidth - PARAMS.Width) / 2+ $(document).scrollLeft()); var positionTop = parseInt((screenHeight - PARAMS.Height) / 2+ $(document).scrollTop()); $("#DialogBySHF").css({"top":positionTop+"px","left":positionLeft+"px"}); }); $("#DialogBySHF #Title").DragBySHF($("#DialogBySHF")); if (caller != "Dialog") { $("#DialogBySHF #btnDialogBySHFConfirm").click(function () { $.DialogBySHF.Close(); if ($.isFunction(PARAMS.ConfirmFun)) { PARAMS.ConfirmFun(); } }) } if (caller == "Confirm") { $("#DialogBySHF #btnDialogBySHFCancel").click(function () { $.DialogBySHF.Close(); if ($.isFunction(PARAMS.CancelFun)) { PARAMS.CancelFun(); } }) } } })(jQuery); //拖动层 (function ($) { $.fn.extend({ DragBySHF: function (objMoved) { return this.each(function () { //鼠标按下时的位置 var mouseDownPosiX; var mouseDownPosiY; //移动的对象的初始位置 var objPosiX; var objPosiY; //移动的对象 var obj = $(objMoved) == undefined ? $(this) : $(objMoved); //是否处于移动状态 var status = false; //鼠标移动时计算移动的位置 var tempX; var tempY; $(this).mousedown(function (e) { status = true; mouseDownPosiX = e.pageX; mouseDownPosiY = e.pageY; objPosiX = obj.css("left").replace("px", ""); objPosiY = obj.css("top").replace("px", ""); }).mouseup(function () { status = false; }); $("body").mousemove(function (e) { if (status) { tempX = parseInt(e.pageX) - parseInt(mouseDownPosiX) + parseInt(objPosiX); tempY = parseInt(e.pageY) - parseInt(mouseDownPosiY) + parseInt(objPosiY); obj.css({ "left": tempX + "px", "top": tempY + "px" }); } //判断是否超出窗体 //计算出弹出层距离右边的位置 var dialogRight = parseInt($(window).width())-(parseInt(obj.css("left"))+parseInt(obj.width())); //计算出弹出层距离底边的位置 var dialogBottom = parseInt($(window).height())-(parseInt(obj.css("top"))+parseInt(obj.height())); var maxLeft = $(window).width()-obj.width(); var maxTop = $(window).height()-obj.height(); if(parseInt(obj.css("left"))<=0){ obj.css("left","0px"); } if(parseInt(obj.css("top"))<=0){ obj.css("top","0px"); } if(dialogRight<=0){ obj.css("left",maxLeft+‘px‘); } if(dialogBottom<=0){ obj.css("top", maxTop+‘px‘); } }).mouseup(function () { status = false; }).mouseleave(function () { status = false; }); }); } }) })(jQuery);
html中调用方法:
<script type="text/javascript"> $(document).ready(function () { $("#btnAlert").click(function () { $.DialogBySHF.Alert({ Width: 350, Height: 200, Title: "提示信息", Content: ‘你好,这是弹出提示,即JS中的alert‘, ConfirmFun: test }); }) $("#btnConfirm").click(function () { $.DialogBySHF.Confirm({ Width: 350, Height: 200, Title: "提示信息", Content: ‘是否确认删除?‘, ConfirmFun: test, CancelFun: testCancel }); }) $("#btnDialog").click(function () { $.DialogBySHF.Dialog({ Width: 1920, Height: 1080, Title: "17素材网", URL: ‘http://www.17sucai.com‘ }); }) }) function test() { $.DialogBySHF.Alert({ Width: 350, Height: 200, Title: "确认后执行方法", Content: ‘确认后执行的方法‘ }); } function testCancel() { $.DialogBySHF.Alert({ Width: 350, Height: 200, Title: "取消后执行方法", Content: ‘取消后执行的方法‘ }); } </script>
源码上传了,文件名是:jquery-dialog-drag1.rar
自定义插件学习-弹框2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。