开发者

Show modal dialog with 'x' without using jqueryui

looking for a very si开发者_运维技巧mple implementation of modal dialog box without using jquery UI.

just for example

<div class="modal" id="something" style="display: none;">
   <div class "titlebar">
      <h4>title</hr>
      <a href="#" >xx</a>
   </div>
   <div class=content></div>
  <div class=footer></div>
</div>

When user click on the button I want to show dialog and close when user pres 'xx'. I saw some example but it has many extras. I am looking for somethig like click of button show dialog it shoud stay there till user clicks on 'x' no jqueryui.

any simple example will be great.


Here's a very basic version:

$("<iframe id='shim' src='http://jsbin.com/abira4'>").css({
    width: "100%",
    height: "100%",
    position: "absolute",
    left: "0px",
    top: "0px",
    zIndex: "100",
    backgroundColor: "#fff",
    opacity: "0.5"
}).appendTo(document.body);
$("<div>Hi there, click me to dismiss</div>").css({
    zIndex: "101",
    border: "1px solid black",
    backgroundColor: "#ecc",
    position: "absolute",
    left: "100px",
    top: "100px"
}).appendTo(document.body)
  .click(function() {
    $(this).remove();
    $("#shim").remove();
});

Live example

How it works:

  • We create an iframe to act as a shim over the body of the page. This eats any clicks outside the "dialog box" div we add a moment later, and also deals with the issue of OS-rendered controls and flash animations popping to the top. We also make it semi-transparent (in this case, background-color is #f0f0f0 and opacity is 50%) so it "greys out" the underlying document. This is absolutely positioned at 0,0 with 100% width and height, and a z-index of 100 (this has to be higher than anything else on the page). The src of the iframe should be a blank document.
  • We then create a div for the "dialog box" which is also absolutely positioned and has a z-index higher than the iframe shim.

Now, there are a lot of variations. For instance, there's no reason at all you can't have the model div markup in the HTML document rather than in the script — -- just give it display: none until you need it. Things like that.

Obviously, this version lets you click anywhere on the dialog box to dismiss it, but it's easily modified to only allow closing with an [X] somewhere. And to say that it could use some styling would be to...put it mildly.

All that said, I can't claim it's remotely bulletproof. That's why well-tested plug-ins are for. :-)


I think you're looking for something like this:

$('.modal .titlebar a').click(function() {
  $(this).parents('.modal').hide();
});

$('button.open-modal').click(function() {
  $('#something').show();
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