【发布时间】:2014-02-08 16:18:39
【问题描述】:
我是论坛的新手,第一个问题! 我是一个初学者,所以它可能非常基础......
我正在尝试实现使 iframe 响应的代码
(function($) {
$.fn.responsiveVideo = function() {
// Add CSS to head
$( 'head' ).append( '<style type="text/css">.responsive-video-wrapper{width:100%;position:relative;padding:0}.responsive-video-wrapper iframe,.responsive-video-wrapper object,.responsive-video-wrapper embed{position:absolute;top:0;left:0;width:100%;height:100%}</style>' );
// Gather all videos
var el = $(this),
all_videos = el.find( 'iframe[src*="player.vimeo.com"], iframe[src*="youtube.com"], iframe[src*="dailymotion.com"],iframe[src*="kickstarter.com"][src*="video.html"], object, embed' );
// Cycle through each video and add wrapper div with appropriate aspect ratio
all_videos.each( function() {
var video = $(this)
aspect_ratio = video.attr( 'height' ) / video.attr( 'width' );
video
.removeAttr( 'height' )
.removeAttr( 'width' );
if ( ! video.parents( 'object' ).length )
video.wrap( '<div class="responsive-video-wrapper" style="padding-top: ' + ( aspect_ratio * 100 ) + '%" />' );
} );
};
} )(jQuery);
要激活它,我使用以下代码调用它:
$(window).ready(function() {
$( 'body' ).responsiveVideo();
});
我的问题是它仅适用于页面的第一次加载或刷新页面时。当我导航到包含 iframe 的页面时,它不起作用。
谢谢!
【问题讨论】:
-
将
responsiveVideo()函数排除在(function($) {之外。 -
如果您解释了它应该做什么以及“它不起作用”时会发生什么,这将有所帮助
-
您确定它不能与 new 尚未加载的 iframe 一起正常工作,因为该代码中没有 onload 处理程序?
-
而且只有文档有准备好的处理程序,窗口没有。
标签: javascript jquery iframe