JQ实现图片放大缩小左右旋转(React)

这篇博客探讨了如何使用HTML和React进行图片操作,包括图片的缩放、旋转功能。通过DOM操作,利用JavaScript实现点击按钮对图片进行大小调整和角度旋转。示例代码展示了具体的实现细节。

HTML代码:

      <div>
      <img id = 'imgage' src= '图片路径'  style={{ margin: 'auto'}} />
      <Button onClick = {()=>this.imgToSize(100)}>放大</Button>
      <Button onClick = {()=>this.imgToSize(-100)}>缩小</Button>
      <Button onClick = {()=>this.imgRotate('left')}>向左旋转</Button>
      <Button onClick = {()=>this.imgRotate('right')}>向右旋转</Button>
      </div>

对应的操作函数:

  // 图片缩放
    imgToSize(size) {
    const img = $("#image"); //获取对应的dom
    const oWidth = img.width(); //取得图片的实际宽度
    const oHeight = img.height(); //取得图片的实际高度
    img.width(oWidth + size); //设置图片宽度
    img.height(oHeight + size / oWidth * oHeight);//设置图片高度
  }
  // 旋转图片
  imgRotate(direction) {
    const img = $("#image");
    if (direction == "left") {
        current = (current - 90) % 360; //特别注意:这里的current是用来记录每次的旋转角度,所以需要定义为全局变量来记录(React可以放入state中,每次执行完后更新一次)
    }
    else if (direction == "right") {
        current = (current + 90) % 360;
    }
   img.css('transform', 'rotate(' + current + 'deg)');
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值