react hooks 实现商品详情页 鼠标移入图片中,右边放大显示

介绍:

实现商品详情页 鼠标移入图片中,右边放大显示

效果

直接使用需要引入 primereact UI框架 ,如果只是作为dome,需要修改banner组件(Galleria)

npm install primereact
npm install primeicons

js代码如下:

import { useEffect, useState, useRef } from "react";
import styles from './productDetail.module.css';
import { Galleria } from 'primereact/galleria';
export default function ProductDetail() {
  const [bubblesObj, setBubblesObj] = useState({})
  const [images, setImages] = useState([{
    url: "https://www.primefaces.org/primereact-v6/showcase/demo/images/galleria/galleria1.jpg",
    alt: "1"
  }, {
    url: "https://www.primefaces.org/primereact-v6/showcase/demo/images/galleria/galleria2.jpg",
    alt: "2"
  }]);
  
  const onMousemove = (e) => {
    let x, y
    let canvas = document.getElementById('canvas');
    let positioning = document.getElementById("positioning");
    var event = e.nativeEvent;

    if (event.offsetX || event.offsetY) {  //适用非Mozilla浏览器
      x = event.offsetX;
      y = event.offsetY;
    } else if (event.layerX || event.layerY) {  //兼容Mozilla浏览器
      x = event.layerX;
      y = event.layerY;
    }
    let top = 0
    let left = 0;
    if (x < 100) {

      left = 0;
    } else if (x > 250) {
      left = 150;
    } else {
      left = x - 100;
    }

    if (y - 70 < 0) {
      top = 0;
    } else if (y - 70 > 210) {
      top = 210;
    } else {
      top = y - 70;
    }
    positioning.style.left = left + 'px';
    positioning.style.top = top + 'px';
    canvas.style.left = -3.5 * left + 'px';
    canvas.style.top = -1.666 * top + 'px';


  }

  const onMouseEnter = (params) => {
    let _bubblesObj = { ...bubblesObj }
    if (!_bubblesObj[params]) {
      _bubblesObj[params] = true
      setBubblesObj(_bubblesObj)
    }
  }


  const onMouseLeave = (params) => {
    let _bubblesObj = { ...bubblesObj }
    _bubblesObj[params] = false;
    setBubblesObj(_bubblesObj)
  }


  const itemTemplate = (item) => {
    return <img
      onMouseEnter={() => onMouseEnter("banner")}
      onMouseLeave={() => onMouseLeave("banner")}
      src={item.url}
      alt={item.alt}
      style={{ width: "350px", height: "350px", }} />
  }
  const thumbnailTemplate = (item) => {
    return <img src={item.url} alt={item.alt} style={{ width: '100%', display: 'block' }} />
  }
  

  return (<div className={styles.page}>
    
    <div className={styles.details}>
      <div className={styles.leftBanner} onMouseMove={(e) => onMousemove(e)}>
        <Galleria value={images}
          numVisible={5}
          style={{ width: '400px', height: "400px" }}
          item={itemTemplate}
          thumbnail={thumbnailTemplate} />
        <div style={{ display: bubblesObj.banner ? "block" : "none" }} id="positioning" className={styles.positioningBox}> </div>
      </div>

      <div className={styles.Popup} style={{ display: bubblesObj.banner ? "block" : "none" }}>
        <img src='https://www.primefaces.org/primereact-v6/showcase/demo/images/galleria/galleria1.jpg' id={"canvas"} className={styles.canvasBox} />
        </div>
        </div>
  </div>)
}

css代码如下
productDetail.module.css

.page {
  min-height: 700px;
  display: flex;
  /* justify-content: center; */
  align-items: center;
  background-color: #fff;
  flex-direction: column;
}
.pageBox {
  min-width: 1500px;
  display: flex;
  flex-direction: column;
  padding: 20px 0;
  position: relative;
}
.router {
  display: flex;
  align-items: center;
}
.routerText {
  font-size: 16px;
  color: #333;
}

.details {
  display: flex;
  justify-content: space-between;
  position: relative;
}

.leftBanner {
  display: flex;
  position: relative;
  background-color: #ff6a00;
  width: 350px;
  height: 350px;
}

.canvasBox {
  position: absolute;
  left: 600px;
  top: 100px;
  width: 525px;
  height: 350px;
  transform: scale(2);
  /* transform-origin: top left; */
  transform-origin: 0 0 0;
}
.positioningBox {
  width: 200px;
  height: 140px;
  position: absolute;
  /* left: -0px;
  top: 0px; */
  border: 1px solid rgb(255, 106, 0);
  background-color: rgba(255, 106, 0, 0.2);
  pointer-events: none;
}
.Popup {
  width: 525px;
  height: 350px;
  position: absolute;
  left: 350px;
  /* background-color: #ff6a00; */
  display: inline-block;
  overflow: hidden;
}

如果觉得有用,一键三连走一波 ☺~~~

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现点击商品跳转到详情页的功能,你可以使用React Router库来管理路由。首先,你需要在你的应用程序安装React Router,然后在你的代码导入它。 然后,你可以创建一个商品列表组件,这个组件会显示所有的商品。在这个组件,你可以使用React Router的Link组件创建每个商品的链接,这个链接会在被点击时跳转到对应的商品详情页。例如: ```jsx import { Link } from "react-router-dom"; function ProductList({ products }) { return ( <div> {products.map(product => ( <div key={product.id}> <h2>{product.name}</h2> <p>{product.description}</p> <Link to={`/products/${product.id}`}>View details</Link> </div> ))} </div> ); } ``` 这个Link组件会渲染成一个a标签,当它被点击时,React Router会在应用程序的URL添加商品ID,例如"/products/123",并加载相应的商品详情组件。 接下来,你需要在你的应用程序定义路由规则,以告诉React Router应该在哪些URL上加载哪些组件。这可以在应用程序的根组件完成,例如: ```jsx import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; function App() { return ( <Router> <Switch> <Route path="/products/:productId"> <ProductDetails /> </Route> <Route path="/"> <ProductList products={...} /> </Route> </Switch> </Router> ); } ``` 这个例子,我们定义了两个路由规则,一个是匹配"/products/:productId"的,另一个是匹配根路径"/"的。当URL匹配到这些规则时,React Router会加载相应的组件。 希望这可以回答你的问题!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值