【发布时间】:2020-08-08 03:28:45
【问题描述】:
我正在开发一个组件,它是一种带有箭头的画廊,可以“滑动”它。基本上默认情况下它会显示六次潜水,每一次潜水都是带有品牌名称的图像。我希望它根据屏幕宽度看起来不同。它应该在大型桌面上显示 6 个 div(或图像),在中型桌面上显示 4 个,在小型桌面上显示 2 个。我想要一种不同于媒体查询的方法。我是 react.js 的新手,因此我们将不胜感激任何提示和帮助。
import React from 'react';
import PropTypes from 'prop-types';
import BrandsBox from '../../common/BrandBox/BrandBox';
import styles from './Brands.module.scss';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faChevronLeft, faChevronRight } from '@fortawesome/free-solid-svg-icons';
class Brands extends React.Component {
state = {
activeBrands: 6,
startBrandDisplay: 0,
};
render() {
const { brands } = this.props;
const { activeBrands } = this.state;
const brandsToDisplay = brands.slice(this.state.startBrandDisplayy, activeBrands+this.state.startBrandDisplay);
return (
<div className={'container'}>
<div className={styles.componentContainer}>
<div
onClick={() => {
this.setState({
...this.state,
startBrandDisplay: this.state.startBrandDisplay-1
})
}}
className={styles.swipe}>
<FontAwesomeIcon icon={faChevronLeft} className={styles.icon} />
</div>
{ brandsToDisplay.map(brands => (
<div id ={brands.id} key={brands.id} className={styles.brandContainer}>
<BrandsBox {...brands} />
</div>
))}
<div
onClick={() => {
this.setState({
...this.state,
startBrandDisplay: this.state.startBrandDisplay+1
})
}}
className={styles.swipe}>
<FontAwesomeIcon icon={faChevronRight} className={styles.icon} />
</div>
</div>
</div>
);
}
}
Brands.propTypes = {
brands: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number,
name: PropTypes.string,
image: PropTypes.string,
})
),
};
export default Brands;
【问题讨论】:
标签: javascript reactjs slider gallery