爱玺玺

爱玺玺的生活日记本。wx:lb87626

移动端rem高度自适应方法

<!doctype html>

<html>

<head>

<meta name="renderer" content="webkit">

<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">

<meta charset="utf-8">

<meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1,user-scalable=no">

<meta name="format-detection" content="telephone=no">

<meta name="apple-mobile-web-app-capable" content="yes">

<script type="text/javascript" src="http://wap.tszyzx.com/js/jquery-1.10.1.min.js"></script>

<script>

 (function (doc, win) {

        var docEl = doc.documentElement,

            resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',

            recalc = function () {

                var clientWidth = docEl.clientWidth;

                if (!clientWidth) return;

                if(clientWidth>=640){

                    docEl.style.fontSize = '100px';

                }else{

                    docEl.style.fontSize = 100 * (clientWidth / 640) + 'px';

                }

            };


        if (!doc.addEventListener) return;

        win.addEventListener(resizeEvt, recalc, false);

        doc.addEventListener('DOMContentLoaded', recalc, false);

    })(document, window);

</script>

<title>无标题文档</title>

<style>

body{ margin:0px; padding:0px;}

</style>

</head>


<body>

<div style="background:url(2-1.jpg) no-repeat; background-size:100%; height:3.65rem; border:solid 1px #f90; font-size:0.5rem">sadf</div>

</body>

</html>


关键部分,引入js代码部分设置font-size可以按照比例变化。

默认font-size:16px;

通过js根据实际尺寸调整font-size

根据1rem=100px,但是像素是变化的,这里有css的像素和实际物理像素的区别的问题?

docEl.style.fontSize = 100 * (clientWidth / 640) + 'px';

发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5