5 回答

TA貢獻1818條經驗 獲得超7個贊
定義i并在方法j內部change(),以便單擊按鈕時可以隨機更新。
另外,您的代碼中存在拼寫錯誤position: absoulte,應更正為absolute
var b = document.querySelector("button");
b.addEventListener("click",change);
function change()
{
var i = Math.floor(Math.random()*500)+1;
var j = Math.floor(Math.random()*500)+1;
b.style.left = i+"px";
b.style.top = j+"px";
}
button{
display: block;
position: absolute;
}
<button>abc</button>

TA貢獻1811條經驗 獲得超6個贊
HTML :-
<body>
<div class="ctr">
<button class="button" id="movingbutton">Button</button>
</div>
</body>
CSS:-
#movingbutton{
margin-left: auto;
margin-right: auto;
display: block;
position: absolute;
left : 20px;
top : 50px;
}
body{
width : 100%;
}
.ctr{
width : 100%;
height : 100%;
}
JS:-
var b = document.querySelector("#movingbutton");
b.addEventListener("click",change);
function change()
{
let i =Math.abs(Math.floor(Math.random()*window.innerWidth-55))
let j = Math.abs(Math.floor(Math.random()*window.innerHeight-21));
console.log('here' , i ,j , b.style.left , b.style.top);
b.style.left = i+'px';
b.style.top = j + "px";
}
如果您愿意,可以在這里查看:實時示例鏈接
如果該按鈕超出 window.innerWidth 和 window.innerHeight,則需要再添加一個條件

TA貢獻1780條經驗 獲得超5個贊
您需要將隨機計算移至函數內change()
。
要將元素保留在其包含元素中,您可以使用getBoundingClientRect()
.?(并考慮按鈕的大小,以避免使用相同的按鈕在右側和底部重疊。)
const c = document.querySelector(".container");
const b = document.querySelector("button");
function change() {
? const
? ? { width: cWidth, height: cHeight } = c.getBoundingClientRect(),
? ? { width: bWidth, height: bHeight } = b.getBoundingClientRect(),
? ? i = Math.floor(Math.random() * (cWidth - bWidth)) + 1,
? ? j = Math.floor(Math.random() * (cHeight - bHeight)) + 1;
? b.style.left = i + "px";
? b.style.top = j + "px";
}
b.addEventListener("click", change);
.container {
? position: relative;
? height: 50vh;
? width: 50vw;
? background-color: lightgray;
}
button{
? position: absolute;
? display: block;
}
<div class='container'>
? <button type='button' id='shifty'>Click</button>
</div>

TA貢獻1802條經驗 獲得超5個贊
如果你想隨機移動一個按鈕,你可以使用簡單的.bind()。當鼠標在按鈕區域移動時,您也可以移動按鈕(無需單擊)。這是兩個代碼:
點擊代碼
var b = document.querySelector("#movingbutton");
b.addEventListener("click",change);
function change()
{
let i = Math.floor(Math.random()*500)+1;
let j = Math.floor(Math.random()*500)+1;
console.log('here' , i ,j , b.style.left , b.style.top);
b.style.left = i+'px';
b.style.top = j + "px";
}
#movingbutton{
margin-left: auto;
margin-right: auto;
display: block;
position: absolute;
left : 20px;
top : 50px;
}
body{
width : 100%;
}
.ctr{
width : 100%;
height : 100%;
}
<body>
<div class="ctr">
<button class="button" id="movingbutton">Button</button>
</div>
</body>
鼠標移動代碼
var b = document.querySelector("#movingbutton");
b.addEventListener("mousemove",change);
function change()
{
let i = Math.floor(Math.random()*500)+1;
let j = Math.floor(Math.random()*500)+1;
console.log('here' , i ,j , b.style.left , b.style.top);
b.style.left = i+'px';
b.style.top = j + "px";
}
#movingbutton{
margin-left: auto;
margin-right: auto;
display: block;
position: absolute;
left : 20px;
top : 50px;
}
body{
width : 100%;
}
.ctr{
width : 100%;
height : 100%;
}
<body>
<div class="ctr">
<button class="button" id="movingbutton">Button</button>
</div>
</body>
添加回答
舉報