大家好,又见面了,我是你们的朋友全栈君。
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>
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/124989.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...