等比例缩放1920:1080

unique-yaobo / 2025-01-15 / 原文

1920:1080根据屏幕的改变进行等比例缩放
const windowsChangeSetScale = () => {
  const containerWidth = window.innerWidth
  const containerHeight = window.innerHeight

  // 目标宽高比
  const boxAspectRatio = 1920 / 1080

  // 计算容器的宽高比
  const containerAspectRatio = containerWidth / containerHeight

  let scale
  if (containerAspectRatio > boxAspectRatio) {
    // 容器宽高比大于盒子,说明窗口更宽,需要根据高度缩放
    scale = containerHeight / 1080
  } else {
    // 容器宽高比小于或等于盒子,说明窗口更高或正好匹配宽度
    scale = containerWidth / 1920
  }

  // 设置缩放
  if (MathExampleDiv.value) {
    MathExampleDiv.value.style.transform = `scale(${scale})`
    MathExampleDiv.value.style.transformOrigin = 'top left' // 保证从左上角进行缩放
  }
}