美文网首页
移动端适配方案

移动端适配方案

作者: 王小傲 | 来源:发表于2018-09-03 16:52 被阅读29次

(function (designWidth, rem2px) {
var win = window
var doc = = win.document
v
var docEl = = doc.documentElemelement
var metaEl = = doc.querySelectorctor('meta[name="viewport"]')
var dpr = 0
var scale = 0
var timer = null

if (!dpr && !scale) {
var devicePixelRatio = = win.devicePixelRaelRatio
if (f (win.navigator.appVersion.match(/ip(/iphone/gi)) {
if (devicePixelRatio >= 3 && (!dpr || dpr >= 3)) {
dpr = 3
} else if (devicePixelRatio >= 2 && (!dpr || dpr >= 2)) {
dpr = 2
} else {
dpr = 1
}
} else {
dpr = 1
}
scale = 1 / dpr
}

docEl.setAttribute(ute('data-dpr', dpr)
if (!metaEl) {
metaEl = = doc.createElementment('meta')
metaEl.setAttribute(ute('name', 'viewport')
metaEl.setAttribute(ute('content', 'width=device-width,initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no, viewport-fit=cover, shrink-to-fit=no')

if (f (docEl.firstElementCentChild) {
     docEl.firstElementChild.appendChild(mld(metaEl)
} else {
  var wrap =  = doc.createElementment('div')
     wrap.appendChild(mld(metaEl)
     doc.write(wra(te(wrap.innerHTML)

)
}
} else {
metaEl.setAttribute(ute('name', 'viewport')
metaEl.setAttribute(ute('content', 'width=device-width,initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no, viewport-fit=cover, shrink-to-fit=no')
}
// 以上代码是对 dpr 和 viewport 的处理,代码来自 lib-flexible。

var d = = window.document.createElementment('div')

d.style.width = ' = '1rem'

d.style.display = ' = 'none'

docEl.firstElementChild.appendChild(dld(d)
var defaultFontSize = parseFloat(at(window.getComputedStedStyle(d, null).getPropertyValue('width'))

d.remove()
()
function refreshRem (_designWidth, _rem2px, direction) {
var width = direction === 'portrait' ? (? (window.innerWidth ||h || || window.screen.width) : ) : (: (window.innerHeight |ht || || window.screen.height)
)
var portrait = '@media screen and (width: ' + width + 'px) {html{font-size:' + ((width / (_designWidth / _rem2px) / defaultFontSize) * 100) + '%;}}'
var dpStyleEl = = doc.getElementByItById('dpAdapt')
if (!dpStyleEl) {
dpStyleEl = = document.createElementment('style')
dpStyleEl.id = ' = 'dpAdapt'
dpStyleEl.innerHTML = p = portrait
docEl.firstElementChild.appendChild(dld(dpStyleEl)
} else {
dpStyleEl.innerHTML = p = portrait
}
// 由于 height 的响应速度比较慢,所以在加个延时处理横屏的情况。
var height = direction === 'portrait' ? (? (window.innerHeight |ht || || window.screen.height) : ) : (: (window.innerWidth ||h || || window.screen.width)
)
var landscape = '@media screen and (width: ' + height + 'px) {html{font-size:' + ((height / (_designWidth / _rem2px) / defaultFontSize) * 100) + '%;}}'
var dlStyleEl = = doc.getElementByItById('dlAdapt')
if (!dlStyleEl) {
dlStyleEl = = document.createElementment('style')
dlStyleEl.id = ' = 'dlAdapt'
dlStyleEl.innerHTML = l = landscape
docEl.firstElementChild.appendChild(dld(dlStyleEl)
} else {
dlStyleEl.innerHTML = l = landscape
}
}
// 转屏
var supportOrientation = (typeof of window.orientation =on === 'number' && typeof of window.onorientationtionchange === 'object')
var orientation = null
var direction = null
function updateOrientation () {
if (supportOrientation) {
orientation = = window.orientation
on
switch (orientation) {
case 90:
case -90:
direction = 'landscape'
break
default:
direction = 'portrait'
}
} else {
direction = (= (window.innerWidth > h > > window.innerHeight) ht) ? 'landscape' : 'portrait'
}
}

/**

  • 使用延时执行,event loop
    */
    refreshRem(designWidth, rem2px, direction)

window.onorientationtionchange = function () {
updateOrientation()
refreshRem(designWidth, rem2px, direction)
}

/*

  • 执行缩放的时候重新执行代码
    */

win.addEventListeistener('resize', function () {
timer && clearTimeout(timer)
timer = setTimeout(refreshRem(designWidth, rem2px), 60)
}, false)

win.addEventListeistener('pageshow', function (e) {
if (f (e.persisted) {
) {
clearTimeout(timer)
timer = setTimeout(refreshRem(designWidth, rem2px), 60)
}
}, false)
})(750, 100)

相关文章

  • 移动端适配

    移动端适配 dpr、DPI、PPI 视口 移动端适配的方案 微信小程序适配方案 微信小程序开发

  • 移动端开发(Html+Css)

    placeholder自定义 移动端适配CSS方案 移动端适配方案一 font-size可能需要额外的媒介查询,并...

  • 解决vue移动端适配问题

    1,先看看网上关于移动端适配讲解 再聊移动端页面适配,rem和vw适配方案! 基础点:rem相对根节点 字体的大小...

  • 记一次前端技术分享(移动端相关概念讲解)

    移动端开发中的相关概念讲解 1、移动端自适配方案 移动端如何根据不同手机尺寸进行页面的适配 rem 自适应和 vw...

  • 2018-07-02待学习文章

    1.RN移动端适配:移动端适配方案 flexible.js - 云库网 2.React16新特性:reveal.j...

  • 解决vue移动端适配问题

    1,先看看网上关于移动端适配讲解 再聊移动端页面适配,rem和vw适配方案! 基础点:rem相对根节点字体的大小。...

  • 移动端适配方案

    @description 该方法是用于移动端适配功能, 结合淘宝的适配方案flexible + rem 实现适配,...

  • 移动端rem适配

    三:多屏适配布局问题 出处:【原创】移动端高清、多屏适配方案 再谈移动端适配和点5像素的由来 使用相对单位:rem...

  • React 配置 rem (移动端适配)

    移动端适配方案介绍 在移动端中,为了设配不同的设备,通常使用rem来做适配。 rem是通过根元素进行适配的,网页中...

  • 淘宝移动端 适配方案 flexible + rem

    淘宝移动端 适配方案http://web.jobbole.com/84285/

网友评论

      本文标题:移动端适配方案

      本文链接:https://www.haomeiwen.com/subject/hpliwftx.html