来源:自学PHP网 时间:2015-04-14 14:51 作者: 阅读:次
[导读] 今天发现同事前段大牛Alex写了两个js文件 方便用于 已写好的web页面突然要支持手机的, 比如有个页面所有宽度是 640px 可引入这两个js文件之后再你的页面写一句话$(document) ready(functio...
今天发现同事前段大牛Alex写了两个js文件 方便用于 已写好的web页面突然要支持手机的, 比如有个页面所有宽度是 640px 可引入这两个js文件之后再你的页面写一句话
$(document).ready(function(){ sun.md.setViewPortContent({ initWidth : 640 }); });
之后便可以用手机看看了, 一般情况只需要很小的调整就可以了,但是有可能及特殊情况不适用, 欢迎有bug通过留言反馈
若有使用的情况特殊可以看下setViewPortContent这个方法, 主要就是根据手机类型设置它的viewport的属性 , 里面还有许多定义好的js常用方法
用此s文件如有bug等等问题带来利益损失概不负责 下面是js代码引入要有顺序 先引入sun.js 再引入sun.md.js, (这两个文件时在jquery下写的所以jquery必须最先引入)
sun.js var sun = sun || {}; /** * it is for alex to shortcut method * delete before online */ (function shortCut() { tojs = function(vmodel) { return ko.mapping.toJS(vmodel) }; log = function (){ console.log(arguments) }; write = function(txt){ var p = document.createElement('p'); var hr = document.createElement('hr'); p.innerHTML = txt; document.body.appendChild(hr); document.body.appendChild(p); }; })() sun.ajax = function() { var mime = { html: 'html', js: 'script', json: 'json', xml: 'xml', txt: 'text' } var _stringifyData = function(sType, oData) { var _data = oData; if ((sType.toUpperCase() === 'POST') && ( !! oData)) { _data = JSON.stringify(oData); } return _data; }; base = function(sUrl, sType, sDataType, oData, fnCallBack, isShlowLoading, isAsync) { var _data = oData, _isAsync = typeof isAsync === 'boolean' ? isAsync : true; if (sUrl.indexOf('?') === -1){ sUrl = sUrl + '?t=' + Math.random(); }else { sUrl = sUrl + '&t=' + Math.random(); } $.ajax({ async: _isAsync, type: sType, url: sUrl, data: _stringifyData(sType, oData), contentType: 'application/json', dataType: sDataType, beforeSend: function(XMLHttpRequest) { if (!!isShlowLoading) { } }, success: function(data, textStatus) { if (!!isShlowLoading) { } if(!_isAsync && (typeof fnCallBack === 'function')){ fnCallBack(data, textStatus); }; _data = data; }, error: function(XMLHttpRequest, textStatus, errorThrown) { if (!!isShlowLoading) { } _data = errorThrown; } }).done(function(data, textStatus, _self) { if (!!isShlowLoading) { } if (!!_isAsync && (typeof fnCallBack === 'function')) { fnCallBack(data, textStatus); } });; return _data; }; return { post: function(sPageUrl, oData, fnCallBack, isAsync) { if ((typeof oData === 'function') && (!fnCallBack)) { fnCallBack = oData; oData = null; } return base(sPageUrl, 'POST', mime.json, oData, fnCallBack, false, isAsync); }, getJSON: function(sPageUrl, oData, fnCallBack, isAsync) { if ((typeof oData === 'function') && (!fnCallBack)) { fnCallBack = oData; oData = null; } return base(sPageUrl, 'get', mime.json, oData, fnCallBack, false, isAsync); } } }(); sun.$ = function(query) { return document.querySelectorAll(query); }; // var __readyFuns = []; // function DOMReady(){ // for(var i=0,l=readyFuns.length;i
sun.md.js sun = sun || {}; sun.md = (function(global){ var self, _userAgent = navigator.userAgent, _rWins = /Windows/i, _rWP = /IEMobile/i, _rAndroid = /Android/i, _rIPhone = /iPhone/i; androidScreenPixelRatio = { pr : null, screenHeight : null, getScreenHeight : function() { var that = this; if (null == that.screenHeight) { that.screenHeight = (screen.width < screen.height) ? screen.height : screen.width; } return that.screenHeight; }, getDpi : function() { var that = this, dpi = 320; if (1200 <= that.getScreenHeight()) { dpi = "device-dpi"; } return dpi; }, getPixelRatio : function() { var that = this; if (null == that.pr) { that.pr = window.devicePixelRatio; } return that.pr; }, judgePixelRatio : function(pr) { var that = this; return (pr === that.getPixelRatio()); }, isLdpi : function() { var that = this; return that.judgePixelRatio(0.75); }, isMdpi : function() { var that = this; return that.judgePixelRatio(1); }, isHdpi : function() { var that = this; return that.judgePixelRatio(1.5); } }; BOMHeight = function() { var pageHeight = window.innerHeight; if(typeof pageHeight != "number"){ if(document.compatMode == "CSS1Compat"){ pageHeight = document.documentElement.clientHeight; }else{ pageHeight = document.body.clientHeight; } } return pageHeight; }; _parseViewPortContent = function(initWidth, initHeight, isUserScale, initScale, minScale, maxScale, isIntelligence) { var w = !!initWidth ? initWidth : "100%", h = !!initHeight ? initHeight : BOMHeight(), isUserScale = !!isUserScale ? 'yes' : 'no', initScale = !!initScale ? initScale : 1.0, //最大极限是 1.69 minScale = !!minScale ? minScale : 0.1, maxScale = !!maxScale ? maxScale : 1.0, domeMeta = '', targetDensitydpi = 'device-dpi', // [dpi_value(70 - 400) | device-dpi | high-dpi | medium-dpi | low-dpi] clientW = document.documentElement.clientWidth, screenW = window.screen.width; if (w === '100%') { w = "device-width"; initScale = 1.0; targetDensitydpi = 'device-dpi'; if (!!isIntelligence) { initScale = (screenW/clientW).toFixed(4); if(self.isAndroid()) { targetDensitydpi = 'device-dpi'; } } } if (typeof w === 'number') { if (!!isIntelligence) { w = Math.max(w, screenW); } initScale = (screenW/w).toFixed(4); if(self.isAndroid()) { initScale = 1; targetDensitydpi = (w/2.25714).toFixed(4); if (targetDensitydpi < 70) { targetDensitydpi = 70; } if (targetDensitydpi > 400) { targetDensitydpi = 400; } } } domeMeta = 'width=' + w + //', height=' + h + ', minimum-scale=' + minScale + ', maximum-scale=' + maxScale + ', initial-scale=' + initScale + ', user-scalable=' + isUserScale + ', target-densitydpi=' + targetDensitydpi; return domeMeta.trim(); }; _getScreen = function(){ // detail information to http://www.cnblogs.com/tearer/archive/2010/09/06/1819471.html // window.devicePixelRatio = 物理像素 / dips // window.screen.width = Android设备返回的是物理像素宽,IOS =》 dips宽 var s = "网页可见区域宽:" + document.body.clientWidth + "\n"; s += " 网页可见区域高:" + document.body.clientHeight + "\n"; s += " 网页可见区域宽:" + document.body.offsetWidth + " (包括边线和滚动条的宽)" + "\n"; s += " 网页可见区域高:" + document.body.offsetHeight + " (包括边线的宽)" + "\n"; s += " 网页正文全文宽:" + document.body.scrollWidth + "\n"; s += " 网页正文全文高:" + document.body.scrollHeight + "\n"; s += " 网页被卷去的高(ff):" + document.body.scrollTop + "\n"; s += " 网页被卷去的高(ie):" + document.documentElement.scrollTop + "\n"; s += " 网页被卷去的左:" + document.body.scrollLeft + "\n"; s += " 网页正文部分上:" + window.screenTop + "\n"; s += " 网页正文部分左:" + window.screenLeft + "\n"; s += " 屏幕分辨率的高:" + window.screen.height + "\n"; s += " 屏幕分辨率的宽:" + window.screen.width + "\n"; s += " 屏幕可用工作区高度:" + window.screen.availHeight + "\n"; s += " 屏幕可用工作区宽度:" + window.screen.availWidth + "\n"; s += " 你的屏幕设置是 " + window.screen.colorDepth + " 位彩色" + "\n"; s += " 物理像素/独立像素比: " + window.devicePixelRatio + " 像素/英寸" + "\n"; s += " 你的屏幕设置 " + window.screen.deviceXDPI + " 像素/英寸" + "\n"; return s; }; self = {}; self.userAgent = _userAgent; //self.screen = _getScreen(); self.getViewPortContent = function() { var domeMeta = document.getElementsByName('viewport')[0]; return domeMeta.content; }; // var options = { // initWidth: null, // initHeight: null, // isUserScale: null, // initScale: null, // minScale: null, // maxScale: null, // isIntelligence: bool /default null // } // default options likes: { initWidth : '100%' } or { initWidth : 540 } // self.setViewPortContent = function (options) { var DOM_meta = document.getElementsByName('viewport')[0], _content = ''; if (!DOM_meta) { DOM_meta = document.createElement('meta'); DOM_meta.name = 'viewport'; document.head.appendChild(DOM_meta); } if (typeof options === 'string'){ _content = _content; } else if (typeof options === 'object') { _content = _parseViewPortContent(options.initWidth, options.initHeight, options.isUserScale, options.initScale, options.minScale, options.maxScale, options.isIntelligence); } else { _content = _parseViewPortContent(); } DOM_meta.content = _content; }; self.isAndroid = function () { var me = this, result = false; if((_userAgent.match(_rAndroid))&& (_userAgent.match(_rAndroid).length > 0)) { result = true; } return result; }; self.isWindows = function () { var me = this, result = false; if((_userAgent.match(_rWins)) && (_userAgent.match(_rWins).length > 0)) { result = true; } return result; }; self.isWinPhone = function () { var me = this, result = false; if((_userAgent.match(_rWP)) && (_userAgent.match(_rWP).length > 0)) { result = true; } return result; }; self.isIOS = function () { var me = this, result = false; if((_userAgent.match(_rIPhone))&& (_userAgent.match(_rIPhone).length > 0)) { result = true; } return result; }; return self; })(this); |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com