【问题标题】:React image gallery with magnify effect like Amazon像亚马逊一样具有放大效果的 React 图像库
【发布时间】:2019-05-25 16:35:26
【问题描述】:

我尝试将react-image-galleryreact-image-magnify 结合使用以获得具有放大预览效果的画廊,并根据react-image-gallery 文档,我将MyReactImageMagnify 组件传递给renderItem 组件上的renderItem 道具,但有右侧没有放大图像。

这是带有放大功能的画廊应该看起来像https://www.amazon.com/Samsung-MicroSD-Adapter-MB-ME128GA-AM/dp/B06XWZWYVP

这是我目前拥有的代码框https://codesandbox.io/s/goofy-lumiere-gk1y1

class MyImageGallery extends Component {
  myRenderItem() {
    return <MyReactImageMagnify {...this.props} />;
  }

  render() {
    const properties = {
      thumbnailPosition: "left",
      useBrowserFullscreen: false,
      showPlayButton: false,
      renderItem: this.myRenderItem.bind(this),
      items: [
        {
          original: "https://placeimg.com/640/480/any/1",
          thumbnail: "https://placeimg.com/250/150/any/1"
        },
        {
          original: "https://placeimg.com/640/480/any/2",
          thumbnail: "https://placeimg.com/250/150/any/2"
        },
        {
          original: "https://placeimg.com/640/480/any/3",
          thumbnail: "https://placeimg.com/250/150/any/3"
        }
      ]
    };

    return <ImageGallery {...properties} />;
  }
}

编辑:亚马逊只是为了说明“向右放大”。我制作了另一个带有 2 列网格的代码框,您可以看到普通的 &lt;MyReactImageMagnify /&gt; 组件如何工作而 &lt;MyImageGallery /&gt; 没有。 https://codesandbox.io/s/practical-browser-0dbyo

【问题讨论】:

  • 你想图像预览是固定的,还是像亚马逊一样可变大小?如果这就是你所需要的,固定会容易得多
  • 固定大小就可以了。

标签: reactjs zooming preview image-gallery


【解决方案1】:

我认为问题在于元素溢出。图库使用overflow: hidden 隐藏不可见的幻灯片,因此缩放后的图像也被隐藏了。

react-image-magnify 的好处是您可以指定要在其中呈现大图像的门户。

这里是沙盒:https://codesandbox.io/s/xenodochial-rosalind-g9ve4

创建一个带有 id 的新 div,您希望在其中显示大图像:

<Grid container spacing={4}>
  <Grid item xs={6}>
    <MyImageGallery />
  </Grid>
  <Grid container spacing={2} item xs={6} direction="column">
    <Grid item>
      <div id="myPortal" />
    </Grid>
  </Grid>
</Grid>

画廊的每个元素都将是一个 ReactImageMagnify 组件,其中门户 id 作为属性:

<ReactImageMagnify
  {...{
    smallImage: {
      alt: "Wristwatch by Ted Baker London",
      isFluidWidth: true,
      src: "https://placeimg.com/640/480/any"
    },
    largeImage: {
      src: "https://placeimg.com/640/480/any",
      width: 640,
      height: 480
    },
    enlargedImagePortalId: "myPortal"
  }}
/>

【讨论】:

  • 这就是解决方案。
【解决方案2】:

我检查了您提供的链接中的图片,发现了这个元素:

<div
    id="magnifierLens"
    style="position: absolute; background-image: url(&quot;https://images-na.ssl-images-amazon.com/images/G/01/apparel/rcxgs/tile._CB483369105_.gif&quot;); cursor: pointer; width: 300px; height: 316px; left: 184px; top: 169px;">
</div>

这是用于图像上的蓝色叠加层,然后预览只是一个绝对的div,其中包含原始未调整大小的图像。预览中的图像与放大镜的位置类似。

从这里,您需要获取图像叠加层的比例和图像预览。

看起来亚马逊所做的是保存产品信息元素的维度:

为了保持尺寸,您需要将resize 事件侦听器附加到窗口

window.addEventListener('resize', onWindowResize); // onWindowResize function would get new component size and update child sizes

然后您将跟踪鼠标在图像上的位置:

previewImageElement.addEventListener("mousemove", onMouseMove); // onMouseMove function would update the position of the image overlay, and zoomed image

设置好所有处理程序和侦听器后,您可以计算一些大小并实现它们。

这里是一些计算尺寸的伪代码:

 // Amazons zoom frame has a max size, and it will shrink to fit the zoomed image
const imageOverlayWidth = Math.Max(zoomedFrameWidth / zoomedImageWidth, 1) * imagePreviewWidth;
const imageOverlayWidth = Math.Max(zoomedFrameHeight / zoomedImageHeight, 1) * imagePreviewHeight;

zoomedImageHeightzoomedImageWidth 是图像的自然尺寸。

计算zoomedFrameWidthzoomedFrameHeight 将取决于您希望预览的响应速度。我个人会接受最大尺寸,但不会缩小以适应。

zoomedFrameWidth = productInfoContainerWidth * 0.5;
zoomedFrameHeight = productInfoContainerHeight;

这应该足以将像亚马逊这样的漂亮图像预览缩放结合在一起。让我知道是否有任何需要澄清的地方,因为它大多只是想法和伪代码。

【讨论】:

  • react-image-magnify 应该这样做,在问题中使用另一个代码框查看我的编辑。
猜你喜欢
  • 2011-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多