简体   繁体   English

更改 iframe 中的 src 取决于视口大小

[英]change src in iframe which depend on viewport size

I'm quite new in website area我在网站区很新

I'm trying to change src in my iframe which the src content is a table along with the width and height which make it not responsive我正在尝试更改 iframe 中的 src,其中 src 内容是一个表格以及宽度和高度,这使得它没有响应

I'm trying to change the src which depend on the size of the viewport我正在尝试更改取决于视口大小的 src

here is my code这是我的代码

 <iframe width="100%" height="410" scrolling="yes" frameborder="0" scrolling="no" allowtransparency="true" src="http://tools.fxempire.com/sidebar-quotes.php?instruments=5,10,1,2,4,3&width=375&height=410&text_color=333&text_hover_color=082F60"></iframe> <div id="fxempire_link" style="width:500px;font-size: 11px;"> The Free Charts are Powered by <a rel="nofollow" style="text-decoration: underline;" target="_blank" href="http://www.fxempire.com">FXEmpire.com</a> - Your Leading Financial Portal</div>

I'm trying to change the following width inside the src automatically我正在尝试自动更改 src 内的以下宽度

src="http://tools.fxempire.com/sidebar-quotes.php?instruments=5,10,1,2,4,3&width=375&height=410&text_color=333&text_hover_color=082F60"

but i don't get any idea to do it但我不知道这样做

is there a way to do it??what I found in internet is all using either link or button to change the src有没有办法做到??我在互联网上发现的都是使用链接或按钮来更改 src

well because of the Access-Control-Allow-Origin you can not do it with ajax, you could do it with php, but i dont know legal/illegal this would be.好吧,因为 Access-Control-Allow-Origin 你不能用 ajax 来做,你可以用 php 来做,但我不知道这会是合法的/非法的。

By using iframe it works this way, but only really sluggish cause the iframe needs a lot time to load...:通过使用 iframe 它以这种方式工作,但只是非常缓慢,因为 iframe 需要很多时间来加载...:

$(window).bind('load resize',function(){
    var windowH = $(window).height(),
        windowW = $(window).width();

    $('#iframe').attr('src','//tools.fxempire.com/sidebar-quotes.php?instruments=5,10,1,2,4,3&width='+windowW+'&height='+windowH+'&text_color=333&text_hover_color=082F60');
    $('#iframe').css({'height':windowH+'px', 'width':windowW+'px'});    
})

http://jsfiddle.net/ebjsd8xx/3/ http://jsfiddle.net/ebjsd8xx/3/

EDIT: I changed the code a little bit for better performance but, well - the graph still loads freaking slow, but it's the same on their preview..编辑:为了更好的性能,我稍微更改了代码,但是,嗯 - 图表加载速度仍然非常慢,但在预览中是一样的..

var resizeTimer;

$(window).load(function(){
   $(window).trigger('resize');
});

$(window).resize(function(e){
    var windowH = $(window).height(),
        windowW = $(window).width();

    clearTimeout(resizeTimer);
    resizeTimer = setTimeout(function() {
       $('#iframe').attr('src','http://tools.fxempire.com/sidebar- quotes.php?instruments=5,10,1,2,4,3&width='+windowW+'&height='+windowH+'&text_color=333&text_hover_color=082F60');

       $('#iframe').css({'height':windowH+'px', 'width':windowW+'px'}); 

     }, 250);


})

http://jsfiddle.net/ebjsd8xx/7/ http://jsfiddle.net/ebjsd8xx/7/

Maybe like this?也许像这样?

jQuery(document).ready(function(){
    var iframe=jQuery('iframe'),/** target iFrame */
        src=iframe.attr('src'); /** src of iFrame */

    /** check viewport size */
    if (window.innerWidth<945){

        /** remove width value from src */
        src=src.replace(/(&width=[0-9]+)/,'');

        /** add new width */
        src+='&width=275';

        /** PROFIT */
        iframe.attr('src',src);
    }
});

On jsfiddlejsfiddle 上

For me the simplest way to select a src given the viewport size was with a small script, where your iframe has the id="vid"对我来说,在给定视口大小的情况下选择src的最简单方法是使用一个小脚本,其中您的 iframe 具有id="vid"

<script>
  var w = window.matchMedia("(max-width: 700px)");
  var vid = document.getElementById("vid");
  
  if (w.matches) {
    vid.src = "media/test_1.mp4";
  } else {
    vid.src = "media/test_2.mp4";
  }

</script>

That will pick the source when you load the page the first time, but wouldn't change it if you resize the viewport later, foor that you need to listen to resize event ( this might help ).这将在您第一次加载页面时选择源,但如果您稍后调整视口大小,则不会更改它,因为您需要侦听调整大小事件( 这可能会有所帮助)。

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

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