The top property is set using the jQuery .css functi...">

JQuery css "+ =" not working

I have a <div> :

 <div class="ball" style="top: 1px;"></div> 

The top property is set using the jQuery .css function as follows:

 $( ".ball" ).css( { top : var_top } ); 

However, when I try to increase it using this syntax, it does not work:

 $( ".ball" ).css( { top : "+=" + var_top } ); 

I even tried it in the Firebug console as follows:

 $( ".ball" ).css( { top : "+=7px" } ); $( ".ball" ).css( { top : "+=7" } ); 

but none of them work.

+4
source share
2 answers

you can try this

 $( ".ball" ).css({ top : "+=" + var_top + 'px'}); 

NOTE: If .ball has position:absolute , then its parent must have position:relative ;

And it seems that your code also works

+3
source

I think you need to restore the old value, increase it and reassign:

 var topPosition = $( ".ball" ).css('top'); topPosition += increment; $( ".ball" ).css('top', topPosition); 
0
source

Source: https://habr.com/ru/post/1414154/


All Articles