放大镜图片放大(淘宝图片放大)

app.js

import React, { useState } from 'react'
import img01 from './images/01.png'
import img02 from './images/02.png'
import img03 from './images/03.png'

export default function App() {

  const [ind, setIndex] = useState(0)

  const imgs = [img01, img02, img03]

  const onMouseMoveList = (e, index) => {
    setIndex(index)
  }

  const onMouseMovePic = (e) => {
    let pic = document.getElementsByClassName('pic')[0]
    let cover = document.getElementsByClassName('cover')[0]
    let detail = document.getElementsByClassName('detail')[0]
    var top = e.clientY,
      left = e.clientX,
      py = pic.getBoundingClientRect().top,
      px = pic.getBoundingClientRect().left,
      tx = left - px - 75,
      ty = top - py - 75;
    if (tx < 0) {
      tx = 0
    }
    if (tx > 250) {
      tx = 250
    }
    if (ty < 0) {
      ty = 0
    }
    if (ty > 250) {
      ty = 250
    }
    cover.style.cursor = 'move'
    cover.style.top = ty + 'px'
    cover.style.left = tx + 'px'
    detail.style.backgroundPosition = tx / 250 * 100 + '% ' + ty / 250 * 100 + '%'
    detail.style.display = 'block'
    if (ind === 0) {
      detail.style.backgroundImage = `url(${img01})`
    } else if (ind === 1) {
      detail.style.backgroundImage = `url(${img02})`
    } else if (ind === 2) {
      detail.style.backgroundImage = `url(${img03})`
    }
  }


  return (
    <div className='App'>
      <div className="itemarea">
        <div className="pic"
          onMouseMove={onMouseMovePic}
        >
          {
            ind === 0 ?
              <img src={img01} alt="" />
              : ind === 1 ?
                <img src={img02} alt="" />
                :
                <img src={img03} alt="" />
          }
          <div className="cover"></div>
        </div>
        <ul className='list'>
          {
            imgs.map((img, index) => {
              return (
                <li
                  className={index === ind ? 'current' : ''}
                  key={index}
                  onMouseMove={(e) => onMouseMoveList(e, index)}
                >
                  <img src={img} alt="" />
                </li>
              )
            })
          }
        </ul>
        <div className="detail">

        </div>
      </div>
    </div>
  )
}



app.css

* {
  margin: 0;
  padding: 0;
}

#root {
  height: 100vh;
}

#root>.App {
  overflow: hidden;
  height: 100vh;
  background-color: #d8e7fa;
}

ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

.itemarea {
  position: relative;
  width: 400px;
  height: 480px;
  border: 1px #888 solid;
  margin: 100px auto;
}

.itemarea .pic {
  margin-bottom: 15px;
}

.itemarea .pic img {
  width: 400px;
  height: 400px;
}

.itemarea .pic .cover {
  position: absolute;
  top: 0;
  left: 0;
  width: 150px;
  height: 150px;
  opacity: .7;
  background-color: #88b6e1;

}

.itemarea .list {
  display: flex;
}

.itemarea .list li {
  margin: auto;
  font-size: 0;
  border: 2px solid white;

}

.itemarea .list img {
  width: 50px;
  height: 50px;
}

.itemarea .list .current {
  font-size: 0;
  border: 2px solid red;
}

.itemarea .detail {
  width: 400px;
  height: 400px;
  display: none;
  border: 1px #888 solid;
  position: absolute;
  top: 0;
  left: 400px;
  /* background: url("./images/01.png") no-repeat; */
  background-size: 300%;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

No DeBug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值