在React中水平滑动div?

时间:2018-09-02 07:25:14

标签: javascript reactjs material-ui slide

这里是一个Slider组件,它以一定的间隔更改div的某些属性,但是您希望更改立即生效。所以我想要水平滑动上一个div和新div(具有当前属性的div)水平滑动到左侧?

Slider.js

class Slider extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            currentIndex: 0,
        }
        this.tick = this.tick.bind(this);
        this.tickBack = this.tickBack.bind(this);
    }

    componentDidMount(){
        this.timerId = setInterval(() => this.tick(), 4000);
    }

    componentWillUnmount(){
        clearInterval(this.timerId);
    }

    tick(){
        const { currentIndex } = this.state;
        const length = this.props.imgDesc.imageDescription.length;

        this.setState((prevState) => {
            return { currentIndex: (prevState.currentIndex > 0) ? 0 : ++(prevState.currentIndex) }
        });
    }

    tickBack(){
        const { currentIndex } = this.state;
        const length = this.props.imgDesc.imageDescription.length;

        this.setState((prevState) => {
            return { currentIndex: (prevState.currentIndex == 0) ? (length-1) : --(prevState.currentIndex) }
        });
    }

    render(){
        const { classes } = this.props;
        const { imageDescription } = this.props.imgDesc;
        const { currentIndex } = this.state;
        const currentImage = imageDescription[currentIndex];

        console.log(currentImage);

        var fullPath = `/images/Large/${currentImage.imagePath}.jpg`;

        return (
            <div style={{ backgroundImage: "url(" + fullPath + ")" }}  className={classes.homeDiv}>
                <Slide direction="right" in={true} timeout={1000}>
                    <div className={classes.imageDescription}>
                        <Typography variant="display3" align="right" component="h1" style={{ paddingBottom: '5px', color: 'rgb(22, 35, 22)', lineHeight: '0.9em' }}  >
                            {currentImage.head}
                        </Typography>
                        <Typography variant="subheading" align="right" style={{ paddingBottom: '10px' }} color="inherit" gutterBottom  >
                            {currentImage.subhead}
                        </Typography>
                        <Slide direction="left" in={true} timeout={700}>
                            <div className={classes.imageDescButton}>
                                <Button variant="outlined" >{currentImage.button.text}</Button>
                            </div>
                        </Slide>
                    </div>
                </Slide>

            </div>
        )
    }
}

0 个答案:

没有答案