【问题标题】:Js show and hide functionjs显示隐藏功能
【发布时间】:2012-04-27 23:42:01
【问题描述】:

我知道有很多脚本可以显示和隐藏内容,但在我的场景中它们都不适合我。 在此测试页面上http://bloghutsbeta.blogspot.com/2012/04/testing-game-content-issue.html 我在 iframe 中有 flash 游戏,如果你点击 PLAY 按钮或者你不点击仍然会在 Chrome 和 IE 中开始加载 flash 内容(而不是在 firefox 中)。

因此,为了处理这种情况,我想到了使用显示和隐藏内容方法并使用了一些脚本,但它们都没有帮助,即使当我使用它们时,flash 内容仍然用于在 IE 的后端加载和铬合金。 我要的是一个脚本,它在执行“onclick”功能之前不会让 Flash 内容加载。 我知道一个脚本可以执行“LazyLoad”,但它适用于图像,我认为它也不适用于 Flash 内容。

注意:1-提供的链接上有音乐 2- 很抱歉提供 blogspot 链接,但 JsFiddle 不适合居住在阿富汗且速度为 5KBps 的人。

相关标记: 灯箱或模态窗口按钮

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

在点击上述按钮之前,内容设置为不显示(这实际上在 IE 和 Chrome 中仅在 firefox 中不起作用)

<div class="popup_block" id="popup_name">
<iframe width="100%" height="98%" src="http://files.cryoffalcon.com/bhgames/dressup/Celebrities/Wizard%20Fashion.html" frameborder="0" scrolling="no" allowTransparency="false"
></iframe>
</div>

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;   
}

JS(实际上是 Jquery)

&lt;script type=&quot;text/javascript&quot;&gt;
    $(document).ready(function(){

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

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

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

            //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 = ($(&#39;#&#39; + popID).height() + 0) / 0;
            var popMargLeft = ($(&#39;#&#39; + popID).width() + 0) / 0;

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

            //Fade in Background
            $(&#39;body&#39;).append(&#39;&lt;div id=&quot;fade&quot;&gt;&lt;/div&gt;&#39;); //Add the fade layer to bottom of the body tag.
            $(&#39;#fade&#39;).css({&#39;filter&#39; : &#39;alpha(opacity=80)&#39;}).fadeIn(); //Fade in the fade layer 

            return false;
        });


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

            return false;
        });


    });

    &lt;/script&gt;

【问题讨论】:

  • 所以问题实际上是:我怎样才能使用 javascript 加载 Flash 内容呢?
  • @twall 很好地加载了 Flash 内容,它没有问题,我所尝试的只是停止它的加载,并让它只有在我们点击播放按钮时才加载。
  • 为什么不点击播放按钮时动态添加iframe?
  • 嗯,这对我来说听起来是个好主意,你能不能给出一个完整的答案,这样我才能知道该怎么做,因为我在专业上编码有点薄弱,因为我是特许会计师。 (但我喜欢编程)

标签: javascript jquery


【解决方案1】:

您应该尝试这样的方法来在按钮单击后立即加载您的框架:

$(document).ready(function(){
    $('a.poplight[href^=#]').click(function() {
        $('<iframe/>')
            .attr('frameborder', 0)
            .attr('allowTransparency', false)
            .attr('scrolling', 'no')
            .attr('width', '100%')
            .attr('height', '98%')
            .attr('src', 'http://files.cryoffalcon.com/bhgames/dressup/Celebrities/Wizard%20Fashion.html')
            .appendTo('#popup_name');
    });
});

更新:要在弹出窗口关闭时删除框架,您可以使用:

$('#popup_name .close').live('click', function() {
    $('#popup_name iframe').remove();
});

【讨论】:

  • @Dmitryiy Chekalyuk 感谢您的回复,但您可以看到我现在拥有完整的代码,在上面提供的同一链接中它不起作用。去用谷歌浏览器使用它并加载游戏然后它会开始发出声音,即使你关闭它,它就像最小化没有关闭一样。
  • @CryOfFaclon 我明白了。您的关闭按钮会动态添加到 div,因此您必须使用 .live('click') 而不是 .click()。查看更新的答案。
  • 我已经做到了。但是如果你不介意的话,你可以在IE 8中看到,点击PLAY按钮打开它然后关闭,这样做3或4次你会看到有时它会显示一个黑屏。如果你知道或如何解决这个问题,这次全是你的选择帮助,我真的很感谢你,你是一个了不起的人。
  • 在与此问题相同的基础上,您想帮助我解决这个问题吗? stackoverflow.com/questions/10353193/…
猜你喜欢
  • 2013-03-20
  • 1970-01-01
  • 1970-01-01
  • 2018-07-09
  • 2012-06-06
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多