1. 程式人生 > >滷煮節流實現移動端自適應開發

滷煮節流實現移動端自適應開發

/* 自執行匿名函式,隨著螢幕大小的變化而變化 */
(function () {
    // deicePixelRatio :裝置畫素
var scale = 1 / devicePixelRatio;
//設定meta 壓縮介面 模擬裝置的高解析度
document.querySelector('meta[name="viewport"]').setAttribute('content', 'initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no');
//debounce 為節流函式,自己實現。或者引入underscoure即可。 /* Begin */ var _ = function(obj) { if (obj instanceof _) return obj; if (!(this instanceof _)) return new _(obj); this._wrapped = obj; }; _.now = Date.now || function() { return new Date().getTime(); }; _.debounce = function(func, wait, immediate) { var
timeout, args, context, timestamp, result; var later = function() { var last = _.now() - timestamp; if (last < wait && last >= 0) { timeout = setTimeout(later, wait - last); } else { timeout = null; if (!immediate) { result = func.apply(context,
args); if (!timeout) context = args = null; } } }; return function() { context = this; args = arguments; timestamp = _.now(); var callNow = immediate && !timeout; if (!timeout) timeout = setTimeout(later, wait); if (callNow) { result = func.apply(context, args); context = args = null; } return result; }; }; /* End */ var reSize = _.debounce(function () { var deviceWidth = document.documentElement.clientWidth > 750 ? 750 : document.documentElement.clientWidth; //按照750畫素下字型為100px的標準來,得到一個字型縮放比例值 7.5 document.documentElement.style.fontSize = (deviceWidth / 7.5) + 'px'; console.log(document.documentElement.style.fontSize); }, 50); window.onresize = reSize; })();