-1

我有一个表格标题的像素宽度。单击功能时,我需要将像素转换为百分比。

<th field="Column1" id="Column1" noresize="true" width="100px">
    <label id="Column1" onclick="getCustomGridColName(this.id,'inner__fghgh');" style="cursor:pointer; font-family: Times; font-size:12pt;">
        Column1
    </label>
</th>
4

2 回答 2

13

百分比是一个相对值。

100px您这样的值无法得出百分比。

您需要具有像 screenWidth (假设)1366px这样的相对值,以便您获得该值的百分比。

var pixels = 100;
var screenWidth = window.screen.width;
var percentage = ( screenWidth - pixels ) / screenWidth ; // 0.92%
于 2013-04-04T09:25:18.527 回答
0
//Transform pixel in percentage
/* - totalpx: Total of pixels depending on the width or height, could be 1920.0 or 1080.0, 1280.0 or 720.0, etc...
     respectively.
   - px: the pixel desidered to be converted.
   - ItIsHeight: boolean to know if is axis x or y ot make the correct calculation.
   - BaseOfConversionWidth & BaseOfConversionHeight: the base of conversion from the original pixel are take it, if
     the design is HD ready then will be 1280.0 and 720.0, if is full HD then will be 1920.0 and 1080.0, etc... */
public Double TransformPercentage(Double Totalpx, Double px, boolean ItIsHeight, double BaseOfConversionWidth, double BaseOfConversionHeight) {
    if (ItIsHeight)
        return (px / BaseOfConversionHeight) * Totalpx;
    else
        return (px / BaseOfConversionWidth) * Totalpx;
}
于 2019-08-19T07:43:25.263 回答