javascript - 使用React JS制作动画

原文 标签 javascript css animation reactjs

我的页面上有几个排序过滤器,当用户单击其中一个时,我想创建动画。

如果用户想按最低价格排序,我想显示新列表,但带有一些动画。

我尝试用React JS做到这一点。

在componentWillMount上,我设置了所有汽车的状态:

componentWillMount(){
    const cart = this.props.cart;
    const listID = this.props.params.id;
    const allCars = data.getAllCars();
    let offers = this.state.offers;

    cart.map(car => {
        const aCar = allCars.find(c => {
            return c.id === car.carID;
        });
        offers[aCar.chassis] = car.offer;
        this.setState({offers: offers});
    });


    //We set state with all cars in this list
    const cars = carData.getCarsInCurrentList(listID, allCars);
    this.setState({cars: cars});
}


然后,当用户单击排序过滤器之一时,我将在此功能上进行处理:

handleSortingFilters(name, event){

    event.preventDefault();
    const cars = this.state.cars;
    const sortFilterValue = this.state.sortFiltersInit[name];
    let filteredCars = "";

    if(sortFilterValue){
        //descending order
        filteredCars = cars.sort((a, b) => {
            if(name === "initialRegistration"){
                return Date.parse(b[name]) - Date.parse(a[name]);
            }else{
                return parseFloat(b[name]) - parseFloat(a[name]);
            }
        });
    }else{
        //ascending order
        filteredCars = cars.sort((a, b) => {
            if(name === "initialRegistration") {
                return Date.parse(a[name]) - Date.parse(b[name]);
            }else{
                return parseFloat(a[name]) - parseFloat(b[name]);
            }

        });
    }

    let sortFiltersInit = this.state.sortFiltersInit;
    sortFiltersInit[name] = !this.state.sortFiltersInit[name];

    let allFilters = this.state.allFilters;
    allFilters[name] = name;

    this.setState({cars: filteredCars, sortFiltersInit: sortFiltersInit, allFilters: allFilters});
}


因此,我使用过滤后的汽车列表来更新状态。一切正常,但当用户单击排序过滤器之一时,我也想在列表上应用一些CSS动画。

有任何想法或链接到一些好的教程吗?

谢谢。

更新

渲染汽车的列表:

<div className="cars">
            <div>
                <ReactCSSTransitionGroup
                transitionName="example"
                transitionAppear={true}
                transitionAppearTimeout={500}
                transitionEnterTimeout={500}
                transitionLeaveTimeout={500}>

                {cars.map((car, i) => {
                    const initialReg = car.initialRegistration.slice(0,3) + car.initialRegistration.slice(6,10);
                    // str.slice(1, 4) extracts the second character through the fourth character (characters indexed 1, 2, and 3)

                    return (
                        <div key={i} className="carBox noPadding">
                            <div className="carBoxContent">

                                <PhotoAndFavorites car={car} language={language} changeStarIcon={this.changeStarIcon} addToFavorites={addToFavorites} userEmail={currentUserEmail} favorites={favorites}/>

                                <div className="carNameAndDesc">
                                    <div><Link to="" style={{textDecoration: 'none'}}>{car.make}</Link></div>
                                    <div>{car.desc}</div>
                                </div>

                                <div className="carPrice">
                                    <div>{car.price}</div>
                                    <div>{car.btw}</div>
                                </div>

                                <div className="extraFeatures" style={{marginBottom: 5, backgroundColor: '#eee'}}>

                                </div>

                                <div className="mainFeatures">
                                    <div><img src="../images/portal/user/status/fuel-icon.png" style={{height: 12}}/> <span>{car.fuel}</span></div>
                                    <div><img src="../images/portal/user/status/road-icon.png" style={{height: 12}}/> <span>{car.mileage}</span></div>
                                    <div><img src="../images/portal/user/status/calendar-icon.png" style={{height: 12}}/> <span>{initialReg}</span></div>
                                </div>

                                <Buttons car={car}
                                         language={language}
                                         changeButton={this.changeButton}
                                         addToCard={addToCard}
                                         removeFromCard={removeFromCard}
                                         cartContent={cartContent}
                                         userEmail={currentUserEmail}
                                         handleChange={handleOffers}
                                         offers={offers}
                                         opacity={this.props.opacity}
                                         />

                            </div>
                        </div>
                    );
                })}
                </ReactCSSTransitionGroup>
            </div>
            <div className="clearfix"/>
        </div>


我也添加了这个CSS:

 .example-appear {
  opacity: 0.01;
}

.example-appear.example-appear-active {
  opacity: 1;
  transition: opacity .5s ease-in;
}

最佳答案

您可以使用react-addons-css-transition-group


  ReactCSSTransitionGroup基于ReactTransitionGroup,是
  React时执行CSS过渡和动画的简单方法
  组件进入或离开DOM。它的灵感来自出色的
  ng-animate库。


每当您更改路线时,这都会在您的主布局中触发过渡

的CSS

.example-enter {
  opacity: 0.01;
  transition: opacity .5s ease-in;
}

.example-enter.example-enter-active {
  opacity: 1;
}

.example-leave {
  opacity: 1;
  transition: opacity .5s ease-in;
}

.example-leave.example-leave-active {
  opacity: 0;
}


js

import React from 'react'
import ReactCSSTransitionGroup from 'react-addons-css-transition-group'

render() {
  return(
    <ReactCSSTransitionGroup
      component="ul"
      transitionName="example"
      transitionEnterTimeout={500}
      transitionLeaveTimeout={500}>
      {this.state.cars.map(car => <li key={car.id}>{car.name}</li>)}
    </ReactCSSTransitionGroup>
  )
}


如果需要随机播放动画,请尝试react-shuffle-子组件的动画随机播放

import React, {Component} from 'react';
import Shuffle from 'react-shuffle';

class App extends Component {
  render() {
    return (
      <Shuffle>
        {// Method to render children goes here}
      </Shuffle>
    )
  }
}

关于javascript - 使用React JS制作动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39201511/

相关文章:

javascript - 如何将props传递到从组件数组映射的组件?

javascript - 自定义垫选择以允许 Angular 嵌套值

html - 从所有子元素清除CSS

android - 尝试在CSS中绘制图像

javascript - CSS 动画在返回之前下降

ios - CATransition多维数据集具有平移手势

javascript - 即使在页面刷新后,如何保持 Websocket 连接持久?

javascript - jquery上的所有ajax ready事件

html - 将样式应用于选择框,为什么不起作用?

javascript - 使用 jquery 为 SVG 的填充设置动画?