Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何随机化图像的位置?如何复制粘贴相同的图像? 图像的位置不会改变 复制图像的代码错误_Javascript_Html_Css - Fatal编程技术网

Javascript 如何随机化图像的位置?如何复制粘贴相同的图像? 图像的位置不会改变 复制图像的代码错误

Javascript 如何随机化图像的位置?如何复制粘贴相同的图像? 图像的位置不会改变 复制图像的代码错误,javascript,html,css,Javascript,Html,Css,随机摩尔位置 img { padding: 20px; position: relative; top: position(); } 函数位置(){ 返回Math.floor(Math.random()*500)+“px”; } 常量mole=document.getElementsByTagName(“img”)[0]; 文件写入(摩尔) 首先,你不能调用css中的函数,这也是js中的函数 请查找下面的代码链接: HTML: JS: 如果您想在HTML文件中添加J

随机摩尔位置

img {
  padding: 20px;
  position: relative;
  top: position();
}


函数位置(){
返回Math.floor(Math.random()*500)+“px”;
}
常量mole=document.getElementsByTagName(“img”)[0];
文件写入(摩尔)


首先,你不能调用css中的函数,这也是js中的函数

请查找下面的代码链接:

HTML:

JS:


如果您想在HTML文件中添加JS,那么在脚本标记中使用JS代码

HTML宽度和高度属性不受支持;CSS无法调用JS函数。。。
<img src="mole.png" width=5% height=5%>


function position(){
  return Math.floor(Math.random()*500) + "px";
}

const mole = document.getElementsByTagName("img")[0]; 
document.write(mole)
<img src="mole.png" width=5% height=5%>
img {
  padding: 20px;
  position: relative;
}
function position(){
  return Math.floor(Math.random()*500) + "px";
}


const top = position();
const img = document.getElementsByTagName("img")[0];
img.setAttribute("style" ,`top:${top}`);