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}`);