首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用javascript让html元素移动到任意位置?

如何使用javascript让html元素移动到任意位置?
EN

Stack Overflow用户
提问于 2019-03-10 16:54:52
回答 4查看 838关注 0票数 0

我尝试使用以下代码:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
    <head>
    <style> 
    #myDIV {
        position: absolute;
        background-color: coral;
        color: white;
    }
    </style>
    </head>
    <body>

    <p>Click the "Try it" button to position the DIV element 100 pixels from 
    the 
    right edge:</p>
    <p><strong>Note:</strong> If the position property is set to "static", the 
    right 
    property has no effect.</p>

    <button onclick="myFunction()">Try it</button>

    <div id="myDIV">
     <h1>myDIV</h1>
    </div>

    <script>
    function myFunction() {
        document.getElementById("myDIV").style.right = Math.floor(Math.random() 
    * 1) 
    + 100.px  ;
    }
    </script>

    </body>
</html>

但是它仍然不能工作!不过,我没怎么尝试过。

EN

回答 4

Stack Overflow用户

发布于 2019-03-10 17:09:03

一个基本的解决方案

这会将div放置到视口中任意位置(x/y)的任意位置。我添加了一个transition来让事情变得更流畅一些。

代码语言:javascript
复制
const btn = document.querySelector("button");
const height = document.documentElement.clientHeight;
const width = document.documentElement.clientWidth;
const box = document.getElementById("myDIV");

btn.addEventListener("click", () => {
  let randY = Math.floor((Math.random() * height) + 1);
  let randX = Math.floor((Math.random() * width) + 1);
  box.style.top = randY + "px";
  box.style.right = randX + "px";
});
代码语言:javascript
复制
#myDIV {
  --div-width: 100px;
  position: absolute;
  background-color: coral;
  color: white;
  transition: .5s top, .5s right;
  top: 0;  
  right: calc(100% - var(--div-width));
  width: var(--div-width);
}

button {
  z-index: 1;
  position: absolute;
}
代码语言:javascript
复制
<button>Try it</button>

<div id="myDIV">
  <h1>myDIV</h1>
</div>

jsFiddle

更好的解决方案

动画topleft/right的性能不是很好,应该用transform代替。使用transform来移动东西会激活硬件加速。该系统将使用其GPU来制作动画,这将与本机应用程序的性能一样流畅。如果你正在制作一个你已经提到的游戏,你会想让它像移动环境中的应用程序一样。

代码语言:javascript
复制
const btn = document.querySelector("button");
const height = document.documentElement.clientHeight;
const width = document.documentElement.clientWidth;
const box = document.getElementById("myDIV");

btn.addEventListener("click", () => {
  let randY = Math.floor((Math.random() * height) + 1);
  let randX = Math.floor((Math.random() * width) + 1);
  box.style.transform = `translate(${randX}px, ${randY}px)`;
});
代码语言:javascript
复制
#myDIV {
  --div-width: 100px;
  position: absolute;
  background-color: coral;
  color: white;
  transition: 0.5s transform;
  top: 0;  
  width: var(--div-width);
}

button {
  z-index: 1;
  position: absolute;
}
代码语言:javascript
复制
<button>Try it</button>

<div id="myDIV">
  <h1>myDIV</h1>
</div>

jsFiddle

票数 3
EN

Stack Overflow用户

发布于 2019-03-10 17:00:47

您的代码几乎是正确的。只需要在JS中做一些小小的更改。看看我的解决方案。希望这能有所帮助!

代码语言:javascript
复制
function myFunction() {
    document.getElementById("myDIV").style.right = Math.random() * window.innerWidth + 'px';
}
代码语言:javascript
复制
#myDIV {
    position: absolute;
    background-color: coral;
    color: white;
}
代码语言:javascript
复制
<p>Click the "Try it" button to position the DIV element a random amount of 
pixels from the 
right edge:</p>
<p><strong>Note:</strong> If the position property is set to "static", the 
right property has no effect.</p>

<button onclick="myFunction()">Try it</button>

<div id="myDIV">
  <h1>myDIV</h1>
</div>

票数 0
EN

Stack Overflow用户

发布于 2019-03-10 17:04:29

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
<style> 
    #myDIV {
    position: absolute;
    background-color: coral;
    color: white;
}
</style>
</head>
<body>

<p>Click the "Try it" button to position the DIV element a random amount of 
pixels from the 
right edge:</p>
<p><strong>Note:</strong> If the position property is set to "static", the 
right property has no effect.</p>

<button onclick="myFunction()">Try it</button>

<div id="myDIV">
  <h1>myDIV</h1>
</div>

<script>
function myFunction() {
    document.getElementById("myDIV").style.right = Math.floor(Math.random()*(1000-0+1)+0)+"px";  ;
}
</script>

</body>
</html>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55086016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档