【问题标题】:dynamic image loading google chrome动态图像加载谷歌浏览器
【发布时间】:2016-05-22 02:54:53
【问题描述】:

我有一个关于在我的网站上运行 JavaScript 以及如何加载图像的新手问题。

基本上,我开发了一个页面,该页面将为小企业主生成一张简单的屏幕发票。所有者可以转到该页面,单击一个按钮,然后他们会被要求提供各种输入以填充发票。他们被要求输入他们的标志图片、他们所做的工作列表(以及他们收取的费用)、底部的联系信息等等。

这是原始的,我知道有很多专业的服务可以做到这一点,但我拥有一家小企业,想学习 HTML 和 JS。

我的特殊问题是用户输入 URL 时徽标图像的显示方式。用户单击开始按钮后,JS 会提示输入徽标 URL。然后脚本询问用户他们是否对显示的图像(即形状和大小)感到满意。

当我在 IE11 上加载网站时,只要用户输入 URL,图像就会显示。这样,他们可以立即看到它并决定他们是否对此感到满意。问题是,当我在 Google Chrome 上运行它时,直到整个发票生成程序运行后,图像才会加载。换句话说,直到组成程序的所有 JS 脚本都被执行后,图像才会显示。

不用说,这限制了我的用户一次尝试显示他们的徽标,因为他们直到程序结束后才会在屏幕上看到它。为什么它会立即在 IE 11 中显示而不是 Chrome,我怎样才能让它立即在 Chrome 中显示(如果可能)?

页面链接是Generate Invoice

这是提示输入图像、显示并根据用户喜欢重新调整大小的 JS 代码。

var url = prompt("Enter the URL for a picture of your logo:");

// create the IMG element to display the logo
var image = document.createElement("IMG");

// assign the user-input url to the variable holding the IMG element
image.src=url;

// Create a variable to assign the DIV to (by ID)
var imageLogo = document.getElementById("addLogoAndMessage");

// Clear out the DIV (i.e. make the button go away)
imageLogo.innerHTML = "";

// Append the image to the variable associated with the DIV
imageLogo.appendChild(image);

// custom width for CE logo is 220px
var logoWidth = prompt("Set a width for your logo.");
var logoWidth1 = logoWidth + "px";
imageLogo.appendChild(image).style.width = logoWidth1;

// custom height for CE logo is 200px
var logoHeight = prompt("Set a height for your logo. To maintain aspect ratio, just hit Enter.");
var logoHeight1 = logoHeight + "px";
if (logoHeight == "" || logoHeight == "undefined") {
    imageLogo.appendChild(image).style.height = "auto";
} else {
    imageLogo.appendChild(image).style.height = logoHeight1; 
}

var sizeOK = prompt("Are you happy with the shape and size of your logo (y/n)?");

while (sizeOK != "n" && sizeOK != "y") {
  sizeOK = prompt("Are you happy with the shape and size of your logo (y/n)?");
}

while (sizeOK == "n") {
  var logoWidth = prompt("Set a new width for your logo.");
  var logoWidth1 = logoWidth + "px";
  imageLogo.appendChild(image).style.width = logoWidth1;

  var logoHeight = prompt("Set a new height for your logo. To maintain aspect ratio, just hit Enter.");
  var logoHeight1 = logoHeight + "px";
  if (logoHeight == "" || logoHeight == "undefined") {
      imageLogo.appendChild(image).style.height = "auto";
  } else {
      imageLogo.appendChild(image).style.height = logoHeight1; 
  }

  // Ask after every width,height set so user can stop when they like the image
  sizeOK = prompt("If you're still not happy with your logo, enter 'n'. Otherwise, just hit Enter.");
}

感谢您的任何想法或意见。

【问题讨论】:

    标签: javascript html google-chrome internet-explorer-11


    【解决方案1】:

    Chrome 可能会在执行文档重排之前等待图像完成加载(= 在更新布局之前)。由于 JavaScript 的执行速度比图像可能加载的速度快得多,因此在 JS 完成之前您不会看到图像。

    您可以尝试将onload 事件附加到完成加载时触发的图像。所以,使用

    image.onload = imageHasLoaded;
    image.src = url;
    

    然后将其余代码放在一个单独的函数中

    function imageHasLoaded() {
        //the rest of your code, to be executed once the image is displayed
    }
    

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2013-03-05
      • 2013-07-08
      • 2014-01-26
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多