计算html的font size,前端开发js+rem动态计算font-size的大小,适配各种设备!

/p>

content=”width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;”

name=”viewport”

/>

响应式布局

font-size: 20px;

width: 100%;

height: 100%;

}

body {

margin: 0;

padding: 0;

}

header,

footer {

width: 100%;

background: #17a578;

color: #fff;

font-size: 1rem;

text-align: center;

line-height: 2rem;

}

.footer {

position: fixed;

bottom: 0;

}

.box {

}

.public {

width: 5rem;

height: 5rem;

font-size: 1.2rem;

display: inline-block;

text-align: center;

color: #fff;

line-height: 5rem;

margin-top: 1rem;

}

.left {

background: #f00;

}

.center {

background: #048f74;

}

.right {

background: #000;

}

页面头部

页面底部

//orientationchange方向改变事件

;(function(doc, win) {

var docEl = doc.documentElement, //根元素

//判断窗口有没有orientationchange这个方法,有就赋值给一个变量,没有就返回resize方法。

resizeEvt =

‘orientationchange’ in window

? ‘orientationchange’

: ‘resize’,

recalc = function() {

var clientWidth = docEl.clientWidth

if (!clientWidth) return

//把document的fontSize大小设置成跟窗口成一定比例的大小,从而实现响应式效果。

docEl.style.fontSize = 20 * (clientWidth / 320) + ‘px’

}

//alert(docEl)

if (!doc.addEventListener) return

win.addEventListener(resizeEvt, recalc, false) //addEventListener事件方法接受三个参数:第一个是事件名称比如点击事件onclick,第二个是要执行的函数,第三个是布尔值

doc.addEventListener(‘DOMContentLoaded’, recalc, false) //绑定浏览器缩放与加载时间

})(document, window)

//alert(document.documentElement.clientWidth/320)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值