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


PHP Customizing Module to show if DIV exists

我对PHP很陌生,所以定制现成的脚本还不是我的强项。

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

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

这是我需要调整的脚本:

$(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>

非常感谢大家,你们总是给我很大的帮助!

-Andrew

编辑

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

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文件,使其也可以自动打开以及点击?

谢谢!

您可能最好使用会话或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>