1

I'm trying make this image move 16px every click, but it's not moving. I've tried this:

But that only moves it once. So I tried something different that I found online.

Here is my code:

Html:

document.getElementById('player').style.left += "-16px";

function left() {
   var left = parseInt(player.style.left);
   player.style.left = (left +16) + "px";
}
<img id="player" src="characters/prisoner-down.png"></img>
<img id="leftbtn" src="icons/left.png" onclick="left();"></img>

Edit: if it's useful to know, there are gonna be three more buttons for the other directions and the image I want to move is in another DIV from the image that moves it. On my editor it says that the symbol ` is invalid, so I'd like to avoid it.

CaptainYulef
  • 21
  • 1
  • 7
  • `document.getElementById('player').style.left += "-16px";` – epascarello Jun 28 '22 at 16:44
  • Did you style the element so it is capable of being positioned? – epascarello Jun 28 '22 at 16:52
  • @epascarello I don't know what you mean exactly by that, but it can be moved so I think yes. – CaptainYulef Jun 28 '22 at 16:55
  • Does this answer your question? [moving an element in JS](https://stackoverflow.com/questions/44617545/moving-an-element-in-js) – Heretic Monkey Jun 28 '22 at 17:03
  • @HereticMonkey It doesn't seem to work either. – CaptainYulef Jun 28 '22 at 17:13
  • 1
    What is "It"? There are 3 answers to that question. The accepted answer has a snippet which shows it working... – Heretic Monkey Jun 28 '22 at 17:16
  • @HereticMonkey I don't how I would apply the code to my code, as I think it was made for moving something different. So I don't think _any_ of the answers will work. – CaptainYulef Jun 28 '22 at 17:59
  • So you didn't try any of them, because they changed `top` instead of `left`? – Heretic Monkey Jun 28 '22 at 18:27
  • @HereticMonkey No, that would be rather dumb. I tried them, and I am still trying to apply them to my code right now. It _might_ work, but it isn't working as of yet. – CaptainYulef Jun 28 '22 at 18:35
  • @HereticMonkey Non of the answers worked for me. – CaptainYulef Jun 28 '22 at 18:48
  • [Edit] your question, showing how you implemented the answers and what "doesn't work" means (are there errors? does it not do what you expect? what debugging have you done?) – Heretic Monkey Jun 28 '22 at 18:53
  • I probably should've specified. This how I tried to add one of them (all the others were practically implemented the same way, just with different code in places) `const myBox = document.querySelector("player"); function left() { myBox.style.top = myBox.getBoundingClientRect().top - 5 + 'px'; // parse the string to number, subtract 5, and add 'px' console.log(myBox.style.top); }` – CaptainYulef Jun 28 '22 at 19:03

2 Answers2

1

You can create a function that get the direction of the translation and the number of pixels to increment, then return a function that set style.transform of the element clicked, using a closure you can update the amount of pixels to translate, try this:

function transform(side, n) {
    let translatePixels = 0;

    if (side == 'right') {
        return function(e) {
            translatePixels += n;
            e.target.style.transform = `translate(${translatePixels}px)`;
        }
    }
    if (side == 'left') {
        return function(e) {
            translatePixels -= n;
            e.target.style.transform = `translate(${translatePixels}px)`;
        }
    }                              
}

const translateRight = transform('right', 16);
const translateLeft = transform('left', 16);
<img onclick="translateRight(event);" src="https://images.rappi.com.ar/products/2311265-1622567743906.png?d=200x200&e=webp"></img>

<img onclick="translateLeft(event);" src="https://cdn.domestika.org/c_fill,dpr_auto,f_auto,h_256,pg_1,t_base_params,w_256/v1499705651/avatars/000/536/178/536178-original.jpg?1499705651" style="right: 0; position:absolute"></img>
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35
  • 1
    After clicking a few times `transform: translate(16px) translate(16px) translate(16px) translate(16px) translate(16px) translate(16px) translate(16px) translate(16px) translate(16px) translate(16px) translate(16px) translate(16px) translate(16px) translate(16px) translate(16px) translate(16px);` I think there would be a better way lol – epascarello Jun 28 '22 at 16:45
  • @epascarello you're right, i will update the answer – sonEtLumiere Jun 28 '22 at 16:46
  • Is there I way I can make this move a different image from the one I'm pressing? – CaptainYulef Jun 28 '22 at 16:50
  • @CaptainYulef yes, check out my answer updated – sonEtLumiere Jun 28 '22 at 17:19
  • @sonEtLumiere I made it so that it moves the img left and right, but when I press the left button then the the right (or the other way around) it jumps to where I was the the last time I pressed the button. – CaptainYulef Jun 28 '22 at 19:38
0

Give position property to the img which you want to move.

Like position: relative;, position: absolute; etc.

By positioning them you can use top, left, right and top properties.

don't use position: static; on that img cause above property has no effect on static element.

document.getElementById('player').style.left = "-16px";

function left() {
   let left = parseInt(player.style.left);
   player.style.left = `${parseInt(left+16)}px`;
}
img{
  /*important for using left, right, top and bottom properties*/
  position: relative;
}
<img id="player" src="https://i.postimg.cc/44pXvSwD/pngwing-com.png"></img>
<img id="leftbtn" src="https://i.postimg.cc/FRkMDYvr/Pngtree-right-arrow-flat-multi-color-3777297.png" onclick="left();"></img>
Extraterrestrial
  • 600
  • 1
  • 6
  • 19