移动端布局用到的自适应的几种方法

刚开始写移动端的时候自适应废了好大劲;在网上找了好多资料;自己也试了很多;以下几种常用方法和大家分享下:
移动端我们设计好的页面一般不需要让它可以放大缩小,因此要在meta标签来控制;可以在 head标签中放这行代码:

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

通过js根据不同的设备宽度在根元素上设置不同的字体大小
(function(win) {
第二个也是通过js根据不同的设备宽度在根元素上设置不同的字体大小大家根据习惯自己用

function font(w) {
var w = w || 640,
docEl = document.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function() {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return;
docEl.style.fontSize = 100 * (clientWidth / w) + 'px';
};
if (!document.addEventListener) return;
window.addEventListener(resizeEvt, recalc, false);
document.addEventListener('DOMContentLoaded', recalc, false);
}
font();

这个我觉得比较好用只要传入你设计搞得大小就行了;然后根据根字体大小来算

(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);
new function (){
var _self = this;
_self.width = 640;//设置默认最大宽度
_self.fontSize = 100;//默认字体大小
_self.widthProportion = function(){var p = (document.body&&document.body.clientWidthdocument.getElementsByTagName("html")[0].offsetWidth)/_self.width;return p>1?1:p<0.5?0.5:p;};
_self.changePage = function(){
document.getElementsByTagName("html")[0].setAttribute("style","font-size:"+_self.widthProportion()*_self.fontSize+"px !important");
}
_self.changePage();
window.addEventListener("resize",function(){_self.changePage();},false);
};

这两种和上面的没有什么区别大家可以根据自己的习惯用
接下来就是用CSS的媒体查询:

html {
font-size : 20px;
}
@media only screen and (min-width: 401px){
html {
font-size: 25px !important;
}
}
@media only screen and (min-width: 428px){
html {
font-size: 26.75px !important;
}
}
@media only screen and (min-width: 481px){
html {
font-size: 30px !important;
}
}
@media only screen and (min-width: 569px){
html {
font-size: 35px !important;
}
}
@media only screen and (min-width: 641px){
html {
font-size: 40px !important;
}
}

给TA打赏
共{{data.count}}人
人已打赏
精选置顶设计园地

CSS3炫酷效果-swing、疯狂摆动、脉冲

2018-5-26 0:17:16

设计园地

给文本文字添加图片背景

2018-5-26 0:17:19

    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索