So I've got a element that needs to sit underneath an anchored toolbar. And the amount of top-padding necessary to get it underneath said toolbar changes based on the width of the viewport. So I've developed a formula to determine the percent of padding-top needed at any point in time from 2560px down to 320px, the largest and smallest viewport widths our software caters to, but I'm struggling to actually get it into a css style.
This is what I'm working with right now.
getTopPadding = (): void => {
var x: number = $(window).width(),
paddingPercent: number = ((0.007589 * (2560 - x)));
if (paddingPercent < 5) {
paddingPercent += 5;
}
$('#appendHere').css('padding-top', paddingPercent);
};
So I've got this formula that takes my current width and pumps out what the padding-top should be. So for example, on a width of 320px, padding needs to be,
padding-top: 17%;
paddingPercent will calculate the number 17 just fine, but if I pass in just paddingPercent as a value for the .css() method it comes out like this
padding-top: 17px;
So what I'm trying to find out is, is there a way to designate whether the value I'm passing in should be px, em, vw, or %? I've tried
$('#appendHere').css('padding-top', (paddingPercent + '%'));
thinking perhaps taking the number and adding a string to the end would add the % symbol I need and stringify the value because you can pass a string in to the .css() method. But thinking about it more I realized the reason it accepts a string is more for things like specifying color etc, not for what I'm doing. I thought about type casting and doing
Number((paddingPercent + '%'));
But that would come back with NaN, so I'm not sure where to go from here. Hoping someone can offer some insight.