vue 判断是否为移动端


mounted() {
      const isMobile = this.$_isMobile()
      if (isMobile) {
          store.dispatch('app/toggleDevice', 'mobile')
          store.dispatch('app/closeSideBar', { withoutAnimation: true })
      }
},


methods: { $_isMobile() { const rect = body.getBoundingClientRect() return rect.width - 1 < WIDTH },
import Cookies from 'js-cookie'

const state = {

  device: 'desktop',
 
}

const mutations = {

  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  }

}

const actions = {
 
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
<template v-if="device!=='mobile'">
        <search id="header-search" class="right-menu-item" />

    <!--    <el-tooltip content="源码地址" effect="dark" placement="bottom">
          <hejiGit id="heji-git" class="right-menu-item hover-effect" />
        </el-tooltip>

        <el-tooltip content="文档地址" effect="dark" placement="bottom">
          <hejiDoc id="heji-doc" class="right-menu-item hover-effect" />
        </el-tooltip> -->

        <screenfull id="screenfull" class="right-menu-item hover-effect" />

        <el-tooltip content="布局大小" effect="dark" placement="bottom">
          <size-select id="size-select" class="right-menu-item hover-effect" />
        </el-tooltip>

      </template>

computed: {
...mapGetters([
'device'
]),

/**
 * 判断某个原生DOM元素是否不在屏幕可见区内
 * @param {*} el 原生DOM元素
 */
const isElementNotInViewport = function(el) {
  let rect = el.getBoundingClientRect();
  return (
    rect.top >= (window.innerHeight || document.documentElement.clientHeight) ||
    rect.bottom <= 0
  );
};

export { isElementNotInViewport};

 

 
上一篇:P2858 [USACO06FEB]Treats for the Cows G/S


下一篇:致敬 hacker |盘点内存虚拟化探索之路