简体   繁体   English

使用JQuery弹出框

[英]Pop up box using JQuery

I am new to JQuery and am trying to add a pop-up box to my page. 我是JQuery的新手,正在尝试向页面添加一个弹出框。
I'm using the following jQuery plugin (adopted from Ray Stone's leanModal): 我正在使用以下jQuery插件(从Ray Stone的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: 我的CSS看起来像这样:

#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: 最后,这是我正在使用的HTML:

<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! 您能提供的任何帮助将不胜感激!

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. 您通过“ leanmodal”调用陷入了错误的元素。

I've created a jsFiddle that pops up the dialog you want when the link is clicked. 我创建了一个jsFiddle,当单击链接时会弹出您想要的对话框。 You can access it here: http://jsfiddle.net/eWUgE/ 您可以在这里访问它: http : //jsfiddle.net/eWUgE/

Basically, I've modified the following line: 基本上,我修改了以下行:

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

to become: 成为:

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

definitely one of the best solutions is to use the jquery UI. 绝对最好的解决方案之一是使用jquery UI。 You can only download the modal plugin if you do not need the entire library. 如果不需要整个库,则只能下载模式插件。

Chears CHEARS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM