admin管理员组文章数量:1022874
In short, the problem is this: we have a news site that has a mobile version of the website. For the page that displays whatever article is called, occasionally, there are embedded videos (from youtube) that show up in iframes. Their default size is typically something like 580px, which obviously es out far too big and wide on a mobile browser.
I wanted to use javascript to search the content and resize any iframe as it finds it. That said, I don't know if using JavaScript on mobile will be the best thing to do, and second, I'm not sure how to search for all instances of a certain element type like that. The iframes do not e in with names or IDs...
I briefly considered using PHP to search for <iframe
but it seems that php, in this instance, would be unnecessarily sloppy and potentially easily broken.
Does anybody have advice/suggestions on how to handle this problem?
In short, the problem is this: we have a news site that has a mobile version of the website. For the page that displays whatever article is called, occasionally, there are embedded videos (from youtube) that show up in iframes. Their default size is typically something like 580px, which obviously es out far too big and wide on a mobile browser.
I wanted to use javascript to search the content and resize any iframe as it finds it. That said, I don't know if using JavaScript on mobile will be the best thing to do, and second, I'm not sure how to search for all instances of a certain element type like that. The iframes do not e in with names or IDs...
I briefly considered using PHP to search for <iframe
but it seems that php, in this instance, would be unnecessarily sloppy and potentially easily broken.
Does anybody have advice/suggestions on how to handle this problem?
Share Improve this question edited Aug 15, 2011 at 14:45 Prisoner 27.7k11 gold badges76 silver badges103 bronze badges asked Aug 15, 2011 at 14:43 Cyprus106Cyprus106 5,7806 gold badges43 silver badges49 bronze badges3 Answers
Reset to default 5Using jQuery it's very easy to select elements based on tag.
jQuery:
$('iframe').width(300);
Fairly easy using normal DOM as well.
var iframes = document.getElementsByTagName('iframe');
for (var i=0; i<iframes.length; i++)
{
iframes[i].style.width = "300px"
}
Using jQuery, resizing all iframes on a document would be as simple as typing, for example,
$('iframe').css('width', '200px');
Sure, jQuery has an overhead in terms of size, but minified and gzipped, its footprint is not so large, even for mobile applications.
This worked for me:
Just include the width margin you want at the top:
//Just Change this
var margin = "20";
//////////////////////////////////////////////////////
var iframes = document.getElementsByTagName('iframe');
var w = window.innerWidth || document.body.clientWidth;
var newwidth = w - ( 2 * margin );
for (var i=0; i<iframes.length; i++)
{
var currentheight = iframes[i].height;
var currentwidth = iframes[i].width;
var newheight = (newwidth / currentwidth) * currentheight;
iframes[i].style.width = newwidth+"px";
iframes[i].style.height = newheight+"px";
}
In short, the problem is this: we have a news site that has a mobile version of the website. For the page that displays whatever article is called, occasionally, there are embedded videos (from youtube) that show up in iframes. Their default size is typically something like 580px, which obviously es out far too big and wide on a mobile browser.
I wanted to use javascript to search the content and resize any iframe as it finds it. That said, I don't know if using JavaScript on mobile will be the best thing to do, and second, I'm not sure how to search for all instances of a certain element type like that. The iframes do not e in with names or IDs...
I briefly considered using PHP to search for <iframe
but it seems that php, in this instance, would be unnecessarily sloppy and potentially easily broken.
Does anybody have advice/suggestions on how to handle this problem?
In short, the problem is this: we have a news site that has a mobile version of the website. For the page that displays whatever article is called, occasionally, there are embedded videos (from youtube) that show up in iframes. Their default size is typically something like 580px, which obviously es out far too big and wide on a mobile browser.
I wanted to use javascript to search the content and resize any iframe as it finds it. That said, I don't know if using JavaScript on mobile will be the best thing to do, and second, I'm not sure how to search for all instances of a certain element type like that. The iframes do not e in with names or IDs...
I briefly considered using PHP to search for <iframe
but it seems that php, in this instance, would be unnecessarily sloppy and potentially easily broken.
Does anybody have advice/suggestions on how to handle this problem?
Share Improve this question edited Aug 15, 2011 at 14:45 Prisoner 27.7k11 gold badges76 silver badges103 bronze badges asked Aug 15, 2011 at 14:43 Cyprus106Cyprus106 5,7806 gold badges43 silver badges49 bronze badges3 Answers
Reset to default 5Using jQuery it's very easy to select elements based on tag.
jQuery:
$('iframe').width(300);
Fairly easy using normal DOM as well.
var iframes = document.getElementsByTagName('iframe');
for (var i=0; i<iframes.length; i++)
{
iframes[i].style.width = "300px"
}
Using jQuery, resizing all iframes on a document would be as simple as typing, for example,
$('iframe').css('width', '200px');
Sure, jQuery has an overhead in terms of size, but minified and gzipped, its footprint is not so large, even for mobile applications.
This worked for me:
Just include the width margin you want at the top:
//Just Change this
var margin = "20";
//////////////////////////////////////////////////////
var iframes = document.getElementsByTagName('iframe');
var w = window.innerWidth || document.body.clientWidth;
var newwidth = w - ( 2 * margin );
for (var i=0; i<iframes.length; i++)
{
var currentheight = iframes[i].height;
var currentwidth = iframes[i].width;
var newheight = (newwidth / currentwidth) * currentheight;
iframes[i].style.width = newwidth+"px";
iframes[i].style.height = newheight+"px";
}
本文标签: javascriptgetting all iFrames within a div and resizing them for mobileStack Overflow
版权声明:本文标题:javascript - getting all iFrames within a div and resizing them for mobile - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745575328a2156983.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论