Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend Existing CSS style by percent- % using JQuery

I am trying to extend an existing css style by 1%, using the following code, but instead its being increment in pixels instead of %.So instead of giving - 7%, its giving me it as 7px.

$("#label").css("top", "+=1%");

The top most div is of size - width: 100%; and the label top : 6% which is already defined.

Any idea/suggestion on this would be really helpful

like image 220
WonderHeart Avatar asked Mar 26 '26 13:03

WonderHeart


2 Answers

You can try to get the current top property value, and then add 1%;

var currentTop = $("#label").css("top");
var topAdded = parseInt(currentTop.replace("%","")) + 1;
$("#label").css("top", topAdded + "%");
like image 132
jmartins Avatar answered Mar 28 '26 02:03

jmartins


You can do this:

var top = parseInt($("#label").css("top").replace("%", ""));
top ++; 
$("#label").css("top", top+"%")

Update:

For one line statement you can do this:

 $("#label").css("top", (parseInt($("#label").css("top").replace("%", "")+1)+"%")
like image 33
hamed Avatar answered Mar 28 '26 02:03

hamed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!