I want to move the picture from the right end of the window to the left. When I write the left and top values myself, it works, but I want to read these values, and not enter them myself.
The javascript code
window.addEventListener('keydown', f, false);
window.addEventListener('keyup', f, false);
var img = document.getElementById("img_main");
var left = 1020;
var topw = 50;
console.log(left);
console.log(topw);
function f(e) {
switch (e.keyCode) {
case 38:
topw -= 10;
img.style.top = topw + 'px';
break;
case 40:
topw += 10;
img.style.top = topw + 'px';
break;
case 39:
left += 10;
img.style.left = left + 'px';
break;
case 37:
left -= 10;
img.style.left = left + 'px';
break;
}
}
the CSS code
#img_main {
width: 300px;
height: 300px;
position: absolute;
left: 1020px;
top: 50px;
}
window.getComputedStyle()var style = window.getComputedStyle(img); var left=style.getPropertyValue('left'); var topw=style.getPropertyValue('top');I did it, but the block does not move.getPropertyValue()returns a string