使用javascript将像素转换为百分比

Rac*_*hel 0 javascript function pixel width percentage

我有一个表头的宽度(以像素为单位).点击一个功能我需要将像素转换为百分比.

<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>
Run Code Online (Sandbox Code Playgroud)

San*_*eep 9

百分比是相对值.

有一个值就像100px你不能做出一个百分比.

你需要有像screenWidth(假设)1366px这样的相对值,这样你才能获得该值的百分比.

var pixels = 100;
var screenWidth = window.screen.width;
var percentage = ( screenWidth - pixels ) / screenWidth ; // 0.92%
Run Code Online (Sandbox Code Playgroud)