247errorsnl 247errorsnl - 2 months ago 10
HTML Question

jQuery How to check if the current position of a div is equal to

I am stuck. I am working on a jQuery game and I need to know the current position of a object and check if it equals to 0. I tried different things but none of them worked. Here is the code:

function movePoint() {
$( '.point' ).each( function() {
if($(this).attr('id').position().left == 0) {
console.log("Yay!");
}
$( this ).css( 'left', $( this ).position().left - ( speed + (roundsClicked / 10) ) );
console.log("Left position of your element - " + $(this).position().left);
});
}

Answer

just change your code to this

function movePoint() {
   $( '.point' ).each( function() {
       if($(this).position().left == 0) {// if($(this).css('left') == '0px') 
          console.log("Yay!");
       }
       $( this ).css( 'left', $( this ).position().left - ( speed + (roundsClicked / 10) ) );
       console.log("Left position of your element - " + $(this).position().left);
   });
}