点击react-image-gallery上的图像并进入全屏模式。

huangapple go评论73阅读模式
英文:

click on image on react-image-gallery and go fullscreen

问题

The Chinese translation of the code snippet you provided is as follows:

import "../../node_modules/react-image-gallery/styles/css/image-gallery.css";
import ImageGallery from "react-image-gallery";
import { useRef } from "react";

const images = [
  {
    original: "https://picsum.photos/id/1018/1000/600/",
    thumbnail: "https://picsum.photos/id/1018/250/150/",
  },
  {
    original: "https://picsum.photos/id/1015/1000/600/",
    thumbnail: "https://picsum.photos/id/1015/250/150/",
  },
  {
    original: "https://picsum.photos/id/1019/1000/600/",
    thumbnail: "https://picsum.photos/id/1019/250/150/",
  },
];
const Carousel = (props) => {
  const imageGalleryRef = useRef(null);

  const onClickHandler = () => {
    imageGalleryRef.current.toggleFullscreen();
  };

  return (
    <ImageGallery
      items={images}
      showThumbnails={true}
      showFullscreenButton={true}
      showPlayButton={false}
      showBullets={true}
      ref={imageGalleryRef}
      onClick={onClickHandler}
    />
  );
};

export default Carousel;

Regarding the error message you're encountering (imageGalleryRef.current.toggleFullscreen is not a function), it seems like the toggleFullscreen function is not available at the time you're trying to call it. Ensure that the ImageGallery component properly initializes and provides this function. If you can run it from the console, it might be related to the timing of when the component is fully mounted and ready.

Regarding implementing clicking on the image and going fullscreen, the approach you've taken with the onClickHandler and toggleFullscreen is generally correct. However, without knowing more about the specific behavior you want, it's challenging to determine if it's the "best" way. This approach allows you to toggle fullscreen when clicking on the image, which is a common use case.

英文:
import &quot;../../node_modules/react-image-gallery/styles/css/image-gallery.css&quot;;
import ImageGallery from &quot;react-image-gallery&quot;;
import { useRef } from &quot;react&quot;;

const images = [
  {
    original: &quot;https://picsum.photos/id/1018/1000/600/&quot;,
    thumbnail: &quot;https://picsum.photos/id/1018/250/150/&quot;,
  },
  {
    original: &quot;https://picsum.photos/id/1015/1000/600/&quot;,
    thumbnail: &quot;https://picsum.photos/id/1015/250/150/&quot;,
  },
  {
    original: &quot;https://picsum.photos/id/1019/1000/600/&quot;,
    thumbnail: &quot;https://picsum.photos/id/1019/250/150/&quot;,
  },
];
const Carousel = (props) =&gt; {
  const imageGalleryRef = useRef(null);

  const onClickHandler = () =&gt; {
    imageGalleryRef.current.toggleFullscreen();
  };

  return (
    &lt;ImageGallery
      items={images}
      showThumbnails={true}
      showFullscreenButton={true}
      showPlayButton={false}
      showBullets={true}
      ref={imageGalleryRef}
      onClick={onClickHandler}
    /&gt;
  );
};

export default Carousel;

this returns imageGalleryRef.current.toggleFullscreen is not a function
but I can run that function from the console and it works, what am I doing wrong?
also is this the best way to implement clicking on the image and going fullscreen?

答案1

得分: 1

应该使用fullScreen而不是toggleFullscreen。根据函数文档

可以使用 refs 访问以下函数:

  • play(): 播放幻灯片
  • pause(): 暂停幻灯片
  • fullScreen(): 激活全屏
  • exitFullScreen(): 退出全屏
  • slideToIndex(index): 切换到特定索引
  • getCurrentIndex(): 返回当前索引
英文:

It should be fullScreen instead of toggleFullscreen. As per the documentation on functions:

> The following functions can be accessed using refs
>
> - play(): plays the slides
> - pause(): pauses the slides
> - fullScreen(): activates full screen
> - exitFullScreen(): deactivates full screen
> - slideToIndex(index): slides to a specific index
> - getCurrentIndex(): returns the current index

huangapple
  • 本文由 发表于 2023年5月10日 17:48:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定