html5 - 移动端如何获取屏真正的宽度,更方便采用viewport进行对页面进行缩放
ringa_lee
ringa_lee 2017-04-17 11:40:56
[HTML讨论组]

平时在切面的时候,我的是按照640宽度切页面,然后将屏幕宽度与640进行比较,获取缩放比例,在iPhone中是好好的,但是在有些安卓机不就不行了,比如酷派,我获取到他的宽度是760像素,我在写js的时候,已经做了判断,大于640的时候,就按照640的呈现,但是在酷派中,虽然按照640呈现的,但是页面完全适配酷派的手机。超出屏幕宽度了。是不是手机的实际的像素跟屏幕的宽度不同啊,js中我是这样获取屏幕宽度的window.screen.width

ringa_lee
ringa_lee

ringa_lee

全部回复(2)
黄舟

可以参考下这个 移动屏幕适配

PHPz

利用screen.width并结合viewport标签:

To reliably use JavaScript to retrieve the actual width of the screen a visitor is using , nowadays you have to use the screen.width property in combination with a viewport meta-tag with width=device-width.

文章: Javascript - get device width / screen width on mobile devices

SO上的相关问题:Get the device width in javascript

You can get the device screen width via the screen.width property.
Sometimes it's also useful to use window.innerWidth (not typically found on mobile devices) instead of screen width when dealing with desktop browsers where the window size is often less than the device screen size.

Typically, when dealing with mobile devices AND desktop browsers I use the following:

var width = (window.innerWidth > 0) ? window.innerWidth : screen.width;
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号