在页面加载时显示模态

时间:2012-05-01 06:40:17

标签: javascript jquery html resize centering

您好我已经编写了以下javascript来启用具有动态创建的id标签的模式,通过调整大小功能(无论其宽度和高度如何)定位到中心屏幕。

但是,在页面加载时,模式显示,而不是通过按钮显示onClick。我是一个菜鸟,所以我不确定我的代码中需要改变什么,所以模态会隐藏在页面加载上。

使用Javascript:


    $('body').prepend('');

    $(function(){
        $(window).resize(function(){       

    // get the screen height and width  
    var maskHeight = $(window).height();  
    var maskWidth = $(window).width();

    // calculate the values for center alignment
    var dialogTop =  (maskHeight  - $('.modalbox').height())/2;  
    var dialogLeft = (maskWidth - $('.modalbox').width())/2; 

    // assign values to the overlay and dialog box
    $('#overlay').css({ height:maskHeight, width:maskWidth }).show();
    $('#modalcontainer').css({ top: dialogTop, left: dialogLeft}).show();
    }).resize();
    });

    $('a.modal').each(function() {
          var link = $(this);
          var id = link.attr('href');
          var target = $(id);               

          if($("#modalcontainer " + id).length === 0) {
              $('#modalcontainer').append(target);
          }

          $("#main " + id).remove();

          link.click(function() {
            $('#modalcontainer > div').hide();        
            target.show();
            $('#overlay').show();
            return false;
          });
        });

        $('.close').click(function() {
          $('#modalcontainer > div').hide();
          $('#overlay').hide();

          return false;
        });​

CSS:


    #overlay {
      background: url(../img/overlay_bg.png);
      position:absolute;
      z-index:10;
    }

    #modalcontainer {
    position:absolute;
        z-index:20;
    }

HTML:

<a href="#modal1" class="modal button plain">view modal</a>
<div style="width:650px; height:400px;" id="modal1" class="modalbox">               
  <div class="box-header">
    <p  align="center">Here is your modal</p>
    <div class="box-content">

    </div>
    <div align="center" class="action_bar">              
      <a href="#" class="close button blue">Close</a>
    </div>
  </div>
    </div>​

<a href="#modal1" class="modal button plain">view modal</a> <div style="width:650px; height:400px;" id="modal1" class="modalbox"> <div class="box-header"> <p align="center">Here is your modal</p> <div class="box-content"> </div> <div align="center" class="action_bar"> <a href="#" class="close button blue">Close</a> </div> </div> </div>​

在jsfiddle演示:http://jsfiddle.net/5Egf8/4/

非常感谢任何帮助。?

2 个答案:

答案 0 :(得分:1)

更新:现在我可以看到你的小提琴,我已经修改了我的答案。请参阅此处的工作分部:http://jsfiddle.net/JXHAt/3/

好的,第三次是魅力。通过消除#modalcontainer,我更容易做到这一点。相反,要显示.modalbox,您只需隐藏当前的.modalbox-active,然后将.modalbox-active添加到您要显示的目标。

答案 1 :(得分:1)

删除第一个function()关键字;我认为你不需要它:

$( '主体')前面加上( '');

$(window).resize(function(){       

// get the screen height and width  
var maskHeight = $(window).height();  
var maskWidth = $(window).width();

// calculate the values for center alignment
var dialogTop =  (maskHeight  - $('.modalbox').height())/2;  
var dialogLeft = (maskWidth - $('.modalbox').width())/2; 

// assign values to the overlay and dialog box
$('#overlay').css({ height:maskHeight, width:maskWidth }).show();
$('#modalcontainer').css({ top: dialogTop, left: dialogLeft}).show();
}).resize();
});

$('a.modal').each(function() {
      var link = $(this);
      var id = link.attr('href');
      var target = $(id);               

      if($("#modalcontainer " + id).length === 0) {
          $('#modalcontainer').append(target);
      }

      $("#main " + id).remove();

      link.click(function() {
        $('#modalcontainer > div').hide();        
        target.show();
        $('#overlay').show();
        return false;
      });
    });

    $('.close').click(function() {
      $('#modalcontainer > div').hide();
      $('#overlay').hide();

      return false;
    });​

然后确保所有()和{}正确排队。祝你好运!

相关问题