我已经想出了居中和调整大小的问题,但我仍然无法让onLoad
正常工作。有人有主意吗?我认为onLoad
应该等待图像被完全加载之前触发代码。因为这是现在,它调整img为下一个img,并在它加载之前淡入,因此前一个图像再次淡入。一旦节目一次完成,它完美的工作,所以显然它不等待图像加载之前完全加载imageLoad()
。图片()onLoad不等待图像加载
<div id="slideShow">
<div id="slideShowImg" style="display:none; margin-right:auto; margin-left:auto;">
</div>
<div id="slideShowThumbs">
</div>
<script type="text/javascript">
loadXMLDoc('http://www.thehoppr.com/hopspots/82/82.xml', function() {
var slideShow = document.getElementById('slideShow');
var items = [];
var nl = xmlhttp.responseXML.getElementsByTagName('image');
var i = 0;
var t;
var slideShowImg = document.getElementById('slideShowImg');
var slideShow = document.getElementById('slideShow');
var maxHeight = 300;
var maxWidth = 800;
var imgNode = new Image();
function image() {
var nli = nl.item(i);
var src = nli.getAttribute('src').toString();
var width = parseInt(nli.getAttribute('width').toString());
var height = parseInt(nli.getAttribute('height').toString());
imgNode.onLoad = imageLoad();
imgNode.src = src;
imgNode.height = height;
imgNode.width = width;
imgNode.setAttribute("style", "margin-right:auto; margin-left:auto; display:block;");
var ratio = maxHeight/maxWidth;
if (imgNode.height/imgNode.width > ratio) {
// height is the problem
if (imgNode.height > maxHeight) {
imgNode.width = Math.round(imgNode.width * (maxHeight/imgNode.height));
imgNode.height = maxHeight;
}
} else {
// width is the problem
if (imgNode.width > maxHeight) {
imgNode.height = Math.round(imgNode.height * (maxWidth/imgNode.width));
imgNode.width = maxWidth;
}
}
}
function imageLoad() {
slideShowImg.appendChild(imgNode);
Effect.Appear('slideShowImg', {
duration: 1
});
t = setTimeout(nextImage, 7000);
}
function nextImage() {
slideShowImg.setAttribute("style", "display:none");
if (i < nl.length - 1) {
i++;
image();
} else {
i = 0;
image();
}
}
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//XML Loaded, create the slideshow
alert(xmlhttp.responseText);
image();
}
});
</script>
足够有趣它似乎确实在Chrome浏览器,而不是IE或Firefox。很明显,在onLoad甚至被分配之前脚本正从缓存中拉出来?在显示图片时预载图片可能是我最好的选择。如果任何人都可以为我提供一些示例代码,可以加快这一点,将不胜感激,我是相当新的Javascript。现在已经很晚了,我正在上床睡觉。 – Jacob 2011-05-10 08:12:51