江湖规矩~无图无真相,先上图!
这是一个展览列表的滑动轮播组件,点击左右按钮就可以进行左右滑动,每次可以滑动指定的数量,以及可以指定展示多少行的内容。
本人前端小菜鸟,大佬们轻喷呜呜呜
下面来分享一下代码
因为本人采用的是React + Typescript来做项目的,大家如果有看不懂也没关系的!换成普通的html跟js也差不多的。
使用
<Slider cityList={cityList} row={2} step={2} />
组件代码
import React, {
Component, ComponentType } from 'react'
import {
Icon } from 'antd'
import './index.scss'
interface IProps {
cityList: Array<number>,
row: number,
step: number
}
interface IStates {
cityContainerWidth: number,
cityWrapWidth: number,
cityWrapTranslateX: number
}
class Slider extends Component<IProps, IStates> {
constructor(props: IProps) {
super(props)
this.state = {
cityContainerWidth: 0,
cityWrapWidth: 0,
cityWrapTranslateX: 0
}
}
componentDidMount(): void {
const {
cityList, row } = this.props
const cityWrapWidth: number = cityList.length > 12 ? Math.ceil(cityList.length / row) * 220 : 1320
const cityWrapDom: HTMLElement | null = document.getElementById('city__wrap') as HTMLElement
const cityContainerDom: HTMLElement | null = document.getElementById('city__container') as HTMLElement
const cityContainerWidth: number = cityContainerDom.offsetWidth
cityWrapDom && (cityWrapDom.style.width = `