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 属性就会变成一个圆 */ }