MetaHandler.js:移动端适配各种屏幕
MetaHandler.js
!function () {
var opt = function() {
var ua = navigator.userAgent,
android = ua.match(/(Android);?[\s\/]+([\d.]+)?/),
ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/),
iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
os = {},
uc = ua.match(/UCBrowser+/);
if (android){ os.android = true, os.version = android[2];}
if (iphone && !ipod) {os.ios = os.iphone = true, os.version = iphone[2].replace(/_/g, '.');}
if (ipad) {os.ios = os.ipad = true, os.version = ipad[2].replace(/_/g, '.');}
if (ipod) {os.ios = os.ipod = true, os.version = ipod[3] ? ipod[3].replace(/_/g, '.') : null;}
var MetaHandler = function(){
//MONOSTATE
if(MetaHandler.prototype.instance){
return MetaHandler.prototype.instance;
}
var me = this;
var meta = {},_els;
/**
* _els
* meta = {name:{content:String,seriation:Array,store:{property:String},...},...}
* @method init
*/
function init(){
_els = document.getElementsByTagName('meta');
for(var i=0;i<_els.length;i++){
var name = _els[i].name;
if(name){
meta[name] = {};
meta[name].el = _els[i];
meta[name].content = _els[i].content;
meta[name].seriation = meta[name].content.split(',');
meta[name].store = getContentStore(name);
}
}
return me;
}
function getContentStore(name){
var content = meta[name].seriation,store = {};
for(var i=0;i<content.length;i++){
if(content[i].length<1){
content[i] = null;
delete content[i];
content.length--;
}else{
var ct = content[i].split('='),
pp = ct[0];
if(pp){
store[pp] = ct[1];
}
}
}
return store;
}
this.hasMeta = function(name){
return meta[name]?1:0;
}
this.createMeta = function(name){
if(!this.hasMeta(name)){
var el = document.createElement('meta');
el.name = name;
document.head.appendChild(el);
meta[name] = {};
meta[name].el = el;
meta[name].content = '';
meta[name].seriation = [];
meta[name].store = {};
}
return me;
}
this.setContent = function(name,value){
meta[name].content = value;
meta[name].el.content = value;
return me;
}
this.getContent = function(name){
return meta[name] && meta[name].content;
}
function updateContent(name){
meta[name].content = meta[name].seriation.join(',');
me.setContent(name,meta[name].content);
return me;
}
this.removeContentProperty = function(name,property){
var _property = property;
if(meta[name]){
if(meta[name].store[_property]!=null){
for(var i = 0;i<meta[name].seriation.length;i++){
if(meta[name].seriation[i].indexOf(property+'=')!=-1){
meta[name].seriation[i] = null;
delete meta[name].seriation[i];
break;
}
}
}
updateContent(name);
}
return me;
}
this.getContentProperty = function(name,property){
return meta[name] && meta[name].store[property];
}
this.setContentProperty = function(name,property,value){
var _property = property,
pv = property+'='+value;
if(meta[name]){
if(meta[name].store[_property]!=null){
meta[name].store[_property] = value;
for(var i = 0;i<meta[name].seriation.length;i++){
if(meta[name].seriation[i].indexOf(property+'=')!=-1){
meta[name].seriation[i] = pv;
break;
}
}
}else{
meta[name].store[_property] = value;
meta[name].seriation.push(pv);
}
updateContent(name);
}
return me;
}
this.fixViewportWidth = function(width,fixBody){
width = width || me.getContentProperty('viewport','width');
if(width != 'device-width'){
var dpr = window.devicePixelRatio;
if(uc){
var iw = window.innerWidth || width,
ow = window.outerWidth/dpr || iw,
sw = window.screen.width/dpr || iw,
saw = window.screen.availWidth/dpr || iw,
ih = window.innerHeight || width,
oh = window.outerHeight/dpr || ih,
sh = window.screen.height/dpr || ih,
sah = window.screen.availHeight/dpr || ih,
w = Math.min(iw,ow,sw,saw,ih,oh,sh,sah),
ratio = w/width;
ratio = Math.min(ratio,dpr);
}else{
var iw = window.innerWidth || width,
ow = window.outerWidth || iw,
sw = window.screen.width || iw,
saw = window.screen.availWidth || iw,
ih = window.innerHeight || width,
oh = window.outerHeight || ih,
sh = window.screen.height || ih,
sah = window.screen.availHeight || ih,
w = Math.min(iw,ow,sw,saw,ih,oh,sh,sah),
ratio = w/width;
ratio = Math.min(ratio,dpr);
}
//fixBody may trigger a reflow,you should not use it if you could do it in your css
if(fixBody){
document.body.style.width = width+'px';
}
//ratio = 1;
if(os.android){
if(uc){
me.removeContentProperty('viewport','user-scalable')
.setContentProperty('viewport','initial-scale',ratio)
.setContentProperty('viewport','maximum-scale',ratio);
}else{
me.removeContentProperty('viewport','user-scalable')
.setContentProperty('viewport','target-densitydpi','device-dpi')
.setContentProperty('viewport','initial-scale',ratio)
.setContentProperty('viewport','maximum-scale',ratio);
}
}else if(os.ios && !os.android){
me.setContentProperty('viewport','user-scalable','no');
if(os.ios && parseInt(os.version)<7){
me.setContentProperty('viewport','initial-scale',ratio);
}
}
}
}
init();
//MONOSTATE
MetaHandler.prototype.instance = this;
};
return new MetaHandler;
}();
// HTML5友情提示 —— 调用自适应屏幕的功能函数
opt.fixViewportWidth(640);
}();
准备一个用px实现的移动页面(宽度固定死的页面),引入metahandler.js框架
1、视口设置
width=640,是根据psd图来设置,有多宽设置多宽(设计图是640的设置640)
<meta content="target-densitydpi=device-dpi,width=640" name="viewport">
2、body设置宽度,是根据psd图来设置,有多宽设置多宽
3. metahandler.js里面参数的设置
调用自适应屏幕的功能函数,640是根据psd图来设置,有多宽设置多宽
opt.fixViewportWidth(640);
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>MetaHandler.js示例 - jingPlus</title>
<meta content="target-densitydpi=device-dpi,width=640" name="viewport">
<style>
body {
/*设置跟psd一样的宽度*/
width: 640px;
margin: 0 auto;
background-color: #f0eff4;
}
.wrap header {
position: relative;
height: 78px;
line-height: 78px;
background-color: #f9f9f9;
font-size: 32px;
text-align: center;
}
.con {
padding: 24px 17px 0;
}
.con nav a {
float: left;
width: 146px;
height: 146px;
margin-bottom: 10px;
line-height: 146px;
font-size: 22px;
text-align: center;
}
.con nav a:nth-child(1) {
width: 299px;
background-color: #e66444;
}
.con nav a:nth-child(2) {
margin: 0 7px;
background-color: #f09056;
}
.con nav a:nth-child(3) {
background-color: #a2c159;
}
.con nav a:nth-child(4) {
background-color: #9178af;
}
.con nav a:nth-child(5) {
margin: 0 7px;
background-color: #49a7da;
}
.con nav a:nth-child(6) {
margin-right: 7px;
background-color: #56bc8a;
}
.con nav a:nth-child(7) {
background-color: #d179ad;
}
</style>
</head>
<body>
<div class="wrap">
<header>示例标题</header>
<section class="con">
<nav class="clearfix">
<a href="" title="">一二三四</a>
<a href="" title="">在线订餐</a>
<a href="" title="">报修申请</a>
<a href="" title="">地铁线路</a>
<a href="" title="">公交站点</a>
<a href="" title="">测试</a>
<a href="" title="">今日优惠</a>
</nav>
</section>
</div>
<!-- 引入MetaHandler -->
<script type="text/javascript" src="metaHandler.src.js"></script>
</body>
</html>
本文转自:http://blog.csdn.net/congcong875/article/details/50352123
MetaHandler.js:移动端适配各种屏幕的更多相关文章
- flexible.js移动端适配安卓高分辨不兼容问题
根据网上找到的解决办法,对于安卓设备,把dpr=1改为当前设备的dpr if (!dpr && !scale) { if (isIPhone) { // iOS下,对于2和3的屏,用2 ...
- JS移动端适配(自适应)
var html = document.querySelector('html'); changeRem(); window.addEventListener('resize', changeRem) ...
- 移动端适配--flexible.js
引言: H5适配一直是一个比较普遍的问题,很多文章都会讲,最近开发了一个H5的项目使用了一下淘宝的 flexible.js,写一篇文章自己总结一下. 一.背景介绍: Flexible.js是淘宝公开的 ...
- 移动端适配方案 flexible.js
前言 移动端适配一直以来都是前端开发中不可或缺的重要组成部分,如果没有了它,那么你做出来的页面极有可能会出现各种意外(写出来的页面与设计稿之间的差别).所有我们得找到一种相对来说让人比较满意的解决方案 ...
- 从flexible.js引入高德地图谈起的移动端适配
曾几何时,前端还仅仅是PC端的.随着移动时代的兴起,h5及css3的推陈出新.前端的领域慢慢的由传统的pc端转入了移动端,这也导致了前端这一职业在风口的一段时间出尽了风头. 从开始的惶恐和无从下手,慢 ...
- hotcss.js Flexible 移动端适配在dpr=2和dpr=3出现的字体大小设置不正确问题.
这段时间一直在用hotcss做移动端适配,做了几个页面没有发现什么问题,后来老大要加快进度,我把项目分出一块给另一个同事做,她发现了一个问题就是字体在dpr=2,dpr=3,的设备上字体大小显示老是不 ...
- JS+rem,移动端适配
window.onresize = function () { setHtmlFz(); } setHtmlFz(); function setHtmlFz() { // 基础值 var baseVa ...
- Web移动端适配总结
移动端适配的相关概念以及几种方案总结 适配相关概念 布局视口(layout viewport):html元素的上一级容器即顶级容器,用于解决页面在手机上显示的问题.大部分移动设备都将这个视口分辨率设置 ...
- vue.js移动端配置flexible.js
前言 最近在用vue做移动端项目,网上找了一些移动端适配的方案,个人觉得手淘团队flexible.js还是比较容易上手,在这里做下总结. 主体 flexible.js适配方案采用rem布局,根据屏幕分 ...
随机推荐
- LoadRunner编程之跳出迭代
LoadRunner编程之跳出迭代 51Testing软件测试网3p6pK.Yo LoadRunner中 提供了函数exit(-1)来结束迭代. 使用return 0 来结束本次迭代,进入下一次迭代. ...
- 移动网站中,用canvas,svg比用图片好?
1.Svg可以单独作为文件打开,在AI里做矢量图形,保存图层路径,即可另存为Svg文件. (1) Path语法:命令+参数.大写字母表示坐标参数为绝对位置,小写字母表示坐标参数为相对位置(即上次画笔结 ...
- 实现点击不同的按钮加载不同的css
这段时间做一个小网站发现有时候特别需要点击不同的按钮去加载不同的css,这样可以确定点击的是哪个,由于每个按钮都是从后端数据库加载过来的,不仅是简简单单的用id或者是类名,用过this也不行: 前端加 ...
- Monkeyrunner脚本中component快速定位方法
在编写MonkeyRunner脚本过程中,会出现component这一项内容,很多人可能不知道怎么确认,其实这个主要是为了指定要测试的程序包名和主Activity名,我们可以用以下方法去进行确认: 1 ...
- oracle性能优化----处理大数据量数据
场景:对2千万个数据,修改他们的名字加上后缀“生日”. 普通sql: and not regexp_like(title, '生日'); 优化sql: declare type rid_Arra ...
- 80端口被占用,pid=4强制杀进程杀不掉
解决: http://www.cnblogs.com/myjavawork/articles/1867839.html 把SqlServer的该服务关闭.虽然他显示的进程号跟搜出来的不一样.
- iOS之09-特有语法
1.分类 - Category 1> 基本用途 分类:可以给某个类扩充一些方法(不修改原来的代码) 如何在不改变原来类模型的前提下,给类扩充一些方法? 有2种方式: 继承: 分类(Categot ...
- A hard puzzle
A hard puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- [R]R下as.Date()函数的坑
问题描述: 在本地使用as.Date()函数从POSIXct类型中提取date时,出现了日期不一致的错误.导致处理数据时,总是出问题. 还好被领导发现数据有误,不然这个bug不知道还要潜伏多久.尽管如 ...
- 挂载磁盘linux
1. 用mkfs命令在/dev/sdb上创建ext4文件系统 1.1 mkfs命令 在设备上创建文件系统 mkfs [options] device 参数 device 为要在其上面创建文件系统的设备 ...