這是似乎引起問題的有問題的代碼:
player.style.height = playerH "px";
function trackXY(){
for(let i=0; i<pokeS.length; i ){
let pokeS = document.getElementsByClassName('pikaStyle' i);
let xPlayer = parseInt(player.style.left .5*player.style.width);
let yPlayer = parseInt(player.style.top .5*player.style.height);
let xpokeS = parseInt(pokeS.style.left .5*pokeS.style.width);
let ypokeS = parseInt(pokeS.style.top .5*pokeS.style.height);
let rPlayer = .5*parseInt(player.style.height);
let rpokeS = .5*parseInt(pokeS.style.height);
let dX = xpokeS - xPlayer;
let dY = ypokeS - yPlayer;
let distance = Math.sqrt((dX*dX) (dY*dY))
if(distance <= (rpokeS rPlayer)){
alert("collison!" pokeS.id);
player.style.height = parseInt(player.style.height) .5*parseInt(pokeS.style.height);
}
}
}
它要么回傳“樣式”無法讀取,要么“左”未定義。有人可以建議嗎?謝謝。
uj5u.com熱心網友回復:
您應該使用window.getComputedStyle()andgetPropertyValue來獲取樣式。你不能直接使用element.style.height,它不會回傳任何東西。
Window.getComputedStyle() 方法在應用活動樣式表并決議這些值可能包含的任何基本計算之后,回傳一個包含元素所有 CSS 屬性值的物件。
在您的代碼中,使用:
window.getComputedStyle(player).getPropertyValue('height')
window.getComputedStyle(pokeS).getPropertyValue('height')
例子:
let div = document.querySelector('div')
//return nothing
//console.log( div.style.height)
console.log(window.getComputedStyle(div).getPropertyValue('height'))
div{
height:50px;
}
<div></div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/412384.html
標籤:
