0


前端实现大屏缩放自适应屏幕

在前端实现大屏缩放自适应屏幕的过程中,可以使用以下几种方式:

  1. 使用CSS3的缩放属性(transform: scale())来缩放页面元素,以适应不同大小的屏幕。缩放后,需要使用CSS来重新布局和调整页面元素的尺寸和位置。
  2. 使用CSS的@media查询来根据不同的屏幕大小调整元素的样式和布局。例如,在@media (max-width: 768px)中,可以设置针对小屏幕的样式和布局。
  3. 使用JavaScript来根据窗口大小调整元素的样式和布局。可以通过window.addEventListener监听resize事件,然后在事件处理程序中根据窗口大小进行调整。

需要注意的是,在使用上述方法时,需要考虑到元素的排版和布局,不要因为缩放而导致元素重叠或者间距过大。同时,也需要权衡页面的性能和效果,避免使用过多的CSS和JavaScript导致页面加载过慢。

前端实现大屏缩放自适应屏幕思路:
页面初始化获取屏幕的原始比例 将自适应元素的scale变量设置为当前比例 监听浏览器窗口大小,获取新的比例值重新给元素scale赋值

<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
      returnfunction(){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:00;
  transition:0.3s;
  z-index:3;}.no-zoom {
  transform:scale(var(1/--scaleww),var(1/--scalewh));}</style>
标签: 前端

本文转载自: https://blog.csdn.net/wgq2020/article/details/134177311
版权归原作者 愚公搬程序 所有, 如有侵权,请联系我们删除。

“前端实现大屏缩放自适应屏幕”的评论:

还没有评论