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