Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert pixel to percent using jQuery

Tags:

jquery

css

i would like to know if there's a way to convert pixel to percent... I have a code which i get the value of full width successfully, but the only thing to keep the object in center, i must need to use percent.

Here's what i use on getting the value of width which i need to convert after to percent.

$(document).ready(function(){
    var sp = $('.slide'),
        winWidth = $('#slides').width();
        console.log(winWidth);

    sp.css('width', winWidth);
});

Thanks for the help.

Here's the updated fiddle link. *note: what i wanted here was to set the ".slide" container become fluid to make the object still in center spot. Feel free to modify my fiddle. Right now im using my given jQuery code to get the exact width of the "#slide" in percent to pixel ('.slide'). Then as of that, try to resize your browser to see the output of the slide frame. Im using SlidesJS plugin FYI.

like image 916
iMarkDesigns Avatar asked Dec 15 '22 17:12

iMarkDesigns


1 Answers

percent = ($("#your_element").width() / $("#some_parent_id").width()) * 100

Where $("#your_element") is element you want to count width in %, where $("#some_parent_id") is element relative to which you want to count width.

like image 183
Daniil Ryzhkov Avatar answered Dec 29 '22 00:12

Daniil Ryzhkov