5

我正在使用 react-slick 在我的项目中创建一个轮播。我已经阅读了文档并尝试了不同的方法,但找不到完全按照我需要的方式自定义它的方法......有谁知道是否有办法让 nextArrow 显示在图像上/前面而不是这是正确的?有关所需结果,请参见下图: image

谢谢你的帮助!

4

9 回答 9

12

我遇到了同样的问题,并一直在尝试按照这个CustomArrows 文档和其他一些建议来寻找解决方案,但它们都没有像我想要的那样工作(使用不同的箭头图标并在幻灯片顶部显示箭头) . 然后我尝试按照这个previousNextMethods 文档,并尝试从那里调整它。

index.js

    renderArrows = () => {
    return (
      <div className="slider-arrow">
        <ButtonBase
          className="arrow-btn prev"
          onClick={() => this.slider.slickPrev()}
        >
          <ArrowLeft />
        </ButtonBase>
        <ButtonBase
          className="arrow-btn next"
          onClick={() => this.slider.slickNext()}
        >
          <ArrowRight />
        </ButtonBase>
      </div>
    );
  };
  render() {
    return (
      <div className="App">
        <div style={{ position: "relative", marginTop: "2rem" }}>
          {this.renderArrows()}
          <Slider
            ref={c => (this.slider = c)}
            dots={true}
            arrows={false}
            centerMode={true}
            slidesToShow={2}
          >
            <div>
              <img src="http://placekitten.com/g/400/200" alt="cat" />
            </div>
            <div>
              <img src="http://placekitten.com/400/200" alt="cat" />
            </div>
            <div>
              <img src="http://placekitten.com/g/400/200" alt="cat" />
            </div>
            <div>
              <img src="http://placekitten.com/400/200" alt="cat" />
            </div>
          </Slider>
        </div>
      </div>
    );
  }

样式.css

.App {
font-family: sans-serif;
}

.slider-arrow {
  position: absolute;
  z-index: 1;
  height: 100%;
  width: 100%;
}

.arrow-btn {
  top: 45%;
}
.next {
  float: right;
}

我希望这将有所帮助。密码箱

于 2019-12-08T05:33:46.973 回答
10

请参阅官方文档 https://react-slick.neostack.com/docs/example/custom-arrows/ https://react-slick.neostack.com/docs/example/previous-next-methods 那里的代码:

import React, { Component } from "react";
import Slider from "react-slick";

function SampleNextArrow(props) {
  const { className, style, onClick } = props;
  return (
    <div
      className={className}
      style={{ ...style, display: "block", background: "red" }}
      onClick={onClick}
    />
  );
}

function SamplePrevArrow(props) {
  const { className, style, onClick } = props;
  return (
    <div
      className={className}
      style={{ ...style, display: "block", background: "green" }}
      onClick={onClick}
    />
  );
}

export default class CustomArrows extends Component {
  render() {
    const settings = {
      dots: true,
      infinite: true,
      slidesToShow: 3,
      slidesToScroll: 1,
      nextArrow: <SampleNextArrow />,
      prevArrow: <SamplePrevArrow />
    };
    return (
      <div>
        <h2>Custom Arrows</h2>
        <Slider {...settings}>
          <div>
            <h3>1</h3>
          </div>
          <div>
            <h3>2</h3>
          </div>
          <div>
            <h3>3</h3>
          </div>
          <div>
            <h3>4</h3>
          </div>
          <div>
            <h3>5</h3>
          </div>
          <div>
            <h3>6</h3>
          </div>
        </Slider>
      </div>
    );
  }
}

或者

import React, { Component } from "react";
import Slider from "react-slick";

export default class PreviousNextMethods extends Component {
  constructor(props) {
    super(props);
    this.next = this.next.bind(this);
    this.previous = this.previous.bind(this);
  }
  next() {
    this.slider.slickNext();
  }
  previous() {
    this.slider.slickPrev();
  }
  render() {
    const settings = {
      dots: true,
      infinite: true,
      speed: 500,
      slidesToShow: 1,
      slidesToScroll: 1
    };
    return (
      <div>
        <h2>Previous and Next methods</h2>
        <Slider ref={c => (this.slider = c)} {...settings}>
          <div key={1}>
            <h3>1</h3>
          </div>
          <div key={2}>
            <h3>2</h3>
          </div>
          <div key={3}>
            <h3>3</h3>
          </div>
          <div key={4}>
            <h3>4</h3>
          </div>
          <div key={5}>
            <h3>5</h3>
          </div>
          <div key={6}>
            <h3>6</h3>
          </div>
        </Slider>
        <div style={{ textAlign: "center" }}>
          <button className="button" onClick={this.previous}>
            Previous
          </button>
          <button className="button" onClick={this.next}>
            Next
          </button>
        </div>
      </div>
    );
  }
}
于 2019-09-15T19:22:43.440 回答
3

