PHP自定义模块,用于显示DIV是否存在

时间:2012-01-12 17:29:08

标签: php popup modal-dialog

我是PHP的新手,所以定制现成的脚本还不是我的目标。

我有一个动画弹出模式脚本,当单击某个时会触发该脚本。我还想在页面上存在特定div时自动触发相同的脚本。

从代码中可以看出,#mask是页面上半透明的黑色层。

这是我需要调整的脚本:

$(document).ready(function() {  

    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');

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

        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect     
        $('#mask').fadeIn(1000);    
        $('#mask').fadeTo("slow",0.8);  

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);

        //transition effect
        $(id).fadeIn(2000); 

    });

});

目前,点击此链接时会自动打开:

<a href="#" name="modal">

当我在页面上存在此DIV时,我正试图让它自动运行:

<div name="showintrovideo"></div>

非常感谢你们,你们总是非常有帮助!

- - 安德鲁

修改

以下是我正在使用的完整代码:

HTML

<div id="boxes">
    <div id="qrcodemarketing" class="window">

        <!-- close button is defined as close class -->
        <div style="float:right;">
        <a href="#" class="close"><img src="images/close_window.png" width="22"height="22" alt="Close Window" /></a>
        </div>

        <iframe width="640" height="480" src="http://www.youtube.com/embed/MYVIDEO" frameborder="0" allowfullscreen></iframe><br /><b>Pause the video before closing this window</b>

    </div>
    </div>




    <!-- Do not remove div#mask, because you'll need it to fill the whole screen --> 
    <div id="mask"></div>

CSS

#mask {
  position:absolute;
  width:100%;
  height:100%;
  left:0;
  top:0;
  z-index:9000;
  background-color:#000;
  display:none;
}

#boxes .window {
  position:absolute;
  background:none;
  display:none;
  z-index:9999;
  padding:20px;
  color:#fff;
}

.js文件

$(document).ready(function() {  

    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');

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

        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect     
        $('#mask').fadeIn(1000);    
        $('#mask').fadeTo("slow",0.8);  

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);

        //transition effect
        $(id).fadeIn(2000); 

    });


    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });     

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });         

});

可以只调整.js文件,这样它也可以自动打开,也可以点击?

谢谢!

1 个答案:

答案 0 :(得分:0)

您最好使用会话或Cookie来确定是否要显示简介视频。我还将你的弹出窗口包装在一个可以在jQuery元素上调用的方法中。类似的东西:

$('a').myPopupMethod();

这样,您也可以通过编程方式调用它:

$.myPopupMethod();

在HTML页面中,您可以使用PHP来确定是否显示弹出窗口:

<?php
    session_start();

    $video_watched = $_SESSION['video_watched'];
?>
<!DOCTYPE html>
<html>
  <body>
    <script src="jquery.js"></script>
<?php if ($video_watched != true): ?>
    <script>
        $(document).ready(function() {
            $.myPopupMethod();
        });
    </script>
<?php endif; ?>
  </body>
</html>