我有4个按钮,我想把它们分配给每张幻灯片.有4张幻灯片.如果点击每个按钮,它应该滚动到分配给它的指定幻灯片,现在也是如此.我如何实现这一点?

import React, { useState, useRef } from "react";
import "./App.css";

const Slide = ({ slide, current, handleSlideClick }) => {
  const slideRef = useRef();

  let classNames = "slide";

  if (current === slide.index) classNames += " slide--current";
  else if (current - 1 === slide.index) classNames += " slide--previous";
  else if (current + 1 === slide.index) classNames += " slide--next";

  return (
    <li
      ref={slideRef}
      className={classNames}
      onClick={() => handleSlideClick(slide.index)}
    >
      <div className="slide__image-wrapper">
        <img className="slide__image" alt={slide.headline} src={slide.src} />
      </div>

      <article className="slide__content">
        <h2 className="slide__headline">{slide.headline}</h2>
        <button className="slide__action btn">{slide.button}</button>
      </article>
    </li>
  );
};

const Slider = ({ slides, heading }) => {
  const [current, setCurrent] = useState(0);

  const handlePreviousClick = () => {
    const previous = current - 1;
    setCurrent(previous < 0 ? slides.length - 1 : previous);
  };

  const handleNextClick = () => {
    const next = current + 1;
    setCurrent(next === slides.length ? 0 : next);
  };

  const handleSlideClick = (index) => {
    if (current !== index) {
      setCurrent(index);
    }
  };

  const wrapperTransform = {
    transform: `translateX(-${current * (100 / slides.length)}%)`
  };

  return (
    <div className="slider">
      <ul className="slider__wrapper" style={wrapperTransform}>
        {slides.map((slide) => (
          <Slide
            key={slide.index}
            slide={slide}
            current={current}
            handleSlideClick={handleSlideClick}
          />
        ))}
      </ul>

      <div className="slider__controls">
        <button onClick={handlePreviousClick}>1</button>
        <button onClick={handleNextClick}>2</button>
        <button onClick={handlePreviousClick}>3</button>
        <button onClick={handleNextClick}>4</button>
      </div>
    </div>
  );

return <Slider slides={slideData} />;
};

试着像这样简化Slider函数,但无济于事.

const Slider = ({ slides, slideRef }) => {
  const [current, setCurrent] = useState(0);

  const handleSlideChange = (swiper) => {
    setCurrent(swiper.realIndex);
  };

  const handleSlideClick = (index) => {
    if (slideRef.current) {
      slideRef.current.swiper.slideTo(index);
      setCurrent(index);
    }
  };

  const wrapperTransform = {
    transform: `translateX(-${current * (100 / slides.length)}%)`
  };

推荐答案

每个幻灯片都有一个按钮,而不是"上一个"和"下一个"按钮,所以当每个按钮被按下时,它们都应该调用相同的回调,并传递它们的"幻灯片索引"来设置current状态值.

示例:

const Slider = ({ slides, heading }) => {
  const [current, setCurrent] = useState(0);

  const clickHandler = index => () => { // NOTE: this is a curried function
    setCurrent(index);
  };

  const handleSlideClick = (index) => {
    if (current !== index) {
      setCurrent(index);
    }
  };

  const wrapperTransform = {
    transform: `translateX(-${current * (100 / slides.length)}%)`
  };

  return (
    <div className="slider">
      <ul className="slider__wrapper" style={wrapperTransform}>
        {slides.map((slide) => (
          <Slide
            key={slide.index}
            slide={slide}
            current={current}
            handleSlideClick={handleSlideClick}
          />
        ))}
      </ul>

      <div className="slider__controls">
        <button onClick={clickHandler(0)}>1</button>
        <button onClick={clickHandler(1)}>2</button>
        <button onClick={clickHandler(2)}>3</button>
        <button onClick={clickHandler(3)}>4</button>
      </div>
    </div>
  );
};

Javascript相关问答推荐

Angular material 表多个标题行映射

如何编辑代码FlipDown.js倒计时?

在贝塞尔曲线的直线上找不到交叉点:(使用@Pomax的bezier.js)

通过使用100%间隔时间来代表我们还剩多少时间来倒计时

类型脚本中只有字符串或数字键而不是符号键的对象

如何解决CORS政策的问题

未捕获错误:在注销后重定向到/login页面时找不到匹配的路由

微软Edge编辑和重新发送未显示""

使搜索栏更改语言

给定一个凸多边形作为一组边,如何根据到最近边的距离填充里面的区域

在grafana情节,避免冲突的情节和传说

数字时钟在JavaScript中不动态更新

并不是所有的iframe都被更换

PDF工具包阿拉伯字体的反转数字

如何在ASP.NET JavaScript中使用Google Charts API仅对绘制为负方向的条形图移动堆叠条形图标签位置

用JS从平面文件构建树形 struct 的JSON

使用auth.js保护API路由的Next.JS,FETCH()不起作用

按下单键和多值

如何根据查询结果重新排列日期

我的NavLink活动类在REACT-ROUTER-V6中出现问题