原文連接 http://azq.space/blog/bigcan-lesson-1/javascript
http://www.jianshu.com/p/ae5e2ec91786/
首先我先說兩個概念,第一,響應式:一個頁面在不一樣尺寸的瀏覽器中不一樣的表現;第二,自適應:一個頁面在任何尺寸瀏覽器下表現一致.這裏只說自適應。css
zoom方式:html
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><!--必須,放在head標籤中--> <script> var _SCALE_ = 1; _SCALE_ = function (){ var deviceWidth = document.documentElement.clientWidth, scale = deviceWidth / 640; scale = scale > 1 ? 1 : scale; document.body.style.zoom = scale; return scale;//留着給js用 }();//在body下執行便可 </script>
以上是經過css屬性zoom來縮放頁面,設計稿寬度爲640(如下設計稿尺寸都爲640),因此這樣的好處就是直接按照原來設計稿的尺寸用px來寫css,不過這個顯然不夠高大上,頁面有時會不清晰,可是經過和%的配合也能夠快速的構建你的h5頁面java
css3的scale方式:webpack
此方式bug衆多,不適合新手,也不適合老司機,因此 略略略css3
meta-viewport-scale方式:web
經過比例來對initial-scale等屬性進行重置,少部分瀏覽器不識別重置npm
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><!--必須,放在head標籤中--> <script> var _SCALE_ = 1; _SCALE_ = function (){ var deviceWidth = document.documentElement.clientWidth, scale = deviceWidth / 640; scale = scale > 1 ? 1 : scale; var metaEl = document.querySelector('meta[name="viewport"]'); metaEl.setAttribute('content', 'width=640,initial-scale=' + scale + ',maximum-scale=' + scale + ', minimum-scale=' + scale + ',user-scalable=no'); return scale; }(); </script>
這種方式和zoom的方式相似也是採用縮放,css也是按照原始設計稿來寫,也能解決1px的問題,暫時沒有發現什麼問題,也許會有不清晰的現象,若是有同窗在使用這種方法的時候遇到問題請及時告訴我,趕忙填坑gulp
這是個高清適配的方法,除了重置meta失效的瀏覽器瀏覽器
dpr,rem,meta
var dpr, rem, scale; var docEl = document.documentElement; var fontEl = document.createElement('style'); var metaEl = document.querySelector('meta[name="viewport"]'); dpr = window.devicePixelRatio || 1; rem = docEl.clientWidth * dpr / 10; scale = 1 / dpr; // 設置viewport,進行縮放,達到高清效果 metaEl.setAttribute('content', 'width=' + dpr * docEl.clientWidth + ',initial-scale=' + scale + ',maximum-scale=' + scale + ', minimum-scale=' + scale + ',user-scalable=no'); // 設置data-dpr屬性,留做的css hack之用 docEl.setAttribute('data-dpr', dpr); // 動態寫入樣式 docEl.firstElementChild.appendChild(fontEl); fontEl.innerHTML = 'html{font-size:' + rem + 'px!important;}'; // 給js調用的,某一dpr下rem和px之間的轉換函數 window.rem2px = function(v) { v = parseFloat(v); return v * rem; }; window.px2rem = function(v) { v = parseFloat(v); return v / rem; }; window.dpr = dpr; window.rem = rem;
而後爲了方便咱們能夠採用less或者sass、scss來對咱們的css樣式進行管理,這裏簡單的介紹less
.px2rem(@name, @px){ @{name}: @px / 64 * 1rem; } .px2px(@name, @px){ @{name}: round(@px / 2) * 1px; [data-dpr="2"] & { @{name}: @px * 1px; } // for mx3 [data-dpr="2.5"] & { @{name}: round(@px * 2.5 / 2) * 1px; } // for 小米note [data-dpr="2.75"] & { @{name}: round(@px * 2.75 / 2) * 1px; } [data-dpr="3"] & { @{name}: round(@px / 2 * 3) * 1px } // for 三星note4 [data-dpr="4"] & { @{name}: @px * 2px; } } /*簡單的圓角半徑*/ .border-radius (@radius: 5px) { -webkit-border-radius: @radius; -moz-border-radius: @radius; border-radius: @radius; } /*四角的半徑定製*/ .border-radius-custom (@topleft: 5px, @topright: 5px, @bottomleft: 5px, @bottomright: 5px) { -webkit-border-radius: @topleft @topright @bottomright @bottomleft; -moz-border-radius: @topleft @topright @bottomright @bottomleft; border-radius: @topleft @topright @bottomright @bottomleft; } /*方塊陰影 Box Shadow*/ .box-shadow (@x: 0px, @y: 3px, @blur: 5px, @alpha: 0.5) { -webkit-box-shadow: @x @y @blur rgba(0, 0, 0, @alpha); -moz-box-shadow: @x @y @blur rgba(0, 0, 0, @alpha); box-shadow: @x @y @blur rgba(0, 0, 0, @alpha); } /*元素過渡效果 Transition*/ .transition (@prop: all, @time: 1s, @ease: linear) { -webkit-transition: @prop @time @ease; -moz-transition: @prop @time @ease; -ms-transition: @prop @time @ease; transition: @prop @time @ease; } /*轉換/旋轉 Transform*/ .transform (@rotate: 90deg, @scale: 1, @skew: 1deg, @translate: 10px) { -webkit-transform: rotate(@rotate) scale(@scale) skew(@skew) translate(@translate); -moz-transform: rotate(@rotate) scale(@scale) skew(@skew) translate(@translate); -ms-transform: rotate(@rotate) scale(@scale) skew(@skew) translate(@translate); transform: rotate(@rotate) scale(@scale) skew(@skew) translate(@translate); } /*線性漸變*/ .gradient (@origin: left, @start: #ffffff, @stop: #000000) { background-color: @start; background-image: -webkit-linear-gradient(@origin, @start, @stop); background-image: -moz-linear-gradient(@origin, @start, @stop); background-image: -ms-linear-gradient(@origin, @start, @stop); background-image: linear-gradient(@origin, @start, @stop); } /*快速漸變*/ .quick-gradient (@origin: left, @alpha: 0.2) { background-image: -webkit-linear-gradient(@origin, rgba(0,0,0,0.0), rgba(0,0,0,@alpha)); background-image: -moz-linear-gradient(@origin, rgba(0,0,0,0.0), rgba(0,0,0,@alpha)); background-image: -ms-linear-gradient(@origin, rgba(0,0,0,0.0), rgba(0,0,0,@alpha)); background-image: linear-gradient(@origin, rgba(0,0,0,0.0), rgba(0,0,0,@alpha)); } /*鏡像效果*/ .reflect (@length: 50%, @opacity: 0.2){ -webkit-box-reflect: below 0px -webkit-gradient(linear, left top, left bottom, from(transparent), color-stop(