Kap*_*pys 17 javascript css jquery html5 css3
我想转换px
到vw
在JavaScript中,我该怎么办呢?
我一直在寻找它,但我没有找到任何东西.
所以,
1px→?VW
谢谢.
fca*_*ran 37
1px =(100vw/[document.documentElement.clientWidth] px)
例如 - 如果您的视口很500px
宽(等于定义100vw
),那么
1px =(100vw/500px)= 0.2vw
我曾经.clientWidth
以排除任何滚动条的计算
小智 16
function viewport_convert(px = 0, vw = 0, vh = 0){
if(px != 0){
if(vw){
return (100 * px / window.innerWidth);
} else {
return (100 * px / window.innerHeight);
}
} else if(vw != 0 && vh != 0){
var w_h_arr = [];
w_h_arr["width"] = Math.ceil((window.innerWidth * vw / 100));
w_h_arr["height"] = Math.ceil((window.innerHeight * vh / 100));
return w_h_arr;
} else if(vw != 0){
return Math.ceil((window.innerWidth * vw / 100));
} else if(vh != 0){
return Math.ceil((window.innerHeight * vh / 100));
}
}
Run Code Online (Sandbox Code Playgroud)