前端大屏自适应实现高清还原设计图 scale

前段时间写过一个实现大屏的 比较简陋 也有些不足的地方 重新优化了一下
可以根据你的屏幕尺寸进行大屏展示 但是必须是16:9的设计图

 <page-resize>
 	//页面dom
 </page-resize>
import pageResize from '@/pages/intelligentoperation/resetScreenSize.vue'

resetScreenSize.vue

components: {
    pageResize
  },
<template>
  <div v-bind:style="styleObject" class="scale-box">
    <slot></slot>
  </div>
</template>

<script>
import debounce from "lodash.debounce"
// const that = this;
export default {
  props: {
    width: {
      type: Number,
      default: 1920,
    },
    height: {
      type: Number,
      default: 1080,
    },
  },
  components: {},
  data () {
    return {
      scale: this.getScale(),
    }
  },
  computed: {
    styleObject () {
      let obj = {
        transform: `scale(${this.scale}) translate(-50%, -50%)`,
        WebkitTransform: `scale(${this.scale}) translate(-50%, -50%)`,
        width: this.width + "px",
        height: this.height + "px",
      }
      return obj
    }

  },
  mounted () {
    this.getScale()
    window.addEventListener("resize", this.setScale)
  },
  methods: {
    getScale () {
      // 固定好16:9的宽高比,计算出最合适的缩放比,宽高比可根据需要自行更改
      console.log(window.innerWidth, 'window.innerWidth');
      let ww = window.innerWidth / this.width
      let wh = window.innerHeight / this.height
      return ww < wh ? ww : wh
    },

    setScale: debounce(function () {

      // 获取到缩放比,设置它
      let scale = this.getScale()
      this.scale = scale
      console.log(this.scale, "---")
    }, 10),
  },
  beforeDestroy () {
    window.addEventListener("resize", this.setScale)
  },
}
</script>

<style scoped lang="scss">
.scale-box {
  transform-origin: 0 0;
  position: absolute;
  left: 50%;
  top: 50%;
  transition: 0.3s;
}
</style>


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于前端头部图片的自适应,可以通过以下几种方式实现: 1. 使用 CSS 的 background-size 属性: 在 CSS 中,可以通过设置 background-size 属性来控制背景图片的大小。可以将该属性设置为 cover,这样背景图片会按比例缩放以填满整个容器,并保持其宽比。例如: ```css .header { background-image: url('path/to/image.jpg'); background-size: cover; background-position: center; } ``` 这样设置后,背景图片会自适应容器大小,并保持图片的宽比例。 2. 使用 CSS 的 object-fit 属性: object-fit 属性用于设置替换元素(如 <img>)的内容的尺寸调整方式。可以将该属性设置为 cover,这样图片会按比例调整大小以完全填充容器,并保持其宽比。例如: ```css .header img { width: 100%; height: 100%; object-fit: cover; } ``` 这样设置后,图片会自适应容器大小,并保持图片的宽比例。 3. 使用媒体查询(Media Queries): 如果需要在不同的幕尺寸下显示不同大小的头部图片,可以使用媒体查询来设置不同的 CSS 样式。例如: ```css @media (max-width: 768px) { .header { background-image: url('path/to/small-image.jpg'); } } @media (min-width: 769px) { .header { background-image: url('path/to/large-image.jpg'); } } ``` 这样设置后,当幕宽度小于等于 768px 时,会显示小图片;当幕宽度大于 768px 时,会显示大图片。 这些方法可以根据实际需求选择使用,以实现前端头部图片的自适应效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值