简体   繁体   English

在 html/javascript 中刷新页面时,如何更改图像的位置?

[英]How can i change the position of an image when page is refreshed in html/javascript?

I need this one image to be in a random position on the screen every time i refresh the page, i found a javascript code that could work but i just don't know how to make it work.每次刷新页面时,我都需要这张图片在屏幕上的随机位置,我发现了一个可以工作的 javascript 代码,但我只是不知道如何使它工作。

this is my html这是我的 html

  <body>
    <p>
      find rufus
    </p>
    <img src="molerat.jpg" id="molerat"></img>
  </body>

javascript: javascript:

console.log();
function onloadFunction() {
  var amount = 10;
  var arrayIDs = "molerat"; 

  for (i=1;i<=amount;i++) {

   var element = document.getElementById(arrayIDs[i-1]);
   var positionInfo = element.getBoundingClientRect();
   var imgHeight = positionInfo.height;
   var imgWidth = positionInfo.width;


   var screenWidth = window.innerWidth;
   var screenHeight = window.innerHeight;

   var imgLeft = Math.floor(Math.random() * (screenWidth - imgWidth));
   var imgTop= Math.floor(Math.random() * (screenHeight - imgHeight));

   document.getElementById(arrayIDs[i-1]).style.top = imgTop+"px";
   document.getElementById(arrayIDs[i-1]).style.left = imgLeft+"px";
  }
}

css: css:

body{
  background-color:white;
  font-family: monospace;
  font-size: 50px;
}

img {
  position: absolute;
}

arrayIDs is not an array. arrayIDs 不是数组。

var arrayIDs = "molerat"; needs to be var arrayIDs = ["molerat"];需要是var arrayIDs = ["molerat"];

Also you only have 1 image so the amount needs to be set to 1此外,您只有 1 张图像,因此数量需要设置为 1

 function onloadFunction() { var amount = 1; var arrayIDs = ["molerat"]; for (i=1;i<=amount;i++) { var element = document.getElementById(arrayIDs[i-1]); var positionInfo = element.getBoundingClientRect(); var imgHeight = positionInfo.height; var imgWidth = positionInfo.width; var screenWidth = window.innerWidth; var screenHeight = window.innerHeight; var imgLeft = Math.floor(Math.random() * (screenWidth - imgWidth)); var imgTop= Math.floor(Math.random() * (screenHeight - imgHeight)); document.getElementById(arrayIDs[i-1]).style.top = imgTop+"px"; document.getElementById(arrayIDs[i-1]).style.left = imgLeft+"px"; } } onloadFunction();
 body{ background-color: white; font-family: monospace; font-size: 50px; } img { position: absolute; }
 <p> find rufus </p> <img src="https://picsum.photos/200" id="molerat" />

Your onloadFunction needs to be invoked, when the page loads.当页面加载时,您的onloadFunction需要被调用。 If you're using jQuery in your project, then $(document).ready(...) is the way to go:如果您在项目中使用jQuery ,那么$(document).ready(...)是您的最佳选择:

$(onloadFunction)

Otherwise you might want to check this question for pure JavaScript $(document).ready(...) alternatives.否则你可能想检查这个问题是否有纯 JavaScript $(document).ready(...)替代方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JavaScript刷新页面时如何更改图像? - how to change the image when a page is refreshed using JavaScript? 刷新页面时如何继续使用 javascript 添加的动画? - How can I continue the animations I added with javascript when the page is refreshed? 刷新页面后如何保存以前的数据? - How can I keep previous datas when the page is refreshed? 刷新时如何更改网页的标题? - How can I have the title of a webpage change when it is refreshed? 当我使用Javascript单击导航菜单链接时,如何更改页面的背景图像? - How can i change background image of the page when i click on navigation menu link using Javascript? 刷新后html页面中的元素状态可能没有变化? - It's possible the element in html page's status don't change when I refreshed? 刷新页面时如何停止页面加载图像 - How to stop page loading image when page is refreshed 我可以从javascript告诉我的页面是否刷新了吗? - Can I tell from javascript whether my page was hard refreshed? 有没有人知道即使页面已刷新,我如何保留Javascript变量? - Does anyone know how can I keep the Javascript variable even though the page has been refreshed? 如何导航到页面,但要确保目标页面将被刷新 - how can I navigate to page but ensure that the target page will be refreshed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM