Show modal dialog with 'x' without using jqueryui

前端 未结 2 1819
不知归路
不知归路 2020-12-22 09:53

looking for a very simple implementation of modal dialog box without using jquery UI.

just for example

相关标签:
2条回答
  • 2020-12-22 10:37

    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. :-)

    0 讨论(0)
  • 2020-12-22 10:43

    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 讨论(0)
提交回复
热议问题