我在jsFiddle中有这个jQuery代码,它将iframe扩展为内容的最大尺寸。
在后您添加HTML代码:
<article>
<p><iframe width="400" height="400" src="http://www.youtube.com/...."></iframe></p>
</article>
在模板中添加下面的jQuery代码。
<script type='text/javascript'>
jQuery(document).ready(function($) {
$(function() {
var $allVideos = $("iframe[src^='http://player.vimeo.com'], iframe[src^='http://www.youtube.com'], object, embed"),
$fluidEl = $("p");
$allVideos.each(function() {
$(this)
// jQuery .data does not work on object/embed elements
.attr('data-aspectRatio', this.height/this.width)
.removeAttr('height')
.removeAttr('width');
});
$(window).resize(function() {
var newWidth = $fluidEl.width();
$allVideos.each(function() {
var $el = $(this);
$el
.width(newWidth)
.height(newWidth * $el.attr('data-aspectRatio'));
});
}).resize();
});
});
</script>
我试了我的Wordpress网站上的代码,它的工作。
在Chrome中很有效。有关Safari和Firefox浏览器兼容性的任何建议? – user2132752 2013-03-26 00:09:48
我不是javascript专家,但看着Chrome开发人员工具,你有很多Javascript错误。先尝试摆脱那些。检查这个http://stackoverflow.com/questions/7181573/does-javascript-execution-stop-when-an-invalid-line-is-encountered – user850010 2013-03-26 07:27:51