• E.Evan's 博客.
  • 前端相关
  • 算法题
  • 日常笔记
  • Chat With AI
  • 博客AI助手
Evan Wang
Evan Wang前端开发 · 个人学习手记

慢一点,也是在向前。

文章目录

获取 URL 中的参数将对象转换为 url 字符串类型限制输入框只能输入数字利用 CSS 实现点击放大缩小元素前端实现水印大文件上传

  1. 首页
  2. 日常笔记
  3. Utils
  4. 利用 CSS 实现点击放大缩小元素
© 2026 Evan 的博客
用心记录,保持好奇。Made with simplicity.
旧版 Blog 链接
598 字
约 2 分钟
更新于 2026/02/27

CSS 实现点击放大、缩小指定元素

// 放大
const handleZoomIn = (element) => {
    if (!element) {
        return
    }
    const img = viewImageContainerRef.current
 
    const currentScale = img.style.transform ? parseFloat(img.style.transform.replace(/[^\d.]/g, '')) || 1 : 1
    const newScale = currentScale * 1.2 > 5 ? 5 : currentScale * 1.2
    img.style.transform = `scale(${newScale})`
}
 
// 缩小
const handleZoomOut = (element) => {
    if (!element) {
        return
    }
    const img = viewImageContainerRef.current
    const currentScale = img.style.transform ? parseFloat(img.style.transform.replace(/[^\d.]/g, '')) || 1 : 1
    const newScale = currentScale * 0.8 < 0.2 ? 0.2 : currentScale * 0.8
    img.style.transform = `scale(${newScale})`
}