Increment css top property using jquery

前端 未结 5 1368
醉话见心
醉话见心 2020-12-24 02:03

I have the following div :

What is the best way to

5条回答
  •  天命终不由人
    2020-12-24 02:09

    You can use the parseFloat function to get only the initial numeric part and convert it from a string to a number. For example:

    var n = $('#new');
    n.css('top', (parseFloat(n.css('top')) + 10) + 'px');
    

    As a side note, if you want to change multiple elements' positions all at once, that could be:

    $('.someClass').css('top', function(i, v) {
        return (parseFloat(v) + 10) + 'px';
    });
    

    But if you are just trying to animate an element over time, take a look at jQuery's .animate method.

提交回复
热议问题