为什么我的JavaScript调整大小函数不能在初始页面加载时运行?

时间:2012-05-10 13:59:54

标签: javascript jquery mobile

这是一个简单的脚本,用于调整iframe,图像和嵌入以适应移动设备的窗口。我遇到的问题是它不会在页面加载时运行,而只会在刷新或调整窗口大小时运行。我已经在Firefox和Chrome中测试过,所以我猜测它不是浏览器的问题,而是代码。

我在下面的脚本之前从谷歌和jQuery Mobile 1.1.0加载jQuery 1.7.2。

 $(window).bind("load", resizeMobile);
 $(window).bind("resize", resizeMobile);

  function resizeMobile() {
    var h = $(window).height();
    var w = ($(window).width() - 30);
    var max_size = w;
    $("embed, iframe, img").each(function (i) {
      if ($(this).height() > $(this).width()) {
        var h = max_size;
        var w = Math.ceil($(this).width() / $(this).height() * max_size);
      } else {
        var w = max_size;
        var h = Math.ceil($(this).height() / $(this).width() * max_size);
      }
      var url = $(this).attr('src');
      //handle ih embeds
      if (url.indexOf("/player/embed.html") > 0) {
        $(this).attr("height", h).attr("width", w);
        $(this).attr("src", $(this).attr("src")); //reload iframe to set new width and height
      } else {
        //$(this).attr("height", h).attr("width", w);
        $(this).css({
          height: h,
          width: w
        });
      }
    });
  }

  $(document).ready(resizeMobile);

0 个答案:

没有答案