h5 web页面手机适配

;(function () {
    window.addEventListener(('orientationchange' in window ? 'orientationchange' : 'resize'), (function () {
        function c() {
            var d = document.documentElement;
            var cw = d.clientWidth || 640;
            d.style.fontSize = (20 * (cw / 320)) > 40 ? 40 + 'px' : (20 * (cw / 320)) + 'px';
        }
        c();
        return c;
    })(), false);
})();
 
/*自适应页面宽度*/ 
var setFontSize = function(){  
   var width = document.documentElement.clientWidth;  
   width = width > 750 ? 750: width;  
   var fontSize = width/750 * 100;  
   document.getElementsByTagName("html")[0].style.fontSize = fontSize + "px";
}  
setFontSize();
window.onresize = setFontSize;

 

//整屏适配(利用背景图片整屏适配,屏幕宽高比小于图片宽高比时,图片向左位移,)

/*css*/
.warp{ width: 100%; margin:0 auto;  overflow: hidden;}
.content{position: relative; width:100%; height:100%;}
#bg{width: auto;min-width:100%;height: 100%;position: absolute;top: 0px;z-index: -100;}

//html
<body>
<div class="warp">
    <div  class="content">
        <img id="bg" src=""/>
    <div>其它内容</div>
   </div>
</div>

//js
setTimeout("adjustBG()", 200);
function adjustBG() {
    var width = $(".content").width();
    var height = document.documentElement.clientHeight;
    var aspectRadio = height / width;
    var $bg = $("#bg");
    $(".warp").css("height", height + "px");
    if(aspectRadio>=($bg.height()/$bg.width())){
        $bg.css("left", -(height / $bg.height() * $bg.width() - width) / 2 + "px");
    }
}

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值