相信很多和会和我碰到一样的情况,就是你用rem去写移动端的时候,如果想引用别的UI库的时候,自己页面写的是rem单位,而ui库的css里面写的是px,大家都知道,rem是在html上设置font-size 字体大小。然后,,然后ui库的里面的px 会和 rem冲突,很麻烦,搜了很多办法都没找到好的解决办法!!!
rem的做法
(function() {
var doc = document,
win = window;
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function() {
var clientWidth = docEl.clientWidth;
if(!clientWidth) return;
//如果屏幕大于750(750是根据我效果图设置的,具体数值参考效果图),就设置clientWidth=750,防止font-size会超过100px
if(clientWidth > 750) { clientWidth = 750 }
//设置根元素font-size大小
docEl.style.fontSize = 100 * (clientWidth / 750) + 'px';
};
//屏幕大小改变,或者横竖屏切换时,触发函数
win.addEventListener(resizeEvt, recalc, false);
//文档加载完成时,触发函数
doc.addEventListener('DOMContentLoaded', recalc, false);
})();
本人在看帖子的时候,看到一个淘宝缩放页面的方法,亲测,效果不错,直接用px单位写,根据meta标签进行缩放。这样如果想引用ui库的css 就不会冲突了,嗯不错不错。
附上缩放代码
<script type="text/javascript">
var phoneWidth = parseInt(window.screen.width);
var phoneHeight = parseInt(window.screen.height);
var phoneScale = phoneWidth / 750; /*除以的值按手机的物理分辨率,就是设计图宽度*/
//获取head标签
var $head = document.getElementsByTagName("head")[0];
var ua = navigator.userAgent;
if(/Android (\d+\.\d+)/.test(ua)) {
var version = parseFloat(RegExp.$1);
// andriod 2.3
if(version > 2.3) {
$head.innerHTML+=('<meta name="viewport" content="width=device-width,initial-scale=' + phoneScale + ',minimum-scale=' + phoneScale + ',maximum-scale=' + phoneScale + ',user-scalable=no">');
// andriod 2.3以上
} else {
$head.innerHTML+=('<meta name="viewport" content="width=device-width,user-scalable=no">');
}
// 其他系统
} else {
$head.innerHTML+=('<meta name="viewport" content="width=device-width, initial-scale=' + phoneScale + ',minimum-scale=' + phoneScale + ',maximum-scale =' + phoneScale + ',user-scalable=no,">');
}
</script>
但是。。。但是。如果做app套浏览器壳的话,是不允许你使用meta标签缩放的。
网易的做法是用css进行缩放。具体做法如下:
html
<div id="app">
<div id="getHeight"></div>
<div id="content">
123
</div>
</div>
css
/* css reset from normalize*/
html {
/* 1 */
-ms-text-size-adjust: 100%;
/* 2 */
-webkit-text-size-adjust: 100%;
/* 2 */
}
body {
margin: 0;
}
/* 上下拉动滚动条时卡顿、慢 */
body {
-webkit-overflow-scrolling: touch;
overflow-scrolling: touch;
}
/* ios和android下触摸元素时出现半透明灰色遮罩 */
a {
-webkit-tap-highlight-color: rgba(255, 255, 255, 0);
}
#getHeight {
height: 100vh;
width: 100vw;
position: absolute;
top: 0;
left: 0;
z-index: -10;
}
#content {
width: 750px;
transform-origin: 0 0;
font-size: 36px;
}
#content {
height: 100%;
}
js
<script>
this.widthBox = window.screen.width
this.heightBox = window.screen.height
if (this.widthBox > document.getElementById('getHeight').clientWidth) {
this.widthBox = document.getElementById('getHeight').clientWidth
}
this.showWindowSize()
function reHeight() {
console.log(this.heightBox)
sessionStorage.removeItem('daicanfenappheight')
}
function showWindowSize() { // 更改变形
if (sessionStorage.daicanfenappheight && sessionStorage.daicanfenappheight !== 0) {
this.heightBox = parseFloat(sessionStorage.daicanfenappheight)
} else {
this.heightBox = parseFloat(document.getElementById('getHeight').clientHeight)
sessionStorage.daicanfenappheight = this.heightBox
}
let slace = this.widthBox / 750
// body
let BigBox = document.getElementsByTagName('body')[0]
BigBox.style.width = this.widthBox + 'px'
BigBox.style.height = this.heightBox + 'px'
BigBox.style.overflow = 'hidden'
BigBox.style.maxWidth = '750px'
// #app
let AppBox = document.getElementById('app')
AppBox.style.width = this.widthBox + 'px'
AppBox.style.height = this.heightBox + 'px'
AppBox.style.overflow = 'hidden'
// #content
let ContentBox = document.getElementById('content')
let ListArr = [ContentBox]
// 循环添加属性
for (let i = 0; i < ListArr.length; i++) {
ListArr[i].style['transform'] = 'scale(' + slace + ')'
ListArr[i].style['-ms-transform'] = 'scale(' + slace + ')'
ListArr[i].style['-moz-transform'] = 'scale(' + slace + ')'
ListArr[i].style['-webkit-transform'] = 'scale(' + slace + ')'
ListArr[i].style['-o-transform'] = 'scale(' + slace + ')'
}
ContentBox.style['overflow-x'] = 'hidden'
ContentBox.style['overflow-y'] = 'auto'
ContentBox.style['height'] = (parseFloat(this.heightBox) / slace) + 'px'
}
</script>
这样方法的好处就是用css进行缩放,在做app的时候也是没有问题的,缺点,就是代码比较多。
不喜勿喷。谢谢!!!