H5移动端横竖屏切换


H5移动端横竖屏切换

  • 监听设备信息进行切换
import Vue from 'vue'
const EventBus = new Vue()
export default EventBus

import EventBus from './EventBus'
import Vue from 'vue'
export function addScreenRotateListen() {
  const mql = window.matchMedia('(orientation: portrait)')

  function onMatchMeidaChange(mql) {
    if (mql.matches) {
      // 竖屏
      console.log('竖屏')
      EventBus.$emit('screenRotated', 'portrait')
    } else {
      // 横屏
      console.log('横屏')
      EventBus.$emit('screenRotated', 'landscape')
    }
  }
  onMatchMeidaChange(mql)
  mql.addListener(onMatchMeidaChange)
}

Vue.prototype.DevicePortraited = () => {
  const mql = window.matchMedia('(orientation: portrait)')
  if (mql.matches) {
    // console.log('DevicePortraited 竖屏')
    return true
  } else {
    // console.log('DevicePortraited 横屏')
    return false
  }

}

在需要转换的界面进行监听

  • 收到通知进行处理。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM