可视化大屏自适应缩放屏幕

前端 实现大屏缩放自适应屏幕 适用vue
思路:

页面初始化获取屏幕的原始比例
将自适应元素的scale变量设置为当前比例
监听浏览器窗口大小,获取新的比例值重新给元素scale赋值
PartZoom.vue文件

// 公共自适应文件
<template>
  <div
    class="ScaleBox"
    ref="ScaleBox"
    :style="{
      width: width + 'px',
      height: height + 'px'
    }"
  >
    <slot></slot>
  </div>
</template>
<script>
export default {
  props: {
    // 标准内容宽度
    uiContentWidth: {
      type: Number,
      default: 1920
    },
    // 标准内容高度
    uiContentHeight: {
      type: Number,
      default: 0
    },
    // 其他内容的宽度
    otherWidth: {
      type: Number,
      default: 300 //左侧菜单栏默认宽度,如果没有则忽略
    }
  },
  data () {
    return {
      width: 1920, // 初始宽
      height: 1080, // 初始高
      zoom: 1 // 计算要缩放的 宽度比(浏览器可视宽度与设计稿的宽度比)
    }
  },
  mounted () {
    this.setScale()
    window.addEventListener('resize', this.debounce(this.setScale, 100))
  },
  beforeDestroy () {
    window.removeEventListener('resize', this.debounce)
  },
  methods: {
    getScale () {
      // 当前屏幕下需要适配内容的宽度 = 屏幕的宽度 - 其他不需要适配的内容的宽度
      const innerWidth = window.innerWidth - this.otherWidth
      // 内容元素需要改变的大小比例 = 当前屏幕尺寸需要变化到标准尺寸的比例 / 标准比例
      this.zoom = Number(innerWidth / this.uiContentWidth)
      // 设置缩放后的宽高
      this.width = innerWidth
    },
    setScale () {
      this.getScale()
      if (this.$refs.ScaleBox) {
        this.$refs.ScaleBox.style.setProperty('--scaleww', this.zoom)
        this.$refs.ScaleBox.style.setProperty('--scalewh', this.zoom)
      }
    },
    debounce (fn, delay) {
      const delays = delay || 500
      let timer
      return function () {
        const th = this
        const args = arguments
        if (timer) {
          clearTimeout(timer)
        }
        timer = setTimeout(function () {
          timer = null
          fn.apply(th, args)
        }, delays)
      }
    }
  }
}
</script>

<style lang="scss">
body {
  &::-webkit-scrollbar {
    display: none;
  }
}
#ScaleBox {
  --scaleww: 1;
  --scalewh: 1;
}
.ScaleBox {
  transform: scale(var(--scaleww), var(--scalewh));
  display: flex;
  flex-direction: column;
  transform-origin: 0 0;
  transition: 0.3s;
  z-index: 3;
}
.no-zoom {
  transform: scale(var(1 / --scaleww), var(1 / --scalewh));
}
</style>


在页面中使用

// 页面文件
<template>
  <div>
  	<PartZoom 
		:uiContentWidth='uiContentWidth'
		:otherWidth='otherWidth'
		:uiContentHeight='uiContentHeight'
	>
		<div>
			此处内容就有了自适应缩放功能了
		</div>
	</PartZoom>
  </div>
</template>
<script>
import PartZoom from './components/PartZoom.vue'
export default {
	components:{PartZoom},
	data(){
		uiContentWidth:'1920', // 标准内容宽度
		otherWidth:'1080', // 标准内容高度
		uiContentHeight:'350' // 其他内容的宽度
	}
}
</script>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue项目做可适应可以尝试以下几个方案: 1. 使用CSS3的transform属性进行 在Vue组件中设置一个容器元素,通过CSS3的transform属性对容器元素进行,可以实现自适应的效果。在容器元素的父元素中监听resize事件,根据父元素的宽度和高度来计算比例,然后通过CSS3的transform属性对容器元素进行。 示例代码: ```html <template> <div class="container" ref="container"> <!-- 可内容 --> </div> </template> <script> export default { mounted() { window.addEventListener('resize', this.handleResize) }, beforeDestroy() { window.removeEventListener('resize', this.handleResize) }, methods: { handleResize() { const container = this.$refs.container const parentWidth = container.parentNode.clientWidth const parentHeight = container.parentNode.clientHeight const contentWidth = container.offsetWidth const contentHeight = container.offsetHeight const scaleX = parentWidth / contentWidth const scaleY = parentHeight / contentHeight container.style.transform = `scale(${Math.min(scaleX, scaleY)})` } } } </script> <style> .container { transform-origin: top left; } </style> ``` 2. 使用Vue的自定义指令进行 在Vue项目中可以创建一个自定义指令,通过该指令来实现自适应的效果。在指令中监听resize事件,根据父元素的宽度和高度来计算比例,然后通过CSS3的transform属性对元素进行。 示例代码: ```html <template> <div class="container" v-resize-scale> <!-- 可内容 --> </div> </template> <script> export default { directives: { resizeScale: { inserted(el) { function handleResize() { const parentWidth = el.parentNode.clientWidth const parentHeight = el.parentNode.clientHeight const contentWidth = el.offsetWidth const contentHeight = el.offsetHeight const scaleX = parentWidth / contentWidth const scaleY = parentHeight / contentHeight el.style.transform = `scale(${Math.min(scaleX, scaleY)})` } window.addEventListener('resize', handleResize) handleResize() }, unbind() { window.removeEventListener('resize', this.handleResize) } } } } </script> <style> .container { transform-origin: top left; } </style> ``` 以上两种方案可以根据实际需求进行选择,第一种方案比较简单,但是需要在组件中手动监听resize事件,第二种方案可以通过Vue的自定义指令来实现效果,使得组件代码更加简洁。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值