user2387572
user2387572

Reputation: 31

Pop up box using JQuery

I am new to JQuery and am trying to add a pop-up box to my page.
I'm using the following jQuery plugin (adopted from Ray Stone's leanModal):

(function($) {
  $.fn.extend({
    leanModal: function(options) {
      var defaults = {
        top: 100,
        overlay: 0.5,
        closeButton: null
      };
      var overlay = $("<div id='lean_overlay'></div>");
      $("body").append(overlay);
      options = $.extend(defaults, options);
      return this.each(function() {
        var o = options;
        $(this).click(function(e) {
          var modal_id = $(this).attr("href");
          $("#lean_overlay").click(function() {
            close_modal(modal_id)
          });
          $(o.closeButton).click(function() {
            close_modal(modal_id)
          });
          var modal_height = $(modal_id).outerHeight();
          var modal_width = $(modal_id).outerWidth();
          $("#lean_overlay").css({
            "display": "block",
            opacity: 0
          });
          $("#lean_overlay").fadeTo(200, o.overlay);
          $(modal_id).css({
            "display": "block",
            "position": "fixed",
            "opacity": 0,
            "z-index": 11000,
            "left": 50 + "%",
            "margin-left": -(modal_width / 2) + "px",
            "top": o.top + "px"
          });
          $(modal_id).fadeTo(200, 1);
          e.preventDefault()
        })
      });

      function close_modal(modal_id) {
        $("#lean_overlay").fadeOut(200);
        $(modal_id).css({
          "display": "none"
        })
      }
    }
  })
})(jQuery);

My CSS looks like this:

#lean_overlay {
  position: fixed;
  z-index:100;
  top: 0px;
  left: 0px;
  height:100%;
  width:100%;
  background: #000;
  display: none;
}
#signup {
  width: 404px;
  padding-bottom: 2px;
  display:none;
  background: red;
  border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  box-shadow: 0px 0px 4px rgba(0,0,0,0.7);
  -webkit-box-shadow: 0 0 4px rgba(0,0,0,0.7);
  -moz-box-shadow: 0 0px 4px rgba(0,0,0,0.7);
}

My initialization script looks like this:

<script type="text/javascript">
  $(function() {
    $("#signup").leanModal();
  });
</script>

And, finally, this is the HTML that I'm using:

<p id="examples" class="section box">
  <strong>Example:</strong>
  <a id="go" rel="leanModal" name="signup" href="#signup">With Close Button</a>
</p>

<div id="signup">
  <div id="signup-ct">
    <div id="signup-header">
      <h2>This is a test</h2>
      <p>testing.</p>
      <a class="modal_close" href="#"></a>
    </div>
  </div>
</div>

With this code, the pop-up isn't coming up & I have a feeling that there must be a very simple fix for this, but I'm breaking my brain trying to figure it out. Any help you can give would be greatly appreciated!

Upvotes: 2

Views: 2774

Answers (2)

Sandcar
Sandcar

Reputation: 892

definitely one of the best solutions is to use the jquery UI. You can only download the modal plugin if you do not need the entire library.

Chears

Upvotes: 1

John Kroetch
John Kroetch

Reputation: 1143

I understand others are suggesting to use the existing library (highly recommended). But, if you want to solve the problem with your specific code, here's what I think you're doing wrong:

You're hooking into the wrong element with your 'leanmodal' call.

I've created a jsFiddle that pops up the dialog you want when the link is clicked. You can access it here: http://jsfiddle.net/eWUgE/

Basically, I've modified the following line:

$("#signup").leanModal();

to become:

$('#go').click(function() { 
    $(this).leanModal(); 
});

Upvotes: 3

Related Questions