简体   繁体   English

如何仅使用@media和display:none加载一个IFRAME; CSS

[英]how to only load one IFRAME using @media and display:none; CSS

I am using @media CSS to serve up 3 different sizes pages depending on device viewport width. 我正在使用@media CSS来提供3种不同大小的页面,具体取决于设备视口的宽度。

I use 3 's so depending on screen width I hide 2 and display 1 我使用3,所以根据屏幕宽度我隐藏2并显示1

the problem is that when I use 's it loads all 3 even when the iframe is within display:none; 问题是,当我使用时,即使iframe在display内,它也会加载全部3个:none;

I need only one of the 's to load mainly the one in the that is visible not the other two that are hidden. 我只需要加载的其中一个即可,而不必隐藏其他两个。

here is an example of the html 这是html的示例

<div class='s1'>
<p>Full size page content here</p>
<iframe id='frame1' src="chat/flashchat.php" width="513" height="500">     </iframe>
<p> more full size page content here</p>

</div>

<div class='s2'>
<p>Medium size page content here</p>
<iframe id='frame1' src="chat/flashchat.php" width="513" height="500">    </iframe>
<p>More medium content here</p>


</div>

<div class='s3'>
<p>Small page content here</p>
<iframe id='frame1' src="chat/flashchat.php" width="320" height="500">    </iframe>
<p>More small page content here</p>

</div>

here is the CSS: 这是CSS:

@media handheld, screen and (min-width: 768px) {
.s2 {
display: none;
}
.s3 {
display: none;
}
.s1 {
display: block;
}

}

@media handheld, screen and (max-width:767px) and (min-width:513px) {
.s1 {
display: none;
}
.s3 {
display: none;
}
.s2 {
display: block;
}

}


@media handheld, screen and (max-width:512px)  {
.s1 {
display: none;
}
.s2 {
display: none;
}
.s3 {
display: block;
}


}

I am thinking javascript might provide a solution but would like to avoid that method if possible. 我认为javascript可能提供解决方案,但如果可能的话,希望避免使用该方法。 I can only allow one of these IFRAMES to load even though only one is displayed the 3 loading messes up an automatic login feature because the is actually loading 3 times even though it is hidden. 即使只显示其中一个,我也只能加载其中一个IFRAMES。3个加载会弄乱自动登录功能,因为即使隐藏了,它实际上也会加载3次。

Any advise on resolving this issue? 对解决此问题有何建议?

Here's the JS I used: 这是我使用的JS:

$(document).on('ready', responsiveVideo);
$(window).on('resize', responsiveVideo);
function responsiveVideo() {
    var windowWidth = $(window).width();
    // Let's make sure that the appropriate video is visible
    $('.video-class-selector').each(function() {
        if ($(this).hasClass('.js__video-hack')) {
            return;
        }
        var isResponsiveVideo = $(this).parent('.video_mobile_wrapped').length,
            BREAKPOINT = 768;

        // Replace visible iframes with a proper iframe with a valid src attribute
        var $original = $(this).find('iframe');
        if ((isResponsiveVideo && windowWidth < BREAKPOINT) || (!isResponsiveVideo && windowWidth >= BREAKPOINT)) {
            var $clone = $original.clone();

            $clone.attr('src', $clone.attr('data-src'));
            $original.replaceWith($clone);

            $(this).addClass('js__video-hack');
        }
    });
}

My markup for the iframe looks like this: 我对iframe的标记如下所示:

<div class="video-class-selector">
  <iframe data-src="..." ...></iframe>
</div>

The responsive one is wrapped in a div with class video_mobile_wrapped: 响应式包装在带有video_mobile_wrapped类的div中:

<div class='video_mobile_wrapped'>
    <div class="video-class-selector">
      <iframe data-src="..." ...></iframe>
    </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM