antd 轮播图样式_React 实现简易轮播图

本文介绍如何使用ReactJS创建一个基本的轮播图组件。通过调整图片组的margin-left实现平滑过渡,利用绝对定位和overflow隐藏超出部分,同时提供左右按钮控制轮播。并讨论了如何实现平滑无限循环的轮播效果。
摘要由CSDN通过智能技术生成

使用 ReactJS 实现一个简易的轮播图 (carousel) 组件。

Task 1:在相框中展示图片,左右按钮切换当前图片

实现思路;把图片横向排列成组(image row),放置在相框(frame)中,隐藏超出相框的部分。利用图片组左侧和相框左侧的距离(margin-left)改变当前展示在相框中的内容,点击左右按钮可以改变这个距离。

// How to make use of this component

{images.map(image => )}

// Carousel component

import React, { Component } from 'react';

export default class Carousel extends Component {

constructor(props) {

super(props);

this.state = { currentIndex: 0 };

this.renderChildren = this.renderChildren.bind(this);

this.setIndex = this.setIndex.bind(this);

}

renderChildren() {

const { children, width, height } = this.props;

const childStyle = {

width: width,

height: height

};

return React.Children.map(children, child => {

const childClone = React.cloneElement(child, { style: childStyle });

return (

style={ {

display: 'inline-block'

}}

>

{childClone}

);

});

}

setIndex(index) {

const len = this.props.children.length;

const nextIndex = (index + len) % len;

this.setState({ currentIndex: nextIndex });

}

render() {

const { width, height } = this.props;

const { currentIndex } = this.state;

const offset = -currentIndex * width;

const frameStyle = {<

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值