|
需求明确:需要在react 中使用 swiper ,且使用导航,自动播放等功能。
参考文献:https://swiperjs.com/react/ [ 这是swiper 的 方式 ];
前提条件:
1. umi 默认使用的是css modules , 如果要使用 react 的 css 引入方式, 需要在 umirc.ts 文件里 增加配置[ 参照上篇或者百度 哈哈 我当时可是给这个憋了三四天才明白 不得不说 umi 的官方文档 莫名其妙 呵呵]。
2. swiper 的官方文档引入的是 .sass 文件 , umi 不具备编辑此文件的 能力, 全部换成 .less 结尾即可
3. 建议使用 cnpm 的 淘宝镜像源 进行安装 否则会凉
开始:
1. 在react 中安装swiper
- cnpm install swiper --save
复制代码
2. 在 react 的 项目中 引入文件 [这是最基础的swiper , 没有任何组件]
[ 这里得说明,该引入 是最初始化的 不包含 组件 ]
- import { Swiper, SwiperSlide } from 'swiper/react';
- import "../../assets/style/laomo.css";
- import 'swiper/swiper.less';
- <Swiper
- spaceBetween={10}
- slidesPerView={4}
- zoom={true}
- loop={true} // 这个不用组件可以直接用
-
- pagination={{ clickable: true }}
- onSlideChange={() => console.log('slide change')}
- onSwiper={(swiper) => console.log(swiper)}
- className="swiper-container">
- {
- this.state.hezuoArr.map((item, index)=>{
- return (
- <SwiperSlide key={index} className="tbox">
- <div className="tbox-img">
- <img src={item.img}/>
- </div>
- <div className="tbox-title">
- {item.title}
- </div>
- </SwiperSlide>
- );
- })
- }
-
-
- </Swiper>
复制代码
3. 你得用组件吧,然后 就引入
- import SwiperCore, { Navigation, Pagination, Autoplay} from 'swiper';//这里 只做个样子
- import 'swiper/components/navigation/navigation.less';//这里 只做个样子
- import 'swiper/components/pagination/pagination.less';//这里 只做个样子
- import 'swiper/components/scrollbar/scrollbar.less'; //这里 只做个样子
- SwiperCore.use([Navigation ,Autoplay]);//这里 只做个样子
复制代码 (每种 组件的使用方法不同, 就比方说 有的有less 有的没有, 有的 直接就能用 ,有的需要加在属性里:例如 autopaly 就得引入且加在属性里)
最终版
- // 这是合作伙伴栏目
- import React from 'react';
- import createReactClass from 'create-react-class';
- import SwiperCore, { Autoplay} from 'swiper';
- import { Swiper, SwiperSlide } from 'swiper/react';
- import "../../assets/style/laomo.css";
- import 'swiper/swiper.less';
- const Cooperation = createReactClass({
- getInitialState:function(){
- return {
- hezuoArr:[
- {'title':'合作伙伴1','img':'http://www.lygky.com/img/logos/logo.png'},
- {'title':'合作伙伴2','img':'http://www.lygky.com/img/logos/logo.png'},
- {'title':'合作伙伴3','img':'http://www.lygky.com/img/logos/logo.png'},
- {'title':'合作伙伴4','img':'http://www.lygky.com/img/logos/logo.png'},
- {'title':'合作伙伴5','img':'http://www.lygky.com/img/logos/logo.png'},
- {'title':'合作伙伴6','img':'http://www.lygky.com/img/logos/logo.png'},
- {'title':'合作伙伴7','img':'http://www.lygky.com/img/logos/logo.png'},
- {'title':'合作伙伴8','img':'http://www.lygky.com/img/logos/logo.png'}
- ]
- }
- },
- render() {
- SwiperCore.use([Autoplay]);
- return (
- <section className="hezuo-box">
- <div className="min-section">
- <div className="min-title"> - 公司案例与合作伙伴 - </div>
- <p className="min-title-p1">为每一位使用者守住您的知识产权与客户档案</p>
- </div>
- <div className="ttt">
- <Swiper
- spaceBetween={10}
- slidesPerView={4}
- zoom={true}
- loop={true}
- autoplay = {true}
- pagination={{ clickable: true }}
- onSlideChange={() => console.log('slide change')}
- onSwiper={(swiper) => console.log(swiper)}
- className="swiper-container">
- {
- this.state.hezuoArr.map((item, index)=>{
- return (
- <SwiperSlide key={index} className="tbox">
- <div className="tbox-img">
- <img src={item.img}/>
- </div>
- <div className="tbox-title">
- {item.title}
- </div>
- </SwiperSlide>
- );
- })
- }
-
-
- </Swiper>
- </div>
-
- </section>
- );
-
- }
- })
- export default Cooperation;
复制代码
|
|