我有一些圖像座標,我想用它們在javascript上放一個小圖像。這可以用JavaScript來完成,或者我需要創建div並修改它的css屬性?順便說一句:圖像可以放在頁面的任何位置。在給出座標的另一個圖像上產生圖像
2
A
回答
0
這裏是一個完全工作代碼:Live Demo
CSS
.overlays{
position:absolute;
}
個JS
function showImage() {
// myImage : ID of image on which to place new image
var image = document.getElementById('myImage');
console.log(image.width);
margin = 20;
l = image.offsetLeft;
t = image.offsetTop;
w = image.width;
h = image.height;
// Location inside the image
offX = parseInt(Math.random() * w);
offY = parseInt(Math.random() * h);
if(offX > margin) offX -= margin;
if(offY > margin) offY -= margin;
l += offX;
t += offY;
var newImage = document.createElement("img");
newImage.setAttribute('src', '1.jpg');
newImage.setAttribute('class', 'overlays');
newImage.style.left = l + "px";
newImage.style.top = t + "px";
document.body.appendChild(newImage);
}
0
CSS:
#yourId{
position: absolute;
}
JS:
var img = document.getElementById('yourId'), // or any other selector
x = x, // your data
y = y; // your data
img.style.left = x+"px";
img.style.top = y+"px";
0
嘗試使用此方法將新的圖像上的父圖像的座標(X,Y)
:
var newImg = document.getElementById('newImg'), // your new (small) image
img = document.getElementById('parentImg'); // parent image
document.body.insertBefore(newImg, img); // Insert the new image before the image
document.body.insertBefore(img, newImg); // Then switch places (So the small image is after the big one in the DOM)
newImg.style.position = 'relative';
newImg.style.left = X + "px";
newImg.style.top = (Y - img.height)+"px"; // Substract the height of the source image to get the position relative to the top left.
雙insertBefore
是買入一個後插入新的圖像,首先插入它在大牌之前,然後在它前面移動大牌。
然後,這只是設置新圖像的座標相對於大圖像的問題。
Working Example(設置2秒後,顯示覆蓋以示區別)
相關問題
- 1. 用Xcode中的運動圖像的座標產生圖像
- 2. 解析圖像座標上的座標
- 3. 將預覽圖像座標映射到圖像圖像座標
- 4. 圖像出現在另一張圖像的上方/上方?
- 5. 將一個圖像放在android中的另一個圖像上
- 6. 在Processing中在另一個圖像上添加一個圖像?
- 7. 查找PDF中圖像的座標以替換爲另一個圖像
- 8. 如何在鼠標上改變另一圖像的圖像
- 9. 如何使圖像出現在另一圖像的懸停上?
- 10. 將鼠標懸停在一個圖像和另一個圖像出現
- 11. 在另一個給出的座標上添加一個div
- 12. 在另一個圖像上重疊一個圖像
- 13. Android:在另一個圖像上放置一個圖像
- 14. 在另一個圖像上添加一個圖像並獲取座標,使其可以拖動和刪除
- 15. 如何使用OpenCV查找圖像上另一點的座標
- 16. 懸停在圖像上,更改另一個圖像 - 多個第一圖像
- 17. 圖像在另一個圖像
- 18. xcode圖像座標
- 19. 將鼠標懸停在圖像變化的另一個圖像
- 20. 極座標位圖圖像
- 21. 將鼠標懸停在另一圖像上時顯示圖像
- 22. 座標圖像 - 圖像形式
- 23. 在緩衝圖像上查找座標
- 24. opencv在其他圖像上查找圖像座標
- 25. 從一個圖像淡出並淡入另一個圖像
- 26. 作爲懸停在另一個圖像上的透明圖像
- 27. 在android中的另一個圖像上添加圖像
- 28. 在另一個圖像IOS上的可拖動圖像Swift
- 29. 在Android的另一個圖像上粘貼圖像
- 30. 在另一個圖像(jQuery)的表面上繪製圖像
用JavaScript您可以創建任何你需要的DOM元素,也改變風格 –
沒錯,但如何改變風格的地方它在一個不固定的圖像的確切座標上? – Octavian
您想要在屏幕上放置任何想要的圖像,這意味着您必須將位置設置爲絕對位置。然後,您可以根據「頂部」,「左側」,「右側」,「底部」定義其位置。 –