99roomz-react-slick 中文文档教程

发布于 8年前 浏览 424 项目主页 更新于 3年前

react-slick

Join the chat at https://gitter.im/akiran/react-slick

使用 React 构建的 Carousel 组件。 它是 slick carousel 的反应端口

Installation

npm install react-slick

还可以为 css 和字体安装 slick-carousel

npm install slick-carousel
@import "~slick-carousel/slick/slick.css";
@import "~slick-carousel/slick/slick-theme.css";

或在您的 html 中添加 cdn 链接

<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick.min.css" />
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick-theme.min.css" />

Demos

PlayGround

使用 jsfiddle 模板 尝试使用不同的设置做出反应。

Filing issues

请使用 jsfiddle 模板 复制您的问题并将其与问题一起发布,以便于我进行调试。

Starter Kit

查看 yeoman generator 以快速 开始使用 react-slick。

Example

var React = require('react');
var Slider = require('react-slick');

class SimpleSlider extends React.Component {
  render: function () {
    var settings = {
      dots: true,
      infinite: true,
      speed: 500,
      slidesToShow: 1,
      slidesToScroll: 1
    };
    return (
      <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>
    );
  }
}
PropertyTypeDescriptionWorking
accessibilityboolEnables tabbing and arrow key navigationYes
classNameStringAdditional class name for the inner slider divYes
adaptiveHeightboolAdjust the slide's height automaticallyYes
arrowsboolShould we show Left and right nav arrowsYes
nextArrowReact ElementUse this element for the next arrow buttonYes
prevArrowReact ElementUse this element for the prev arrow buttonYes
autoplayboolShould the scroller auto scroll?Yes
autoplaySpeedintdelay between each auto scoll. in msYes
centerModeboolShould we centre to a single item?Yes
centerPadding
cssEase
customPagingfuncCustom paging templates. ExampleYes
dotsboolShould we show the dots at the bottom of the galleryYes
dotsClassstringClass applied to the dots if they are enabledYes
draggableboolIs the gallery scrollable via dragging on desktop?Yes
easingstring
fadeboolSlides use fade for transitionYes
focusOnSelectboolGo to slide on clickYes
infiniteboolshould the gallery wrap around it's contentsYes
initialSlideintwhich item should be the first to be displayedYes
lazyLoadboolLoads images or renders components on demandsYes
pauseOnHoverboolprevents autoplay while hoveringYes
responsivearrayArray of objects in the form of { breakpoint: int, settings: { ... } } The breakpoint int is the maxWidth so the settings will be applied when resolution is below this value. Breakpoints in the array should be ordered from smalles to greatest. Use 'unslick' in place of the settings object to disable rendering the carousel at that breakpoint. Example: [ { breakpoint: 768, settings: { slidesToShow: 3 } }, { breakpoint: 1024, settings: { slidesToShow: 5 } }, { breakpoint: 100000, settings: 'unslick' } ]Yes
rtlboolReverses the slide orderYes
slidestring
slidesToShowintNumber of slides to be visible at a timeYes
slidesToScrollintNumber of slides to scroll for each navigation item
speedint
swipebool
swipeToSlideboolAllow users to drag or swipe directly to a slide irrespective of slidesToScrollYes
touchMovebool
touchThresholdint
variableWidthbool
useCSSboolEnable/Disable CSS TransitionsYes
verticalboolVertical slide modeYes
afterChangefunctioncallback function called after the current index changesYes
beforeChangefunctioncallback function called before the current index changesYes
slickGoTointgo to the specified slide number

Methods

  • slickNext() - function called to change current slide on next slide (Example)
  • slickPrev() - function called to change current slide on previous slide (Example)
  • slickGoTo(slideNumber) - function called to change current slide to given slide number (Example)

Custom next/prev arrows

要自定义下一个/上一个箭头元素,只需创建新的 React 组件并设置它们 作为 nextArrow 和 prevArrow 的值。

class LeftNavButton extends React.Component {
  render() {
    return <button {...this.props}>Next</button>
  }
}

重要提示:确保将组件的道具传递给可点击元素 就像上面的例子。 如果不这样做,您的自定义组件将不会触发点击处理程序。

如果您只想设置点击处理程序,也可以设置 onClick={this.props.onClick}

Flexbox support

如果您在滑块的容器 div 上有 flex 属性,请在 css 下面添加

* {
  min-height: 0;
  min-width: 0;
}

Test Setup

如果您尝试在使用 react-slick 的项目中使用 jest 运行测试,您可能会遇到此错误

matchMedia not present, legacy browsers require a polyfill

要解决此问题,请在 test-setup.js 中添加以下代码段

window.matchMedia = window.matchMedia || function() {
    return {
        matches : false,
        addListener : function() {},
        removeListener: function() {}
    };
};

并在 package.json 中添加以下笑话配置

"jest": {
    "setupFiles": ["test-setup.js"]
}

