gpt4 book ai didi

javascript 图像 src 更改事件(不是加载事件)

转载 作者:行者123 更新时间:2023-11-30 21:05:20 25 4
gpt4 key购买 nike

我正在尝试实现以下内容:用户单击一个小缩略图(低分辨率图像),并将大图像设置在同一来源。异步下载更高分辨率的图像,并在下载准备就绪时渲染为大图像。下载时,低分辨率图像旁边应该有一个指示器,表明正在下载更高分辨率的图片。此外,图像在下载后应始终从浏览器缓存中检索。

我尝试了以下方法(不是完整的解决方案,但更像是试验)

$('#bigphoto').on('load', function() {
console.log('loaded');
if ($('#bigphoto').attr('src').includes('type=low-res')) {
$('#loading').show();
} else {
$('#loading').hide();
}
});

function paivitaKuva(kuvaEl) {

var src = kuvaEl.getAttribute('src');
var r_src = kuvaEl.getAttribute('r-src');

$('#bigphoto').attr('src', src);
$('#bigphoto').attr('src', r_src);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id="bigphoto" src="http://www.viikonloppu.com/wp-content/uploads/2014/04/lotoflaughters.com_-619x428.jpg?c3bc1b" />

<img id="thumbnail" src="https://i.pinimg.com/736x/01/c0/1c/01c01c45cb997f16675c5a5825645ceb--funny-happy-birthdays-pictures-of.jpg" r-src="https://www.manitowoccranes.com/~/media/Images/news/2014/Potain-China-hi-res.jpg" onClick="paivitaKuva(this)" )
/>

<div id="loading">loading</div>

有几个问题:

  • load 事件在下载准备好之后触发,而不是在更改 src 时触发
  • 如果图像是从浏览器缓存中检索的,加载事件似乎不会始终一致地触发
  • 实际上有不同缩略图的列表和一张大照片。我在考虑如果用户快速更改照片会发生什么

如何使用 javascript 干净地实现它?

最佳答案

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Thumbnail View Sample</title>

<script type="text/javascript" language="javascript">

// Global variables
var loadingFlag = false;
var statusElem = null;
var hrElem = null;
var hrIMG = null;

function processSelection(tnElem)
{
var hrSrc = null;

try
{

if (!loadingFlag)
{
loadingFlag = true;

statusElem.innerHTML = "Loading...";

hrElem.src = tnElem.src;
hrElem.setAttribute("width", "64");
hrElem.setAttribute("height", "64");

hrSrc = tnElem.getAttribute("hrsrc");

loadHRImage(hrSrc);
}
else
{
alert("Currently loading another image.\r\nOnly one image can be loaded at a time.\r\nTry again, later.");
}
}
catch (e)
{
alert("processSelection Error: " + e.Message);
}
finally
{

}
}


function loadHRImage(url)
{
try
{
hrIMG.src = url;
}
catch (e)
{
alert("loadHRImage Error: " + e.Message);
}
finally
{

}
}


function setHRImage()
{
var error = false;

try
{
hrElem.src = hrIMG.src;

error = (hrIMG.width==0 || hrIMG.height==0);

if(!error)
{
hrElem.setAttribute("width", hrIMG.width.toString());
hrElem.setAttribute("height", hrIMG.height.toString());
}
}
catch (e)
{
error = true;
alert("setHRImage Error: " + e.Message);
}
finally
{
loadingFlag = false;
if (error) statusElem.innerHTML = "Loading...failed.";
else statusElem.innerHTML = "Loading...completed.";
}
}


</script>
</head>
<body>
<div id="theStatus" ><b>Select a Thumbnail Image to View the High Resolution version</b></div><hr/>
<table>
<tr>
<td style="vertical-align: top">
<table>
<tr><td><img id="tn0" alt="" src="https://i.pinimg.com/736x/01/c0/1c/01c01c45cb997f16675c5a5825645ceb--funny-happy-birthdays-pictures-of.jpg" width="64" height="64" hrsrc="https://www.manitowoccranes.com/~/media/Images/news/2014/Potain-China-hi-res.jpg" onclick="processSelection(this)" title="Click to view High Resolution version" /></td></tr>
<tr><td><img id="tn1" alt="" src="https://i.pinimg.com/736x/01/c0/1c/01c01c45cb997f16675c5a5825645ceb--funny-happy-birthdays-pictures-of.jpg" width="64" height="64" hrsrc="https://www.manitowoccranes.com/~/media/Images/news/2014/Potain-China-hi-res.jpg" onclick="processSelection(this)" title="Click to view High Resolution version" /></td></tr>
<tr><td><img id="tn2" alt="" src="https://i.pinimg.com/736x/01/c0/1c/01c01c45cb997f16675c5a5825645ceb--funny-happy-birthdays-pictures-of.jpg" width="64" height="64" hrsrc="https://www.manitowoccranes.com/~/media/Images/news/2014/Potain-China-hi-res.jpg" onclick="processSelection(this)" title="Click to view High Resolution version" /></td></tr>
</table>
</td>
<td></td>
<td>
<img id="hrImage" alt="" src="" />
</td>
</tr>
</table>
<script type="text/javascript" language="javascript">
// Initialize global variables
statusElem = document.getElementById("theStatus");
hrElem = document.getElementById("hrImage");
hrIMG = new Image();
hrIMG.addEventListener('loadend', setHRImage, false);
</script>
</body>
</html>

关于javascript 图像 src 更改事件(不是加载事件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46689244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com