基于react和swiper4实现无缝轮播组件 中间显示主图 左右显示部分图

swiper4中文文档

子组件 commonSwiper.js

import React, {Component} from 'react'
import ImportedImage from '../../../common/components/importedImage'
import '../../../common/sass/swiper'
import Swiper from '../../../common/js/swiper'
/*
* 调用示例: 
*   <SwiperHtml
*       list={list} 必传-轮播列表 
*       courseInfo={courseInfo} 非必传-课程介绍 
*       isShowPagination={false} 非必传-是否展示分页器(默认不展示)
*       isLoop={true} 非必传-是否循环轮播展示(默认循环展示)
*       clickEvent={t.clickSwiper.bind(t)}> 非必传-事件回调 
*       isAuto={isAuto} 非必传-是否自动播放 默认不自动播放
*   </SwiperHtml>
*
*/


/*
* 参数示例: 
* list: [{
*    id: 1,
*    name: 'aaaaa',
*    title: '标题标题标题标题标题',
*    pic: pic1, // swiper 图片的src
*    url: url, // 点击swiper跳转的url,
*    text: [
*        '介绍1介绍1介绍1介绍1',
*        '介绍2介绍2介绍2介绍2',
*        '介绍3介绍3介绍3介绍3',
*        '介绍4介绍4介绍4介绍4',
*    ]
* }]
*  
*  
*  isAuto示例
*  autoplay:true,//等同于以下设置
*  autoplay: {
*    delay: 3000,
*    stopOnLastSlide: false,
*    disableOnInteraction: true,// 用户操作swiper之后,是否禁止autoplay。默认为true:停止。
*  },
*/

class App extends Component {
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        const t = this;
        let list = t.props.list;
        let isLoop = t.props.isLoop || true;
        let isAuto = t.props.isAuto || false;

        var mySwiper = new Swiper ('.swiper-container', {
            direction: 'horizontal',
            loop: isLoop,
            slidesPerView: "auto",
            centeredSlides:true,
            spaceBetween: 20,
            // 如果需要分页器
            pagination: {
                el: '.swiper-pagination'
            },
            on: {
                click: (event) => {
                    if(t.props.clickEvent) {
                        t.props.clickEvent(event, mySwiper);
                    }
                }
            },     

        })    

    }    

    render() {
        const t = this;
        const {list, isShowPagination, courseInfo} = t.props;
        return (
            <div>
                <div className="swiper-container">
                    <div className="swiper-wrapper">
                        {
                            list.map((item, index) =>
                                <div className="swiper-slide">
                                    <ImportedImage img={item.pic}></ImportedImage>
                                    {courseInfo && courseInfo[index]}
                                </div>
                            )
                        }
                    </div>
                    <div className="swiper-pagination" style={isShowPagination ? {'display':'block'} : {'display': 'none'}}></div>
                </div>
            </div>
        )
    }
}
export default App;

父组件

/*事件回调
* 这里做了2件事 1.点击slide上的+号 有其他处理
* 2. 点击整个slide会进行相应跳转
*/ 
clickSwiper(event, mySwiper) {
    const t = this;
    const {list} = t.state;
    const index = mySwiper.activeIndex % list.length;
    const type = event.target.getAttribute("data-type");
    const id = event.target.getAttribute("data-id");
    if (type == 'add') {
        t.onBtnAdd(id);
    }
    else {
        window.location.href = list[index].url;
    }
}

// html
<div id="slide">
	<SwiperHtml
		list={list}
		courseInfo={courseInfo}
		isShowPagination={false}
		clickEvent={t.clickSwiper.bind(t)}>
	</SwiperHtml>
</div>   

css

#slide{
    width: 100%;
    .swiper-pagination-bullet-active{
        background: #ff7327;
    }
    .swiper-container{
        height:u(600px);
    }
    .swiper-slide{
        width: 76%;
        height:u(590px);
        float: left;
        position: relative;
        border-radius: u(30px);
        overflow: hidden;
        box-sizing: border-box;
        background: #fff;
        box-shadow: #f2f2f2 0px u(10px) u(10px);
        .icon{
            width: 0.8rem;
            height: 1rem;
            background: url('../assets/gather/slide/icons.png') no-repeat;
            background-size: 7.08rem 2.3rem;
            background-position: -6.24rem 0;
            position: absolute;
            bottom: 0.3rem;
            right: 0.2rem;
            z-index: 99;
        }
        .img{
            width: 200%;
            height: u(260px);
        }
        .title{
            width: 86%;
            margin: 0 auto;
            font-size: u(30px);
            color: #333;
            text-align: left;
            margin-top: u(30px);
        }
        .content{
            width: 80%;
            margin: 0 auto;
            box-sizing: border-box;
            font-size: u(24px);
            color: #666;
            margin-top: u(24px);
            li{
                padding-top: u(16px);
            }
        }
    }
    .swiper-pagination-fraction, .swiper-pagination-custom, .swiper-container-horizontal > .swiper-pagination-bullets{
        height: 0.4rem;
        line-height: 0;
    }

}

效果图
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 JavaScript 实现的简单的轮播案例: ```html <!DOCTYPE html> <html> <head> <title>JavaScript 案例:轮播</title> <style> #slideshow { position: relative; width: 600px; height: 400px; margin: 0 auto; overflow: hidden; } #slideshow img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: 0; transition: opacity 1s ease-in-out; } #slideshow img.active { opacity: 1; } #slideshow button { position: absolute; top: 50%; transform: translateY(-50%); background-color: #fff; border: none; padding: 10px; cursor: pointer; font-size: 24px; } #slideshow button.prev { left: 0; } #slideshow button.next { right: 0; } </style> <script> var images = ["image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg"]; var index = 0; var slideshow = document.getElementById("slideshow"); var prevButton = document.createElement("button"); var nextButton = document.createElement("button"); function init() { prevButton.innerHTML = "❮"; prevButton.classList.add("prev"); prevButton.addEventListener("click", prevSlide); nextButton.innerHTML = "❯"; nextButton.classList.add("next"); nextButton.addEventListener("click", nextSlide); slideshow.appendChild(prevButton); slideshow.appendChild(nextButton); showSlide(); setInterval(nextSlide, 5000); } function showSlide() { var img = document.createElement("img"); img.src = images[index]; img.classList.add("active"); slideshow.appendChild(img); } function hideSlide() { var activeImg = slideshow.querySelector(".active"); activeImg.classList.remove("active"); slideshow.removeChild(activeImg); } function prevSlide() { hideSlide(); index--; if (index < 0) { index = images.length - 1; } showSlide(); } function nextSlide() { hideSlide(); index++; if (index >= images.length) { index = 0; } showSlide(); } window.addEventListener("load", init); </script> </head> <body> <h1>轮播</h1> <div id="slideshow"></div> </body> </html> ``` 在这个案例,我们使用了 JavaScript 来实现一个基本的轮播。在 HTML ,我们定义了一个包含片的 div 元素,并使用 CSS 来设置其样式。在 JavaScript ,我们定义了一个 images 数组来存储片的文件名,一个 index 变量来记录当前显示片的索引,以及 prevButton 和 nextButton 两个按钮元素来控制切换片。在 init 函数,我们初始化轮播,并设置了自动切换的定时器。在 showSlide 函数,我们创建一个 img 元素,并将其添加到轮播。在 hideSlide 函数,我们移除当前显示片。在 prevSlide 函数和 nextSlide 函数,我们分别实现了向前和向后切换片的逻辑。最后,我们在 window 的 load 事件调用 init 函数来初始化轮播

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值