一个vue mixin 小案例,实现等比例缩放

打印 上一主题 下一主题

主题 963|帖子 963|积分 2889

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
mixin.js

  1. /*
  2. * @Author: jinjianwei
  3. * @Date: 2024-07-24 16:17:16
  4. * @Description: 等比例缩放,屏幕适配 mixin 函数
  5. */
  6. // * 默认缩放值
  7. const scale = {
  8.   width: '1',
  9.   height: '1',
  10. }
  11. // * 设计稿尺寸(px)
  12. const baseWidth = 1920
  13. const baseHeight = 1080
  14. // * 需保持的比例(默认1.77778)
  15. const baseProportion = parseFloat((baseWidth / baseHeight).toFixed(5))
  16. export default {
  17.   data() {
  18.     return {
  19.       // * 定时函数
  20.       drawTiming: null,
  21.     }
  22.   },
  23.   mounted() {
  24.     //进入触发
  25.     this.calcRate()
  26.     window.addEventListener('resize', this.resize)
  27.   },
  28.   beforeDestroy() {
  29.     window.removeEventListener('resize', this.resize)
  30.   },
  31.   computed: {
  32.     getRef() {
  33.       return null
  34.     }
  35.   },
  36.   methods: {
  37.     calcRate() {
  38.       //拿到整个页面元素
  39.       let appRef = this.getRef
  40.       //如果没有值就结束
  41.       if (!appRef) return
  42.       // 当前宽高比
  43.       const currentRate = parseFloat((window.innerWidth / window.innerHeight).toFixed(5))
  44.       //判断:如果有值代表页面变化了
  45.       if (appRef) {
  46.         //判断当前宽高比例是否大于默认比例
  47.         if (currentRate > baseProportion) {
  48.           // 如果大于代表更宽了,就是放大了
  49.           //那么把默认缩放的宽高改为:同比例放大
  50.           scale.width = ((window.innerHeight * baseProportion) / baseWidth).toFixed(5)
  51.           scale.height = (window.innerHeight / baseHeight).toFixed(5)
  52.           console.log(scale.width, scale.height, '放大');
  53.           //整个页面的元素样式,缩放宽高用当前同比例放大的宽高
  54.           appRef.style.transform = `scale(${scale.width}, ${scale.height})`
  55.         } else {
  56.           // 如果不大于默认比例代表缩小了。
  57.           //那么把默认缩放的宽高改为:同比例缩小
  58.           scale.height = ((window.innerWidth / baseProportion) / baseHeight).toFixed(5)
  59.           scale.width = (window.innerWidth / baseWidth).toFixed(5)
  60.           console.log(scale.width, scale.height, '缩小');
  61.           //整个页面的元素样式,缩放宽高用当前同比例放大的宽高
  62.           appRef.style.transform = `scale(${scale.width}, ${scale.height})`
  63.         }
  64.         //等dom节点加载完后执行
  65.         this.$nextTick(() => {
  66.           //appRef.getBoundingClientRect() 为获取当前div容器距离windows视图的上边距与左边距
  67.           let appRefBoundingClientRect = appRef.getBoundingClientRect()
  68.           // 第一种方式
  69.           // let finallyTop = this.prevAppRefBoundingClientRect.top === 0 ? -appRefBoundingClientRect.top : (this.prevAppRefBoundingClientRect.top - appRefBoundingClientRect.top)
  70.           // let finallyLeft = this.prevAppRefBoundingClientRect.left === 0 ? -appRefBoundingClientRect.left : (this.prevAppRefBoundingClientRect.left - appRefBoundingClientRect.left)
  71.           // appRef.style.top = `${finallyTop}px`
  72.           // appRef.style.left = `${finallyLeft}px`
  73.           // this.prevAppRefBoundingClientRect.top = finallyTop
  74.           // this.prevAppRefBoundingClientRect.left = finallyLeft
  75.           // 第二种方式
  76.           let finallyTop = 0;
  77.           let finallyLeft = 0;
  78.           if (this.isFirst) {
  79.             // 第一次缩放偏移量
  80.             finallyTop = appRefBoundingClientRect.top
  81.             finallyLeft = appRefBoundingClientRect.left
  82.             this.isFirst = false;
  83.           } else {
  84.             // 第二次变化后的缩放偏移量
  85.             finallyTop = this.prevAppRefBoundingClientRect.top * (1 - scale.height) / (1 - this.scalePrev)
  86.             finallyLeft = this.prevAppRefBoundingClientRect.left * (1 - scale.height) / (1 - this.scalePrev)
  87.           }
  88.           // 设置缩放元素偏移量
  89.           appRef.style.top = `${-finallyTop}px`;
  90.           appRef.style.left = `${-finallyLeft}px`;
  91.           this.scalePrev = scale.width;
  92.           this.prevAppRefBoundingClientRect.top = finallyTop
  93.           this.prevAppRefBoundingClientRect.left = finallyLeft
  94.         });
  95.       }
  96.     },
  97.     resize() {
  98.       clearTimeout(this.drawTiming)
  99.       this.drawTiming = setTimeout(() => {
  100.         this.calcRate()
  101.       }, 200)
  102.     }
  103.   }
  104. };
复制代码
这里留意要拿到引用组件的dom元素,必要用计算属性。
引用组件里的代码

  1. // html
  2. <div  ref="domRef" id="index"><div>
  3. // css
  4. #index {
  5.   color: #d3d6dd;
  6.   //此处的宽高就是你设计稿的尺寸
  7.   width: 1920px;
  8.   height: 1080px;
  9.   //绝对定位 脱离标准流
  10.   position: absolute;
  11.   //分别将 div容器向左 和 向下 移动50%
  12.   top: 50%;
  13.   left: 50%;
  14.   // 设置以容器左上角为中心,进行缩放移动
  15.   transform-origin: left top;
  16.   //再将容易往反方向分别移动50%,这样div容器 一直处于可视窗口中心
  17.   transform: translate(-50%, -50%);
  18.   //超出部位隐藏
  19.   overflow: hidden;
  20. }
  21. // js
  22. import studioMixin from "../../mixin";
  23. mixins: [studioMixin],
  24. computed: {
  25.     getRef(){
  26.       return this.$refs.domRef
  27.     }
  28.   },
复制代码
参考

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

曹旭辉

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表