vue2指令:滚轮控制图片大小

效果图

在这里插入图片描述

将指令单独写一个文件

在这里插入图片描述

代码

// 图片滚动放大缩小
export const initVWheelScale = (Vue) => {
  Vue.directive("wheelScale", (el, binding) => {
    const {
      maxScale = 5,
      minScale = 0.5,
      initScale = 1,
      cssVarName = "--scale",
    } = binding.arg || {};
    let currentScale = initScale || el.style.getPropertyValue(cssVarName) || 1;
    setWheelScale(binding, {
      el,
      cssVarName,
      currentScale,
      minScale,
      maxScale,
    });
    if (el) {
      el.onwheel = (e) => {
        currentScale = el.style.getPropertyValue(cssVarName) || 1;

        if (e.wheelDelta > 0) {
          currentScale = currentScale * 1 + 0.1;
        } else {
          currentScale = currentScale * 1 - 0.1;
        }
        setWheelScale(binding, {
          el,
          cssVarName,
          currentScale,
          minScale,
          maxScale,
        });
      };
    }
  });
};
// 设置 --scale 变量 缩放比例
const setVarScale = (el, cssVarName, currentScale, minScale, maxScale) => {
  // 现在缩放范围
  if (currentScale > maxScale) {
    currentScale = maxScale;
  } else if (currentScale < minScale) {
    currentScale = minScale;
  }
  let cssText = el.style.cssText;
  let cssTextList = cssText.split(";");
  let isExist = false;
  let isExistIndex = -1;
  for (let index = 0; index < cssTextList.length; index++) {
    const element = cssTextList[index];
    if (element.includes(cssVarName + ":")) {
      isExist = true;
      isExistIndex = index;
      break;
    }
  }
  if (isExist) {
    cssTextList[isExistIndex] = `--scale: ${currentScale}`;
  } else {
    cssTextList.push(`--scale: ${currentScale}`);
    //   el.setAttribute("style", `--scale: ${currentScale}`)
  }
  cssText = cssTextList.join(";");
  el.style.cssText = cssText;
  return currentScale;
};
// 设置 style.transform
const setTransformCss = (el, cssVarName) => {
  let transformCssString = el.style.transform;
  let regScaleGlobal = /scale\(.*?[ )]*[)]+[ ]*/g; //匹配 Scale属性 全局
  if (regScaleGlobal.test(transformCssString)) {
    transformCssString = transformCssString.replace(
      regScaleGlobal,
      ` scale(var(${cssVarName})) `
    );
  } else {
    transformCssString += " " + `scale(var(${cssVarName}))`;
  }
  el.style.transform = transformCssString;
};
export const setWheelScale = (binding = {}, options) => {
  const { el, cssVarName, currentScale, minScale, maxScale } = options;
  const nowScale = setVarScale(
    el,
    cssVarName,
    currentScale,
    minScale,
    maxScale
  );
  setTransformCss(el, cssVarName);
  // 缩放改变回调函数
  const wheelScaleHandle = binding.value || null;
  if (wheelScaleHandle instanceof Function) {
    wheelScaleHandle({
      el,
      cssVarName,
      maxScale,
      minScale,
      currentScale: nowScale,
      setScale: (_scale) => {
        setWheelScale(binding, { ...options, currentScale: _scale });
      },
      binding,
    });
  }
};

在main.js中定义
在这里插入图片描述

// 图片放大缩小
import { initVWheelScale } from "@/directive/wheel-scale/index.js";
initVWheelScale(Vue);

使用

直接用v-wheelScale就可以了
在这里插入图片描述

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值