javascript
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
(function () { var layout = document.documentElement.clientWidth / 16; var styleNode = document.createElement('style'); // 建立style標籤 styleNode.innerHTML = "html{font-size : " + layout + "px!important"; // 將根標籤的font-size置爲佈局視口的寬/16 document.head.appendChild(styleNode); //將style標籤添加到head中 })()
(function () { var targetW = 640 // 設計圖寬度 var scale = document.documentElement.clientWidth / targetW // 理想視口的寬度 / 設計圖寬 var meta = document.querySelector("meta[name='viewport']") meta.content = 'initial-scale=' + scale + ',minimum-scale=' + scale + ',maximum-scale='+scale+',user-scalable=no' })()
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no,maximum-scale=1.0,minmul-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>1物理像素</title> <style> * { margin: 0; padding: 0; } #test { width: 16rem; height: 1px; background-color: black; margin-top: 30px; } </style> </head> <body> <div id="test"></div> <script> // rem 適配 // 淘寶的作法,經過適配角度解決1物理像素的問題 window.onload = function () { (function () { var dpr = window.devicePixelRatio || 1 // 像素比 var layout = document.documentElement.clientWidth // 佈局視口寬度 var styleNode = document.createElement('style') styleNode.innerHTML = "html { font-size:" + layout * dpr / 16 + "px!important}" document.head.appendChild(styleNode) var scale = 1 / dpr var meta = document.querySelector('meta[name="viewport"]') meta.content = "width=device-width, initial-scale=" + scale + ",user-scalable=no,maximum-scale=" + scale + ",minimum-scale=" + scale })() } </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no,maximum-scale=1.0,minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>1物理像素</title> <style> * { margin: 0; padding: 0; } #test { width: 100%; height: 1px; background-color: black; margin-top: 5rem; } @media only screen and (-webkit-device-pixel-ratio:2) { #test { transform: scaleY(0.5); } } @media only screen and (-webkit-device-pixel-ratio:3) { #test { transform: scaleY(0.33333); } } </style> </head> <body> <div id="test"></div> <script> (function () { var layout = document.documentElement.clientWidth / 16; var styleNode = document.createElement('style'); styleNode.innerHTML = "html{font-size :" + layout + "px!important;}" document.head.appendChild(styleNode) })() </script> </body> </html>
// 下面這行代碼能夠禁止手機瀏覽器全部的默認事件 document.addEventListener("touchstart", function (ev){ ev = ev || window.event // ev.cancelable 可用於查看是否能取消該事件的瀏覽器默認行爲 item.innerHTML = ev.cancelable; ev.preventDefault() })
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no,minimum-scale=1.0,maximum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script> window.onload = function () { // 阻止默認行爲 document.addEventListener('touchstart', function (ev) { ev = ev || window.event ev.preventDefault() }) // rem適配方案 !(function (flag) { var w = document.documentElement.clientWidth var styleNode = document.createElement('style') styleNode.innerHTML = "html{font-size: " + w / flag + "px!important}" document.head.appendChild(styleNode) })(16) } </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no,minimum-scale=1.0,maximum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> * { margin: 0; padding: 0; } body, html { line-height: 0; } .wrapper { width: 200px; height: 200px; background-color: skyblue; opacity: 0.5; position: absolute; top: 0; left: 0; } a { font-size: 12px; line-height: 18px; } </style> </head> <body> <div class="wrapper"></div> <a href="http://www.baidu.com">百度一下</a> <script> window.onload = function () { // 阻止默認行爲 document.addEventListener('touchstart', function (ev) { ev = ev || window.event ev.preventDefault() }) // rem適配方案 !(function (flag) { var w = document.documentElement.clientWidth var styleNode = document.createElement('style') styleNode.innerHTML = "html{font-size: " + w / flag + "px!important}" document.head.appendChild(styleNode) })(16) var wrapper = document.querySelector('.wrapper') wrapper.addEventListener('click', function () { wrapper.style.display = "none" }) // 全面禁止事件默認行爲會致使手機端a標籤沒法跳轉 // 移動端a標籤跳轉方案,解決誤觸 var aNodes = document.querySelectorAll("a") for (var i = 0; i < aNodes.length; i++) { // 該寫法使滑動的時候頁面不發生跳轉 aNodes[i].addEventListener('touchstart', function () { this.isMove = false; }) aNodes[i].addEventListener('touchmove', function () { this.isMove = true; }) aNodes[i].addEventListener('touchend', function () { if (!this.isMove) { window.location = this.href } }) } } </script> </body> </html>
<a href="tel:110">13579246801</a> <a href="mailto:123@qq.com">123@qq.com</a>
a { text-decoration: none; -webkit-tap-highlight-color: rgba(0,0,0,0); /* 解決連接高亮問題 */ }
input { width: 50px; height: 50px; border-radius: 5px; -webkit-appearance: none; /* iphone 只要有 border-radius 屬性就會變成一個圓 */ }