灯箱加载内容而不被触发

时间:2012-04-26 09:19:31

标签: javascript jquery css

我正在使用一个简单的灯箱,或者你可以称之为模态窗口。在你点击PLAY按钮之前它没有加载任何flash内容,当你点击关闭时,它会停止flash游戏(它实际上会停止,因为当你再次点击PLAY时游戏从头开始) 但这只是关于FIREFOX,它不是CHROME,IE或Safari中的行为,除了MOZILLA FIREFOX所有浏览器都以完全不同的方式操作,这与我上面描述的方式相反。

在chrome,IE,Safari中,Opera可能会在不触发的情况下开始加载Flash内容(意味着如果你没有点击PLAY BUTTON仍会加载),即使你点击关闭按钮也是如此。而不是像在Firefox中一样停止闪存,Flash游戏继续运行(如最小化而不是关闭)。

为什么会发生这种情况,如何解决,我无法理解这种奇怪的行为?

相关代码: CSS:

#fade { 
    display: none; 
    background: #000;
    position: fixed; left: 0; top: 0;
    width: 100%; height: 100%;
    opacity: .80;
    z-index: 9999999;
}

.popup_block{
   width: 98.95%; height: 98.2%;
    display: none; 
    padding: 0px;
    line-height:1em;
    font-size: 1em;
    position: fixed;
    top: 0px; left: 0px;
    z-index: 999999999;
    -webkit-box-shadow: 0px 0px 20px #000;
    -moz-box-shadow: 0px 0px 20px #000;
    box-shadow: 0px 0px 20px #000;
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
}
.close {
    height:20px;
    float: right;
    margin: 0 2px 0 0;   
}

HTML:

<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a class="poplight" href="#?w=100%" rel="popup_name"><img alt="play game" class="happybutton" onmouseout="this.style.opacity=0.8;this.filters.alpha.opacity=80" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100" src="http://farm5.static.flickr.com/4084/4998558471_27e3985c16_m.jpg" style="opacity: 0.8;" /></a></td></tr>
</tbody></table>
<div class="popup_block" id="popup_name">
<div class="gamesharebuttons addthis_toolbox addthis_default_style ">
<a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_button_more">Share</a></div>
<iframe id="myvideo" width="100%" height="98%" src="http://files.cryoffalcon.com/bhgames/Adventure/star%20island.html" frameborder="0" scrolling="no" allowTransparency="false"
></iframe>
</div>

JS:

<script type="text/javascript">
$(document).ready(function(){

    //When you click on a link with class of poplight and the href starts with a # 
    $('a.poplight[href^=#]').click(function() {
        var popID = $(this).attr('rel'); //Get Popup Name
        var popURL = $(this).attr('href'); //Get Popup href to define size

        //Pull Query &amp; Variables from href URL
        var query= popURL.split('?');
        var dim= query[1].split('&amp;');
        var popWidth = dim[0].split('=')[1]; //Gets the first query string value

        //Fade in the Popup and add close button
        $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href=";#"; title=";Close It"; class=";close";><img src=";http://files.cryoffalcon.com/bloghuts/images/close%20button.png"; alt=";Close"; width=";20"; height=";20"; /></a>');

        //Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
        var popMargTop = ($('#' + popID).height() + 0) / 0;
        var popMargLeft = ($('#' + popID).width() + 0) / 0;

        //Apply Margin to Popup
        $('#' + popID).css({ 
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft
        });

        //Fade in Background
        $('body').append('<div id=";fade";></div>'); //Add the fade layer to bottom of the body tag.
        $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 

        return false;
    });


    //Close Popups and Fade Layer
    $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
        $('#fade , .popup_block').fadeOut(function() {
            $('#fade, a.close').remove();  
    }); //fade them both out

        return false;
    });


});

</script>

这里是现场演示页面http://bloghutsbeta.blogspot.com/2012/04/fullscreen-testing.html

1 个答案:

答案 0 :(得分:0)

display:none添加到包含您的Flash的iframe似乎并不能阻止Flash在后台播放,那么如何将其从页面中删除,将其存储在变量中按需添加。你看起来好像有多个灯箱,所以你可以将它们存放在一个物体中。

所以页面加载:

var popups = {};
$("a.poplight").each(function(){
  var $that = $("#" + $(this).attr('rel'));
  popups[$that.attr("id")] = $that.html(); //store the object in an object for later
  $that.html(""); //only empty it, don't remove it, so we can use it late
});

然后在你的$('a.poplight[href^=#]').click电话中,在淡出弹出窗口之前:

$('#' + popID).html(popups['#' + popID]);

希望有效并且我已经足够清楚了!

实施

<script type="text/javascript">
var popups = {};
$(document).ready(function(){
    $("a.poplight").each(function(){
       var $that = $("#" + $(this).attr('rel'));
       popups[$that.attr("id")] = $that.html(); //store the object in an object for later
       $that.html(""); //only empty it, don't remove it, so we can use it late
    });
    //When you click on a link with class of poplight and the href starts with a # 
    $('a.poplight[href^=#]').click(function() {
        var popID = $(this).attr('rel'); //Get Popup Name
        var popURL = $(this).attr('href'); //Get Popup href to define size

        //Pull Query &amp; Variables from href URL
        var query= popURL.split('?');
        var dim= query[1].split('&amp;');
        var popWidth = dim[0].split('=')[1]; //Gets the first query string value

        //Put the html back in
        $('#' + popID).html(popups['#' + popID]);

        //Fade in the Popup and add close button
        $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href=";#"; title=";Close It"; class=";close";><img src=";http://files.cryoffalcon.com/bloghuts/images/close%20button.png"; alt=";Close"; width=";20"; height=";20"; /></a>');

        //Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
        var popMargTop = ($('#' + popID).height() + 0) / 0;
        var popMargLeft = ($('#' + popID).width() + 0) / 0;

        //Apply Margin to Popup
        $('#' + popID).css({ 
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft
        });

        //Fade in Background
        $('body').append('<div id=";fade";></div>'); //Add the fade layer to bottom of the body tag.
        $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 

        return false;
    });


    //Close Popups and Fade Layer
    $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
        $('#fade , .popup_block').fadeOut(function() {
            $('#fade, a.close').remove();  
    }); //fade them both out

        return false;
    });


});

</script>