get CSS rule's percentage value in jQuery

后端 未结 12 1344
一个人的身影
一个人的身影 2020-11-22 08:22

Let\'s say the rule is as follows:

.largeField {
    width: 65%;
}

Is there a way to get \'65%\' back somehow, and not the pixel value?

12条回答
  •  清酒与你
    2020-11-22 08:51

    Convert from pixels to percentage using cross multiplication.

    Formula Setup:

    1.) (element_width_pixels/parent_width_pixels) = (element_width_percentage / 100)

    2.) element_width_percentage = (100 * element_width_pixels) / parent_width_pixels

    The actual code:

    
    

提交回复
热议问题