jQuery實(shí)現(xiàn)的自定義彈出層效果實(shí)例詳解
本文實(shí)例講述了jQuery實(shí)現(xiàn)的自定義彈出層效果。分享給大家供大家參考,具體如下:
dialog.css:
#DialogBySHFLayer { width:100%; height:100%; left:0; top:0; position:fixed; z-index:500; background-color:#333333; filter:alpha(Opacity=40); -moz-opacity:0.4; opacity: 0.4; } /*彈出的提示框*/ #DialogBySHF { position:absolute; border-radius:3px; box-shadow:0 0 8px rgba(0, 0, 0, .8); background-color:#f2f2f2; z-index:600; } #DialogBySHF #Title { margin:0; width:100%; height:35px; background-color:#ffa500; color:#FFFFFF; font-family: 'microsoft yahei'; font-size:18px; text-align:center; cursor:move; line-height:35px; border-radius:3px 3px 0 0; -moz-user-select:none; -webkit-user-select:none; user-select:none; } #DialogBySHF #Close { position:absolute; right:7px; top:6px; height:21px; line-height:21px; width:21px; cursor:pointer; display:block; border:1px solid #da8e02; box-shadow:0 0 4px rgba(255, 255, 255, .9); border-radius:3px; } #DialogBySHF #Container { padding:0px 5px 5px 5px; /*width:390px; height:355px;*/ } #DialogBySHF #Container table,#DialogBySHF #Container iframe { width:100%; height:100%; } #DialogBySHF #Container table td { vertical-align:middle; } #DialogBySHF #Container table #TipLine { padding:0 30px; font-family: 'microsoft yahei'; } #DialogBySHF #Container table #BtnLine { height:60px; text-align:center; } #DialogBySHF #Container table #BtnLine input { margin:6px 11px; -moz-user-select: none; background-color:#F5F5F5; background-image: -moz-linear-gradient(center top , #F5F5F5, #F1F1F1); background-image:-ms-linear-gradient(rgb(245, 245, 245), rgb(241, 241, 241)); background-image:-webkit-linear-gradient(top,#f8f8f8,#f1f1f1); border:1px solid rgba(0,0,0,0.1); *border:1px solid #DDDDDD; border:1px solid #DDDDDD\0; border-radius:2px; font-family: 'microsoft yahei'; color:#666666; cursor:default; font-size:12px; font-weight:bold; height:29px; line-height:27px; min-width:54px; padding:0 8px; text-align:center; } #DialogBySHF #Container table #BtnLine input:hover { background-color: #F8F8F8; background-image: -moz-linear-gradient(center top , #F8F8F8, #F1F1F1); border: 1px solid #C6C6C6; box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1); color: #333333; } #DialogBySHF #Container table #BtnLine input:focus { border: 1px solid #4D90FE; outline: medium none; }
dialog.js:
;(function ($) { //默認(rèn)參數(shù) var PARAMS; var DEFAULTPARAMS = { Title: "彈出層的標(biāo)題", 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"); }, //彈出確認(rèn)框 Confirm: function (params) { Show(params, "Confirm"); }, //彈出引用其他URL框 Dialog: function (params) { Show(params, "Dialog") }, //關(guān)閉彈出框 Close: function () { $("#DialogBySHFLayer,#DialogBySHF").remove(); } }; //初始化參數(shù) 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=\"" + 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=\"確定\" />"); if (caller == "Confirm") { Content.push(" <input type=\"button\" id=\"btnDialogBySHFCancel\" value=\"取消\" />"); } Content.push(" </td>"); Content.push(" </tr>"); Content.push(" </table>"); } Content.push(" </div>"); Content.push("</div>"); $("body").append(Content.join("\n")); SetDialogEvent(caller); } //設(shè)置彈窗事件 function SetDialogEvent(caller) { //添加按鈕關(guān)閉事件 $("#DialogBySHF #Close").click(function () { $.DialogBySHF.Close();}); //添加ESC關(guān)閉事件 $(window).keydown(function(event){ var event = event||window.event; if(event.keyCode===27){ $.DialogBySHF.Close(); } }); //添加窗口resize時(shí)調(diào)整對(duì)話框位置 $(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); //拖動(dòng)層 (function ($) { $.fn.extend({ DragBySHF: function (objMoved) { return this.each(function () { //鼠標(biāo)按下時(shí)的位置 var mouseDownPosiX; var mouseDownPosiY; //移動(dòng)的對(duì)象的初始位置 var objPosiX; var objPosiY; //移動(dòng)的對(duì)象 var obj = $(objMoved) == undefined ? $(this) : $(objMoved); //是否處于移動(dòng)狀態(tài) var status = false; //鼠標(biāo)移動(dòng)時(shí)計(jì)算移動(dòng)的位置 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" }); } //判斷是否超出窗體 //計(jì)算出彈出層距離右邊的位置 var dialogRight = parseInt($(window).width())-(parseInt(obj.css("left"))+parseInt(obj.width())); //計(jì)算出彈出層距離底邊的位置 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'); } }).mouseup(function () { status = false; }).mouseleave(function () { status = false; }); }); } }) })(jQuery);
demo.html:
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no"> <title>新建H5模板</title> <link rel="stylesheet" href="css/dialog.css" /> <style> input[type="button"] { margin: 100px 20px; padding: 10px; } </style> <script type="text/javascript" src="js/jquery-1.10.1.min.js"></script> <script type="text/javascript" src="js/dialog.js"></script> </head> <body> <center> <input type="button" value="彈出提示框" id="btnAlert" /> <input type="button" value="彈出確認(rèn)框" id="btnConfirm" /> <input type="button" value="彈出iframe" id="btnDialog" /> </center> <script type="text/javascript"> $(function() { $("#btnAlert").click(function() { $.DialogBySHF.Alert({ Width: 350, Height: 200, Title: "彈出提示框", Content: "你好,你選擇的內(nèi)容是空白的", ConfirmFun: test }); }) $("#btnConfirm").click(function() { $.DialogBySHF.Confirm({ Width: 350, Height: 200, Title: "彈出確認(rèn)框", Content: "你確定要?jiǎng)h除這條內(nèi)容嗎", ConfirmFun: test, CancelFun: testCancel }); }) $("#btnDialog").click(function() { $.DialogBySHF.Dialog({ Width: 1024, Height: 500, Title: "新開(kāi)一個(gè)窗口", URL: "http://www.dbjr.com.cn" }); }) }) function test() { $.DialogBySHF.Alert({ Width: 350, Height: 200, Title: "確認(rèn)后執(zhí)行方法", Content: "確認(rèn)后執(zhí)行的方法" }); } function testCancel() { $.DialogBySHF.Alert({ Width: 350, Height: 200, Title: "取消后執(zhí)行方法", Content: "取消后執(zhí)行的方法" }); } </script> </body> </html>
效果圖:
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jquery中Ajax用法總結(jié)》、《jQuery常見(jiàn)經(jīng)典特效匯總》、《jQuery動(dòng)畫(huà)與特效用法總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- JQuery彈出層示例可自定義
- jQuery Dialog 彈出層對(duì)話框插件
- jquery實(shí)現(xiàn)居中彈出層代碼
- jQuery彈出層始終垂直居中相對(duì)于屏幕或當(dāng)前窗口
- JQUERY THICKBOX彈出層插件
- Jquery 彈出層插件實(shí)現(xiàn)代碼
- jQuery+html5實(shí)現(xiàn)div彈出層并遮罩背景
- jquery實(shí)現(xiàn)點(diǎn)擊彈出層效果的簡(jiǎn)單實(shí)例
- 漂亮的jquery提示效果(仿騰訊彈出層)
- jQuery實(shí)現(xiàn)簡(jiǎn)單網(wǎng)頁(yè)遮罩層/彈出層效果兼容IE6、IE7
- jQuery點(diǎn)擊自身以外地方關(guān)閉彈出層的簡(jiǎn)單實(shí)例
相關(guān)文章
JQuery插件iScroll實(shí)現(xiàn)下拉刷新,滾動(dòng)翻頁(yè)特效
下拉自動(dòng)加載進(jìn)行分頁(yè)的運(yùn)用越來(lái)越多,比起傳統(tǒng)的分頁(yè)該方法分頁(yè)用戶體驗(yàn)更好,布局也更簡(jiǎn)單了。目前正在使用和學(xué)習(xí)中……2014-06-06用jQuery解決IE不支持的option disable屬性
使用jQuery解決IE不支持的option disable屬性2009-05-05jQuery Mobile的loading對(duì)話框顯示/隱藏方法分享
jQuery Mobile提供兩個(gè)方法,使得開(kāi)發(fā)者在編寫(xiě)JavaScript業(yè)務(wù)編輯時(shí),可以隨意的控制loading提示框2013-11-11jquery實(shí)現(xiàn)圖片上傳前本地預(yù)覽
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)圖片上傳前本地預(yù)覽功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04jQuery實(shí)現(xiàn)Ajax功能分析【與Flask后臺(tái)交互】
這篇文章主要介紹了jQuery實(shí)現(xiàn)Ajax功能,結(jié)合實(shí)例形式分析了jQuery ajax功能實(shí)現(xiàn)方法以及與Flask后臺(tái)進(jìn)行交互的相關(guān)操作技巧,需要的朋友可以參考下2019-06-06jquery獲取焦點(diǎn)和失去焦點(diǎn)事件代碼
鼠標(biāo)在搜索框中點(diǎn)擊的時(shí)候里面的文字就消失了,經(jīng)常會(huì)用到搜索框的獲得焦點(diǎn)和失去焦點(diǎn)的事件,接下來(lái)介紹一下具體代碼,感興趣的朋友額可以參考下2013-04-04S2SH整合JQuery+Ajax實(shí)現(xiàn)登錄驗(yàn)證功能實(shí)現(xiàn)代碼
登錄驗(yàn)證,在項(xiàng)目開(kāi)發(fā)中很常用的,尤其是這一塊非常有利于用戶體驗(yàn),感興趣的朋友可以參考下,或許對(duì)你學(xué)習(xí)登陸驗(yàn)證有所幫助,好了閑話不多說(shuō)了,看代碼2013-01-01