Development

想要在本地运行演示

git clone https://github.com/akiran/react-slick
npm install
npm start
open http://localhost:8080

Polyfills for old IE support

matchMedia 支持来自 media-match

react-slick

Join the chat at https://gitter.im/akiran/react-slick

Carousel component built with React. It is a react port of slick carousel

Installation

npm install react-slick

Also install slick-carousel for css and font

npm install slick-carousel
@import "~slick-carousel/slick/slick.css";
@import "~slick-carousel/slick/slick-theme.css";

or add cdn link in your html

<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick.min.css" />
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick-theme.min.css" />

Demos

PlayGround

Use jsfiddle template to try react-slick with different settings.

Filing issues

Please replicate your issue with jsfiddle template and post it along with issue to make it easy for me to debug.

Starter Kit

Checkout yeoman generator to quickly get started with react-slick.

Example

var React = require('react');
var Slider = require('react-slick');

class SimpleSlider extends React.Component {
  render: function () {
    var settings = {
      dots: true,
      infinite: true,
      speed: 500,
      slidesToShow: 1,
      slidesToScroll: 1
    };
    return (
      <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>
    );
  }
}
PropertyTypeDescriptionWorking
accessibilityboolEnables tabbing and arrow key navigationYes
classNameStringAdditional class name for the inner slider divYes
adaptiveHeightboolAdjust the slide's height automaticallyYes
arrowsboolShould we show Left and right nav arrowsYes
nextArrowReact ElementUse this element for the next arrow buttonYes
prevArrowReact ElementUse this element for the prev arrow buttonYes
autoplayboolShould the scroller auto scroll?Yes
autoplaySpeedintdelay between each auto scoll. in msYes
centerModeboolShould we centre to a single item?Yes
centerPadding
cssEase
customPagingfuncCustom paging templates. ExampleYes
dotsboolShould we show the dots at the bottom of the galleryYes
dotsClassstringClass applied to the dots if they are enabledYes
draggableboolIs the gallery scrollable via dragging on desktop?Yes
easingstring
fadeboolSlides use fade for transitionYes
focusOnSelectboolGo to slide on clickYes
infiniteboolshould the gallery wrap around it's contentsYes
initialSlideintwhich item should be the first to be displayedYes
lazyLoadboolLoads images or renders components on demandsYes
pauseOnHoverboolprevents autoplay while hoveringYes
responsivearrayArray of objects in the form of { breakpoint: int, settings: { ... } } The breakpoint int is the maxWidth so the settings will be applied when resolution is below this value. Breakpoints in the array should be ordered from smalles to greatest. Use 'unslick' in place of the settings object to disable rendering the carousel at that breakpoint. Example: [ { breakpoint: 768, settings: { slidesToShow: 3 } }, { breakpoint: 1024, settings: { slidesToShow: 5 } }, { breakpoint: 100000, settings: 'unslick' } ]Yes
rtlboolReverses the slide orderYes
slidestring
slidesToShowintNumber of slides to be visible at a timeYes
slidesToScrollintNumber of slides to scroll for each navigation item
speedint
swipebool
swipeToSlideboolAllow users to drag or swipe directly to a slide irrespective of slidesToScrollYes
touchMovebool
touchThresholdint
variableWidthbool
useCSSboolEnable/Disable CSS TransitionsYes
verticalboolVertical slide modeYes
afterChangefunctioncallback function called after the current index changesYes
beforeChangefunctioncallback function called before the current index changesYes
slickGoTointgo to the specified slide number

Methods

  • slickNext() - function called to change current slide on next slide (Example)
  • slickPrev() - function called to change current slide on previous slide (Example)
  • slickGoTo(slideNumber) - function called to change current slide to given slide number (Example)

Custom next/prev arrows

To customize the next/prev arrow elements, simply create new React components and set them as the values of nextArrow and prevArrow.

class LeftNavButton extends React.Component {
  render() {
    return <button {...this.props}>Next</button>
  }
}

Important: be sure that you pass your component's props to your clickable element like the example above. If you don't, your custom component won't trigger the click handler.

You can also set onClick={this.props.onClick} if you only want to set the click handler.

Flexbox support

If you have flex property on container div of slider, add below css

* {
  min-height: 0;
  min-width: 0;
}

Test Setup

If you try to run tests with jest in a project that uses react-slick, you my run into this error

matchMedia not present, legacy browsers require a polyfill

To fix this issue add below snippet in test-setup.js

window.matchMedia = window.matchMedia || function() {
    return {
        matches : false,
        addListener : function() {},
        removeListener: function() {}
    };
};

and add below jest config in package.json

"jest": {
    "setupFiles": ["test-setup.js"]
}

Development

Want to run demos locally

git clone https://github.com/akiran/react-slick
npm install
npm start
open http://localhost:8080

Polyfills for old IE support

matchMedia support from media-match

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文