我倾向于禁用箭头并自己构建它们。

创建参考

const slider = React.useRef(null);

连接到滑块

<Slider ref={slider} {...settings}>

随心所欲地添加按钮

<button onClick={() => slider?.current?.slickPrev()}>Prev</button>
<button onClick={() => slider?.current?.slickNext()}>Next</button>
于 2021-06-15T11:06:09.167 回答
2

你可以试试这个方法

render() {
    const ArrowLeft = (props) => (
        <button
            {...props}
            className={'prev'}/>
    );
    const ArrowRight = (props) => (
        <button
            {...props}
            className={'next'}/>
    );

    const settings = {
        arrows: true,
        prevArrow: <ArrowLeft />,
        nextArrow: <ArrowRight />,
    };

    return (
        <Slider {...settings}>
            {/* items... */}
        </Slider>
    )
}
于 2019-07-23T16:10:14.323 回答
0
import React, { Component } from "react";
import Slider from "react-slick";

function SampleNextArrow(props) {
  const { className, style, onClick } = props;
  return (
    <div
      className={className}
      style={{ ...style, display: "block", background: "red" }}
      onClick={onClick}
    />
  );
}

function SamplePrevArrow(props) {
  const { className, style, onClick } = props;
  return (
    <div
      className={className}
      style={{ ...style, display: "block", background: "green" }}
      onClick={onClick}
    />
  );
}

export default class CustomArrows extends Component {
  render() {
    const settings = {
      dots: true,
      infinite: true,
      slidesToShow: 3,
      slidesToScroll: 1,
      nextArrow: <SampleNextArrow />,
      prevArrow: <SamplePrevArrow />
    };
    return (
      <div>
        <h2>Custom Arrows</h2>
        <Slider {...settings}>
          <div>
            <h3>1</h3>
          </div>
          <div>
            <h3>2</h3>
          </div>
          <div>
            <h3>3</h3>
          </div>
          <div>
            <h3>4</h3>
          </div>
          <div>
            <h3>5</h3>
          </div>
          <div>
            <h3>6</h3>
          </div>
        </Slider>
      </div>
    );
  }
}

在此处检查 React-slick Custom Next & Prev 按钮:https ://react-slick.neostack.com/docs/example/custom-arrows

于 2019-10-22T05:22:14.453 回答
0

添加自定义css样式

.slick-next {
  background: url('./images/next.png') center center no-repeat!important;

  &::before {
    display: none;
  }
}

.slick-prev {
  background: url('./images/back.png') center center no-repeat!important;

  &::before {
    display: none;
  }
}
于 2019-12-03T14:04:03.510 回答
0

请记住,需要将 onClick 道具添加到您的按钮:

const SlickArrow = ({onClick}: props) => (
  <button onClick={onClick}><Icon.Arrow /></button>
);
于 2022-02-09T13:00:24.383 回答
0

如果有人试图达到相同的结果,那么可以使用一些 css:

.slick-next {
  right: 25px; 
}
于 2018-02-28T14:56:23.180 回答
0

您可以构建自己的箭头,并在他们的文档中提到,就像上面提到的每个人一样。

但是,如果您想更改图像(大多数情况下都是这样),那么我所做的只是为滑块添加 2 行代码.slick-next:before & .slick-prev:before并将内容替换为图像。有了这个,您不需要处理禁用最后一项上的箭头(这是默认设置)。使用下面的代码只是改变你的箭头,其余的行为保持不变。

请参阅下面的代码

.some_class .slick-slider {
    button.slick-next:before {
        content: url("your_image_here.svg");
    }
    button.slick-prev:before {
        content: url("your_image_here.svg");
    }
   }
于 2021-12-28T08:45:15.970 回答