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

移动端适配方案

作者: 王小傲 | 来源:发表于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)

    相关文章

      网友评论

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

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