【发布时间】:2015-10-04 10:43:54
【问题描述】:
最近(客户)要求我使用 js sn-p 根据分辨率更改元视口标签宽度。他们说这可能会提高网站移动/平板电脑版本的性能。但我拒绝相信它,因为在我看来,它看起来与视口标签宽度 = 设备宽度有些相同,我会说更糟。
这是sn-p:
function setviewport(){
if(window.innerHeight > window.innerWidth){
if(screen.height > screen.width){
deviceWidth = screen.width;
}else{
deviceWidth = screen.height;
}
}else{
if(screen.height > screen.width){
deviceWidth = screen.height;
}else{
deviceWidth = screen.width;
}
}
if(deviceWidth<=512){
$("#viewport").attr("content","width=480,initial-scale="+deviceWidth/480);
}else if(deviceWidth>=960){
$("#viewport").attr("content","width=1024");
}else{
$("#viewport").attr("content","width=768,initial-scale="+deviceWidth/768);
}
}
也许我什么都不懂,这确实是一个更好的选择,只是宽度=设备宽度。如果是,你能解释为什么会这样吗?它会提高性能吗?提前致谢。
【问题讨论】:
-
不要这样做,如果性能是你的理由。您正在更改客户端在整个渲染过程中使用的一些变量。在页面已经(至少是一方)渲染并有效地重复当前页面的渲染之后,您正在这样做。请注意,浏览器制造商正在努力优化渲染性能,我怀疑您的单个客户端是否比整个 Chrome 或 Firefox 团队更智能。如果您仍然想在您的网站中构建类似的东西,您应该使用科学的方法来衡量差异。
标签: javascript media-queries viewport