How i can Get from Div Top and add to this div top + 25?


I tried to add this Div top: + 25 but I wasted 6 hours and no have more ideas how I can achieve it.

I tried like this:

var testDiv = document.getElementById("GAME_NPC").offsetTop;
var testDiv2 = document.getElementById("GAME_CHARACTER").offsetTop += "32px";

//alert(testDiv2);
if (testDiv == testDiv2) {}

But this doesn't work.

            var testDiv = document.getElementById("GAME_NPC").offsetTop;
        var testDiv2 = document.getElementById("GAME_CHARACTER");
        var top = testDiv2.offsetTop;

        top += 32; //that one is int

        testDiv2.style.top = top + "px"; // and that one should be string with 'px'

        if(testDiv2 == testDiv){


        }

this also doesn't work

test.css

#GAME_CHARACTER {
display: block;
position: absolute;
width: 32px;
height: 48px;
left: 32px;
top: 32px;
z-index:200;
}

#GAME_NPC {
    display: block;
    position: absolute;
    left: 0;
    top:120px;
    width: 96px;
    height: 128px;
    background: url(http://xxx/npc.gif) no-repeat center;
}

html code:

            <DIV id="GAME_CHARACTER" class="left-stand" style="background-image: url(http://xxx/character.png);"></DIV>

        <DIV id="GAME_MAP2"><DIV id="GAME_NPC"></DIV></DIV>

This will all 25px to whatever the current top is, which should achieve your desired effect.

function addTop(element, px){
    element.style.top = (Number(element.style.top.replace(/\D/g, "")) + px) + "px";
}
addTop(document.getElementById("GAME_CHARACTER"), 25);

Grab element offsetTop, update it and set updated value to whatever element you want. Remember append "px" when updating it.

var testDiv = document.getElementById("my-div-id");
var top = testDiv.offsetTop;

top += 32; //that one is int

testDiv.style.top = top + "px"; // and that one should be string with 'px'