vue项目使用screenfull做网页全屏功能

1.安装依赖(本人使用4.2.0版本,。最开始使用5.x.x版本取到的浏览器允许全屏的值为undefind,换了版本后成功取值,使用Chrome浏览器)

cnpm install screenfull@4.2.0 --save

2.写组件Screenfull/index.vue

<template>
  <div>
    <svg-icon :icon-class="isFullscreen?'exit-fullscreen':'fullscreen'" @click="click" />
  </div>
</template>

<script>
import screenfull from 'screenfull'

export default {
  name: 'Screenfull',
  data() {
    return {
      isFullscreen: false
    }
  },
  mounted() {
    this.init()
  },
  beforeDestroy() {
    this.destroy()
  },
  methods: {
    click() {
      if (!screenfull.enabled) {
        this.$message({
          message: 'you browser can not work',
          type: 'warning'
        })
        return false
      }
      screenfull.toggle()
    },
    change() {
      this.isFullscreen = screenfull.isFullscreen
    },
    init() {
      if (screenfull.enabled) {
        screenfull.on('change', this.change)
      }
    },
    destroy() {
      if (screenfull.enabled) {
        screenfull.off('change', this.change)
      }
    }
  }
}
</script>

<style scoped>
.screenfull-svg {
  display: inline-block;
  cursor: pointer;
  fill: #5a5e66;;
  width: 20px;
  height: 20px;
  vertical-align: 10px;
}
</style>

3.在页面调用组件,以及添加样式

import Screenfull from '@/components/Screenfull'
<template>
  <div class="navbar">

    <div class="right-menu">

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

    </div>
  </div>
</template>

<script>
import Screenfull from '@/components/Screenfull'

export default {
  components: {
    Screenfull
  },
  computed: {
    ])
  },
  methods: {
  }
}
</script>

<style lang="scss" scoped>
.navbar {
    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background .3s;

        &:hover {
          background: rgba(0, 0, 0, .025)
        }
      }
    }
}
</style>

 

Logo

前往低代码交流专区

更多推荐