Home Web Front-end JS Tutorial Example of JavaScript implementation of automatic scaling of mobile pages according to mobile phone screen resolution

Example of JavaScript implementation of automatic scaling of mobile pages according to mobile phone screen resolution

Aug 20, 2017 am 10:06 AM
javascript js cell phone

这篇文章主要介绍了移动端页面按手机屏幕分辨率自动缩放的js代码,通过阻止浏览器的默认行为各方面分析缩放的功能实现,具体操作步骤大家可查看下文的详细讲解,感兴趣的小伙伴们可以参考一下。

 手机的屏幕有大有小,移动web最好做成响应式布局,也就是自适应屏幕,没有固定宽高,这样的话,在所有手机上都可以正常显示。关于移动端页面按手机屏幕分辨率自动缩放的js,先附上代码


<script>
  var phoneWidth = parseInt(window.screen.width);
  var phoneHeight = parseInt(window.screen.height);
  var phoneScale = phoneWidth/750;//除以的值按手机的物理分辨率
  var ua = navigator.userAgent;
  if (/Android (\d+\.\d+)/.test(ua)) {
   var version = parseFloat(RegExp.$1);
   // andriod 2.3
   if (version > 2.3) {
    document.write(&#39;<meta name="viewport" content="width=device-width,initial-scale=&#39;+phoneScale+&#39;,minimum-scale=&#39;+phoneScale+&#39;,maximum-scale=&#39;+phoneScale+&#39;,user-scalable=no">&#39;);
    // andriod 2.3以上
   } else {
    document.write(&#39;<meta name="viewport" content="width=device-width,user-scalable=no">&#39;);
   }
   // 其他系统
 } else {
   document.write(&#39;<meta name="viewport" content="width=device-width, initial-scale=&#39;+phoneScale+&#39;,minimum-scale=&#39;+phoneScale+&#39;,maximum-scale =&#39;+phoneScale +&#39;,user-scalable=no,">&#39;);
  }
</script>
Copy after login

做手机端页面最头疼的就是物理分辨率和逻辑分辨率的转换了,当拿到设计图的时候,图基本都是按物理分辨率来设计的,一般常用的为640(iphone5/5s)、750(iphone6/6s),而谷歌等浏览器采用小手机模式浏览页面的时候,上面的值为逻辑分辨率,调试的时候很难把控页面样式,在手机端的样式也会因此大乱,在页面头部加入以上一段js之后,在手机端就可以正常显示了,

var phoneScale = phoneWidth/750;

除以的为设计图设计的页面宽度,750是按iphone6来设计(根据自己使用需求来修改),即让页面的 放大比率=屏幕的逻辑分辨率/物理分辨率,从而达到适应手机的效果。

(注意,有时候页面加了这段代码在调试的时候,切记刷新,刷新过后就会按手机缩放比例显示)

概念解析:

phys.width:一般我们所指的宽度width即为phys.width,物理宽度(物理分辨率)

device-width:又称为css-width,设备宽度(逻辑分辨率)

其中我们可以获取phys.width通过document.documentElement.clientWidth;

而获取css-width通过 window.screen.width获取。

所以这里 phoneWidth(逻辑分辨率) = parseInt(window.screen.width);

如iphone6的phys.width为750px,而css-width为375px。

明白一个浏览器默认行为。

试想,浏览器如果把电脑端的980px的网页展现在宽度为750px的iphone6手机屏上,势必会放不下,手机端横向会出现滚动条,怎么阻止这种情况呢,很简单,浏览器默认一个虚拟窗口,不同浏览器有不同的虚拟窗口宽度的默认值如:safari iphone:980px;

opera:850px;

Andriod webkit:800px;

IE:974px;

然后会把这个980px虚拟窗口装进宽度为750px的iphone6中,当然这样的话必须缩放,这就是为什么在手机中展现电脑端页面没有出现横向滚动条,而且字迹明显变小的原因。

initial-scale=&#39;+phoneScale+&#39;,minimum-scale=&#39;+phoneScale+&#39;,maximum-scale=&#39;+phoneScale+&#39;
Copy after login

这段代码切记要指定 initial-scale=***,在安卓系统中,不指定默认的nitial-scale=***,只指定最小和最大缩放值,也可以正常显示,但是safari浏览器则会失效,对于宽度是100%的页面, 则显示为页面的30%左右的宽。

target-densitydpi=device-dpi
Copy after login

WebKit内核默认按照160的DPI来排版。假如设备真实DPI是480,宽度是1080,在WebKit会按160DPI,360宽度来排版。排版结束后在放大到1080宽。

所以当取window.innerwidth之类的值的时候,取的是WebKit实际排版宽度360,而不是1080.

target-densitydpi=device-dpi可以强制内核以480DPI排版,使画面更精细,window.innerwidth也将为屏幕宽度1080.

但WebKit从去年开始取消了对target-densitydpi的支持。(所以现在不建议写该属性了)

想实现target-densitydpi=device-dpi的效果有什么方法?提交此次补丁的WebKit开发者说可以用responsive images 和 CSS device units来替代。

The above is the detailed content of Example of JavaScript implementation of automatic scaling of mobile pages according to mobile phone screen resolution. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

OPPO A3x mobile phone released: equipped with MediaTek Dimensity 6300, 4+128GB version priced at 1199 yuan OPPO A3x mobile phone released: equipped with MediaTek Dimensity 6300, 4+128GB version priced at 1199 yuan Jul 30, 2024 am 01:27 AM

According to news on July 29, in addition to the 1,799 yuan OPPO A3 Vibrant Edition mobile phone, OPPO also launched an A3x (PKD130), available in three colors: starlight white, dark night purple, and cloud feather pink. According to @Perfectly arranged digital, OPPOA3x seems to be an offline model. The main difference from the active version is that the rear lens is replaced with a 32+2MP dual camera. The summary pricing is as follows: 4+128GB version 1199 yuan 6+128GB version 1499 yuan 8+256GB version 1999 yuan 1. Equipped with MediaTek Dimensity 6300 processor, equipped with LPDDR4X memory and UFS2.2 flash memory, supports 2TB storage expansion, adopts 6.67-inch "sunshine screen" , with 1600×720 resolution, 1

Samsung Galaxy S25 Ultra mobile phone leaked: 6.86 inches, horizontal screen-to-body ratio 94.1% Samsung Galaxy S25 Ultra mobile phone leaked: 6.86 inches, horizontal screen-to-body ratio 94.1% Aug 17, 2024 pm 01:49 PM

According to news on August 17, the source @ibinguniverse posted on Weibo today, stating that the exact size of Apple iPhone 16 Pro Max is 6.88 inches, and the exact size of Galaxy S25 Ultra is 6.86 inches. Both can be regarded as 6.9 inches. Sources indicate that the Samsung Galaxy S25 Ultra has a narrower body and a wider screen than the S24 Ultra, with a horizontal screen-to-body ratio of 94.1%, while the S24 Ultra’s horizontal screen-to-body ratio is 91.5%. Fenye checked the relevant Weibo of the source. He also commented on the newly exposed photos of iPhone 16 Pro Max and believed that it was wrong to be close to a micro-curve. The phone is actually a straight screen + 2.5D glass.

'Tandem OLED' has been popularized by Apple. How is it better than OLED? 'Tandem OLED' has been popularized by Apple. How is it better than OLED? Aug 19, 2024 am 04:42 AM

Although Apple has been criticized for its lack of innovation in recent years, Apple has not always stood still. At least in terms of hardware design, with the support of the high unit prices of Apple products, its engineers can easily try some new technologies without having to consider too much cost issues. For example, iPad Pro, as Apple's favorite "display technology" test field, iPad Pro has been at the forefront of display technology for portable smart devices from miniLED in 2021 to tandem OLED in 2024. Although the iPad Pro is not the first portable smart device equipped with a miniLED screen (MSI released a miniLED laptop a year earlier than Apple), when you compare the parameters of the two, you will quickly realize that they are not the same

Hynix is ​​the first to demonstrate UFS 4.1 flash memory: based on V9 TLC NAND particles Hynix is ​​the first to demonstrate UFS 4.1 flash memory: based on V9 TLC NAND particles Aug 09, 2024 pm 03:33 PM

According to news on August 9, at the FMS2024 Summit, SK Hynix demonstrated its latest storage products, including UFS4.1 universal flash memory that has not yet officially released specifications. According to the official website of the JEDEC Solid State Technology Association, the latest UFS specification currently announced is UFS4.0 in August 2022. Its theoretical interface speed is as high as 46.4Gbps. It is expected that UFS4.1 will further improve the transmission rate. 1. Hynix demonstrated 512GB and 1TBUFS4.1 general-purpose flash memory products, based on 321-layer V91TbTLCNAND flash memory. SK Hynix also exhibited 3.2GbpsV92TbQLC and 3.6GbpsV9H1TbTLC particles. Hynix shows off V7-based

Released on August 1, physical photos of Xiaomi POCO M6 Plus 5G mobile phone and POCO Buds X1 headphones exposed Released on August 1, physical photos of Xiaomi POCO M6 Plus 5G mobile phone and POCO Buds X1 headphones exposed Jul 30, 2024 pm 02:29 PM

According to news on July 30, source Yogesh Brar posted a tweet on the X platform yesterday (July 29), sharing physical photos of Xiaomi POCOM6 Plus 5G mobile phone and POCO Buds X1 headphones. The official announcement of the two products will be released on August 1. Xiaomi POCOM6 Plus 5G mobile phone sources said that Xiaomi POCOM6 Plus 5G mobile phone will be equipped with a 6.8-inch LCD screen, equipped with Qualcomm Snapdragon 4Gen2AE processor, equipped with a 108-megapixel camera on the back, and a 5030mAh capacity battery. As previously reported, this phone is available in three colors: purple, black, and silver. It is roughly the same as the standard POCOM6 phone, but the LED flash ring is relatively more prominent. POCOB

The first 1.5K under-screen camera! Nubia Z70 Ultra is here: the world's first Snapdragon 8 Gen4 true full-screen phone The first 1.5K under-screen camera! Nubia Z70 Ultra is here: the world's first Snapdragon 8 Gen4 true full-screen phone Aug 19, 2024 pm 03:47 PM

According to news on August 19, Nubia has been adhering to the true full-screen design since the release of Z50 Ultra, and has been continuously exploring the field of proactive photography under high-pixel screens. Today, digital blogger Wisdom Pikachu broke the news that the Nubia Z70 Ultra, which will be released in the second half of this year, will debut with 1.5K under-screen camera technology, which is the highest-resolution UDC solution in the industry so far. It is reported that ZTE’s under-screen proactive solution has advanced to the sixth generation. The latest under-screen proactive solution is available in the Nubia Z60 Ultra and Red Magic 9S Pro series. The screen resolution is 2480x1116, which is between 1080P and 1.5K resolution. This time Nubia will break through the limitations of existing resolutions and set a new benchmark in the industry.

To run Google's Gemini Nano AI model locally, the Samsung Galaxy S25 Ultra phone was revealed to be equipped with 16GB of memory To run Google's Gemini Nano AI model locally, the Samsung Galaxy S25 Ultra phone was revealed to be equipped with 16GB of memory Jul 31, 2024 pm 05:55 PM

According to news on July 31, the source @ibinguniverse posted a tweet on the Equipped with 16GB of memory. Samsung mobile phone memory capacity update Samsung has launched 16GB memory on Galaxy S20 Ultra and Galaxy S21 Ultra mobile phones. Starting from Galaxy S22 Ultra, including the latest flagship Galaxy S24 Ultra mobile phone, the memory capacity of Samsung mobile phones is capped at 12GB. It is reported that the upcoming Samsung Galaxy S25 and Galaxy S25+ will use 12GB LPDD

Apple and Huawei both wanted to make a buttonless phone, but Xiaomi made it first? Apple and Huawei both wanted to make a buttonless phone, but Xiaomi made it first? Aug 29, 2024 pm 03:33 PM

According to a report from Smartprix, Xiaomi is developing a buttonless mobile phone codenamed "Suzaku". According to this news, this mobile phone codenamed Zhuque will be designed with an integrated concept, use an under-screen camera, and be equipped with Qualcomm Snapdragon 8gen4 processor. If the plan does not change, we are likely to see its arrival in 2025. When I saw this news, I thought I was back in 2019 - at that time, Xiaomi released the Mi MIX Alpha concept phone, and the surround-screen button-less design was quite amazing. This is the first time I have seen the charm of a buttonless mobile phone. If you want a piece of "magic glass", you must first kill the buttons. In "The Biography of Steve Jobs", Jobs once expressed that he hoped that the mobile phone could be like a piece of "magic glass".

See all articles