gpt4 book ai didi

javascript - 为什么我的 javascript 动态图像放置不像我的硬编码版本那样工作?

转载 作者:行者123 更新时间:2023-11-27 23:18:15 25 4
gpt4 key购买 nike

我已经成功创建了一个 html/javascript 文档,该文档使用硬编码“<div>”和“<img>”将几乎透明的图像 (t01.png) 覆盖在相同大小的背景图像 (map.png) 上标签。

我想使用 javascript createElement 命令代替硬编码标签来复制结果,但显示的页面将第二张图片放在背景图片下方的页面下方,而不是按需要覆盖它。

我需要更改什么才能使动态图像放置与硬编码版本一样工作?

硬编码版本:

<html>
<head>

<script LANGUAGE="JavaScript" type="text/javascript">
var iImage = 0;
var imageFiles = ["./map.png","./t01.png"]
var numImages = 2;

function imageLoad() {
for(iImage = 0; iImage < numImages; iImage++) {
document.images[iImage].src = imageFiles[iImage];
}
}
</script>
</head>

<body onLoad="imageLoad();">
<div id="imageBox" STYLE="position: absolute; top:0px; height: 1px;">
<div ID="frame0" STYLE="position: absolute; top: 0em; left: 0em;">
<img src="./map.png" border="1">
</div>
<div ID="frame1" STYLE="position: absolute;top: 0em; left: 0em;">
<img src="./t01.png" border="1">
</div>
</div>
</body>
</html>

动态放置版本

<html>
<head>

<script LANGUAGE="JavaScript" type="text/javascript">
var iImage = 0;
var imageFiles = ["./map.png","./t01.png"]
var numImages = 2;

function imageLoad() {
placeImage();
for(iImage = 0; iImage < numImages; iImage++) {
document.images[iImage].src = imageFiles[iImage];
}
}

function placeImage()
{
for(iImage = 0; iImage < numImages; iImage++) {

// Create div for this image.
var thisDiv = document.createElement("div");
var thisDivID = "frame" + iImage;
thisDiv.id = thisDivID;
thisDiv.position = "absolute";

// Create image.
var thisImage = document.createElement("img");
thisImage.src=imageFiles[iImage];

// Add this image to this div.
thisDiv.appendChild(thisImage);

// Add this div to the imageBox div.
document.getElementById("imageBox").appendChild(thisDiv);

}
}

</script>
</head>

<body onLoad="imageLoad();">
<div id="imageBox" STYLE="position: absolute; top:0px; height: 1px;"></div>
</body>
</html>

Background layer--map.png Mostly transparent layer--t01.png

最佳答案

您没有为动态放置版本中的元素提供任何样式,这是这里的问题

在你的代码中试试这个

thisDiv.style.position="absolute";
thisDiv.style.top="0em";
thisDiv.style.left="0em";

document.getElementById("imageBox").style.top="0px";
document.getElementById("imageBox").style.height="1px";
document.getElementById("imageBox").style.position="absolute";

并为您在静态硬编码版本中也为其提供样式的其他必需元素提供这样的样式。

关于javascript - 为什么我的 javascript 动态图像放置不像我的硬编码版本那样工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42265722/

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