使用javascript将像素转换为百分比 [英] Convert pixel to percentage using javascript

查看:199
本文介绍了使用javascript将像素转换为百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表头的像素宽度。点击一个函数,我需要将像素转换为百分比。

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


解决方案

百分比是一个相对值。

有一个值,例如 100px ,您无法创建百分比。

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

  var pixels = 100; 
var screenWidth = window.screen.width;
var percentage =(screenWidth - pixels)/ screenWidth; // 0.92%


I have a table header's width in pixel. on click of a function i need to convert pixel to 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>

解决方案

percentage is a relative value.

with one value like 100px you cannot make a percentage.

you need to have relative value like screenWidth (for suppose) 1366px so that you will get percentage of that value.

var pixels = 100;
var screenWidth = window.screen.width;
var percentage = ( screenWidth - pixels ) / screenWidth ; // 0.92%

这篇关于使用javascript将像素转换为百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