Franco 的个人资料The Devigner日志列表网络 工具 帮助

日志


    9月22日

    Creating a modal message box in jQuery

    One of the common tasks of web developers is to develop and design their site according to its purpose. And in-order for their sites to look more professional, the theme of the site, the animations, banners, buttons, fonts, links should be uniform. For example the font of a link button must be the same with the font of the content.

    In this article, I am going to show you how to create a modal dialog using jQuery instead of showing the usual pop-up message of your web browser. First lets take a look at the common way of doing it.

    The usual style.

    <script language="javascript" type="text/javascript">

    function ShowModalBox() {

    alert("The common box");

    }

    </script>

    <input type="button" value="Show Modal Box" onclick="ShowModalBox();" />

    The output.

    second

    The jQuery way of  doing it.

    <script language="javascript" type="text/javascript">
    $().ready(function() {
    $("#btnShowModalBox").click(function() {

    $("#Container").css("visibility","visible");
    $("#Container").css("float","none");
    $("#Container").css("position","absolute");
    $("#Container").css("left",0);
    $("#Container").css("top",0);
    $("#Container").css("width",window.screen.width);
    $("#Container").css("height",window.screen.height);
    $("#Container").css("background-color","#555555");
    $("#Container").css("opacity","0.5");

    var left = window.screen.width / 2 - 200;
    var top = window.screen.height / 2 - 300;
    $("#PopupBox").css("visibility","visible");
    $("#PopupBox").css("float","none");
    $("#PopupBox").css("position","absolute");
    $("#PopupBox").css("left",left);
    $("#PopupBox").css("top",top);

    });
    $("#btnClose").click(function() {
    $("#PopupBox").css("visibility","hidden");
    $("#Container").css("visibility","hidden");
    });
    });
    </script>

    <body style="margin: 50px; font-family: Verdana; font-size: 11px; font-weight: bold;">
    <form id="form1" runat="server">
    <div style="float: left; position: relative; width: 100%;">
    <input type="button" id="btnShowModalBox" value="Show Modal Box" />
    </div>
    <div id="PopupBox" style="width: 400px; height: 300px; visibility: hidden; z-index: 1;">
    <div style="float: left; position: relative; height: 20px; background-color: navy;
    width: 365px; color: #ffffff; padding-left:5px; padding-top:5px;">
    JQuery Modal Dialog
    </div>
    <div style="float: left; position: relative; height: 20px; background-color: navy;
    width: 30px; color: #ffffff; padding-top:5px; text-align:center;">
    <input type="button" id="btnClose" value="x" style="border-style:solid; border-width:1px; background-color:#ffffff; height:15px; padding:0px; cursor:pointer;" />
    </div>
    <div style="float: left; position: relative; background-color: #ffffff; width: 400px;
    height: 150px;text-align: center; vertical-align:middle;">
    This is how to implement a simple Modal Dialog
    </div>
    </div>
    <div id="Container" style="visibility: hidden; z-index: 0;">
    </div>
    </form>
    </body>

    The output.

    jqueryoutput

    If you'll notice there is now a gray background on top of our page where our dialog box is aligned in the middle.

    How did we achieve that?

    I. We must first create our dialog box.

    jqueryoutputBox

    Here is the code for the message box.

    <body style="margin: 50px; font-family: Verdana; font-size: 11px; font-weight: bold;">
    <form id="form1" runat="server">
    <div style="float: left; position: relative; width: 100%;">
    <input type="button" id="btnShowModalBox" value="Show Modal Box" />
    </div>
    <div id="PopupBox" style="width: 400px; height: 300px; visibility: hidden; z-index: 1;">
    <div style="float: left; position: relative; height: 20px; background-color: navy;
    width: 365px; color: #ffffff; padding-left:5px; padding-top:5px;">
    JQuery Modal Dialog
    </div>
    <div style="float: left; position: relative; height: 20px; background-color: navy;
    width: 30px; color: #ffffff; padding-top:5px; text-align:center;">
    <input type="button" id="btnClose" value="x" style="border-style:solid; border-width:1px; background-color:#ffffff; height:15px; padding:0px; cursor:pointer;" />
    </div>
    <div style="float: left; position: relative; background-color: #ffffff; width: 400px;
    height: 150px;text-align: center; vertical-align:middle;">
    This is how to implement a simple Modal Dialog
    </div>
    </div>

    <div id="Container" style="visibility: hidden; z-index: 0;">
    </div>
    </form>
    </body>

    II. Then our gray background which will cover the entire page.

    <body style="margin: 50px; font-family: Verdana; font-size: 11px; font-weight: bold;">
    <form id="form1" runat="server">
    <div style="float: left; position: relative; width: 100%;">
    <input type="button" id="btnShowModalBox" value="Show Modal Box" />
    </div>
    <div id="PopupBox" style="width: 400px; height: 300px; visibility: hidden; z-index: 1;">
    <div style="float: left; position: relative; height: 20px; background-color: navy;
    width: 365px; color: #ffffff; padding-left:5px; padding-top:5px;">
    JQuery Modal Dialog
    </div>
    <div style="float: left; position: relative; height: 20px; background-color: navy;
    width: 30px; color: #ffffff; padding-top:5px; text-align:center;">
    <input type="button" id="btnClose" value="x" style="border-style:solid; border-width:1px; background-color:#ffffff; height:15px; padding:0px; cursor:pointer;" />
    </div>
    <div style="float: left; position: relative; background-color: #ffffff; width: 400px;
    height: 150px;text-align: center; vertical-align:middle;">
    This is how to implement a simple Modal Dialog
    </div>
    </div>

    <div id="Container" style="visibility: hidden; z-index: 0;">
    </div>

    </form>
    </body>

    III. Finally our jQuery Code

    // jQuery has a simple statement that checks the document and waits until it's ready to be manipulated, known as the ready event

    $().ready(function() {

    }

    //Then we assign a click event for our btnShowModalBox button

    $().ready(function() {
    $("#btnShowModalBox").click(function() {

    });

    });

    //Then we manipulate the position,visibility, width and height of our gray message box container.

    $("#Container").css("visibility","visible"); -> sets the visibility of our div to visible which is initially hidden.
    $("#Container").css("float","none");  -> sets the float property
    $("#Container").css("position","absolute"); -> sets the position and assign aabsolute value
    $("#Container").css("left",0);  -> sets the left property
    $("#Container").css("top",0); -> sets the top property
    $("#Container").css("width",window.screen.width); -> sets the width of our div
    $("#Container").css("height",window.screen.height); -> sets the height of our div
    $("#Container").css("background-color","#555555"); -> sets the background color
    $("#Container").css("opacity","0.5"); -> sets the opacity of our gray background to half

    //And then we show our message box

    var left = window.screen.width / 2 - 200;
    var top = window.screen.height / 2 - 300;
    $("#PopupBox").css("visibility","visible");
    $("#PopupBox").css("float","none");
    $("#PopupBox").css("position","absolute");
    $("#PopupBox").css("left",left);
    $("#PopupBox").css("top",top);

    //Finally to hide our box we assign a click event to our button located on the upper right-most of the message box.

    $("#btnClose").click(function() {
    $("#PopupBox").css("visibility","hidden"); -> hides the messagebox
    $("#Container").css("visibility","hidden");-> hides the gray area
    });

    And that is how we create a simple modal message box using jQuery.  You can even make the message box fade by using the bellow function in jQuery.

    $("#PopupBox").fadeIn("slow");

    评论 (1)

    请稍候...
    很抱歉,您输入的评论太长。请缩短您的评论。
    您没有输入任何内容,请重试。
    很抱歉,我们当前无法添加您的评论。请稍后重试。
    若要添加评论,需要您的家长授予您相应权限。请求权限
    您的家长禁用了评论功能。
    很抱歉,我们当前无法删除您的评论。请稍后重试。
    您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
    因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
    完成下面的安全检查,您提供评论的过程才能完成。
    您在安全检查中键入的字符必须与图片或音频中的字符一致。

    若要添加评论,请使用您的 Windows Live ID 登录(如果您使用过 Hotmail、Messenger 或 Xbox LIVE,您就拥有 Windows Live ID)。登录


    还没有 Windows Live ID 吗?请注册

    woah too many codes, hopefully there's a designer for this. For the mean time, I'm happy with the intellisense that VS.NET 2008 offers for JQuery.
    9 月 25 日

    引用通告

    此日志的引用通告 URL 是:
    http://francorobles.spaces.live.com/blog/cns!A1E9BBD296578595!162.trak
    引用此项的网络日志