如何使用JavaScript更改图像的坐标?

问题描述 投票:0回答:1

因此,我一直在尝试创建平台游戏,但是我对HTML知之甚少。我有这段代码,我试图找出如何更改其坐标,以便完成平台游戏。

var logo=document.createElement("img"); 
logo.src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"; 
document.body.appendChild(logo);
//I am using the Google Logo so you can see the image.
javascript image
1个回答
0
投票

如果您是指页面中任何元素的位置,则可以使用以下代码来实现;

var logo=document.createElement("img"); 
logo.src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"; 
document.body.appendChild(logo);
logo.style.position = "absolute";
logo.style.left = x_pos+'px';
logo.style.top = y_pos+'px';

或作为函数来执行,因此您可以将其附加到诸如onmousedown之类的事件上

function placeDiv(x_pos, y_pos) {
  var logo=document.createElement("img"); 
logo.src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"; 
document.body.appendChild(logo);
  logo.style.position = "absolute";
  logo.style.left = x_pos+'px';
  logo.style.top = y_pos+'px';
}
© www.soinside.com 2019 - 2024. All rights reserved.