React-Router 动画 Animation

React-Router动画实际上和React动画没什么区别,都是使用 ‘react-addons-css-transition-group‘ 这个组件;但是,和普通的 React-Router 的 App 的写法稍有t不同;

通常我们这样定义一个使用了 React-Router 的组件

export default React.createClass({
  render() {
    return <div>
      {this.props.children}
    </div>
  }
})

但是当需要使用 ReactCSSTransitonGroup 的时候,需要写成如下:

const App = ({children, location}) => (
    <div>
        <CSSTransitionGroup
            component="div"
            transitionName="page"
            transitionAppear={true}
            transitionAppearTimeout={1500}
            transitionEnterTimeout={1500}
            transitionLeaveTimeout={1500}>
            {React.cloneElement(children, {
                key: location.pathname
            })}
        </CSSTransitionGroup>
    </div>
)

上面是一个最基础的使用 ReactCSSTransitonGroup 的 App。

下面是几个示例:

Demo1: 参考自官方示例

var React = require(‘react‘);
var ReactDOM = require(‘react-dom‘);
var CSSTransitionGroup = require(‘react-addons-css-transition-group‘);
var ReactRouter = require(‘react-router‘);
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var hashHistory = ReactRouter.hashHistory;
var IndexRoute = ReactRouter.IndexRoute;
var Link = ReactRouter.Link;

const App = ({children, location}) => (
    <div>
        <ul>
            <li><Link to="/page1">Page 1</Link></li>
            <li><Link to="/page2">Page 2</Link></li>
        </ul>
        <CSSTransitionGroup
            component="div"
            transitionName="page"
            transitionAppear={true}
            transitionAppearTimeout={1500}
            transitionEnterTimeout={1500}
            transitionLeaveTimeout={1500}>
            {React.cloneElement(children, {
                key: location.pathname
            })}
        </CSSTransitionGroup>
    </div>
)

const Index = () => (
    <div className="Image">
        <h1>Index</h1>
        <p>Animations with React Router are not different than any other animation.</p>
    </div>
)

const Page1 = () => (
    <div className="Image">
        <h1>Page 1</h1>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Do eiusmod tempor incididunt ut labore
            et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
            aliquip exea commodo consequat.
            Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
            Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
            laborum.</p>
    </div>
)

const Page2 = () => (
    <div className="Image">
        <h1>Page 2</h1>
        <p>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
            Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
            Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
            Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
            laborum.</p>
    </div>
)

ReactDOM.render((
    <Router history={hashHistory}>
        <Route path="/" component={App}>
            <IndexRoute component={Index}/>
            <Route path="page1" component={Page1}/>
            <Route path="page2" component={Page2}/>
        </Route>
    </Router>
), document.getElementById(‘container‘));

Demo2

var React = require(‘react‘);
var ReactDOM = require(‘react-dom‘);
var CSSTransitionGroup = require(‘react-addons-css-transition-group‘);
var ReactRouter = require(‘react-router‘);
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var hashHistory = ReactRouter.hashHistory;
var IndexRoute = ReactRouter.IndexRoute;
var Link = ReactRouter.Link;

var Index = ()=>(<div>Index</div>)

var App3 = ()=> (<div className="page1">ABCDEFGHIJKLMN</div>)

var App4 = ()=> (<div className="page2">ABCDEFGHIJKLMNFKFKFKFK</div>)

const App = ({children, location}) => (
    <div>
        <CSSTransitionGroup
            component="div"
            transitionName="page"
            transitionAppear={true}
            transitionAppearTimeout={1500}
            transitionEnterTimeout={1500}
            transitionLeaveTimeout={1500}>
            {React.cloneElement(children, {
                key: location.pathname
            })}
        </CSSTransitionGroup>
    </div>
);

ReactDOM.render(
    <Router history={hashHistory}>
        <Route path="/" component={App}>
            <IndexRoute component={Index}/>
            <Route path="page1" component={App3}/>
            <Route path="page2" component={App4}/>
        </Route>
    </Router>,
    document.getElementById(‘container‘));

Demo3:

还可以不使用如上的方式,转而将每个App都使用 ReactCSSTransitionGroup 封装

var React = require(‘react‘);
var ReactDOM = require(‘react-dom‘);
var CSSTransitionGroup = require(‘react-addons-css-transition-group‘);
var ReactRouter = require(‘react-router‘);
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var hashHistory = ReactRouter.hashHistory;
var IndexRoute = ReactRouter.IndexRoute;

var App1 = React.createClass({

    handleClick: function () {
        this.props.router.push(‘/page2‘);
    },

    render: function () {
        return (
            <CSSTransitionGroup
                transitionName="page"
                transitionAppear={true}
                transitionAppearTimeout={1500}
                transitionEnterTimeout={1500}
                transitionLeaveTimeout={1500}>
                <div onClick={this.handleClick} className="page1">ABC</div>
            </CSSTransitionGroup>
        )
    }
});

var App2 = React.createClass({

    handleClick: function () {
        this.props.router.goBack();
    },

    render: function () {
        return (
            <CSSTransitionGroup
                transitionName="page"
                transitionAppear={true}
                transitionAppearTimeout={1500}
                transitionEnterTimeout={1500}
                transitionLeaveTimeout={1500}>
                <div onClick={this.handleClick} className="page2">ABCDEFGHIJKLMN</div>
            </CSSTransitionGroup>
        )
    }
});

var App = ()=>(<div>Welcome App</div>)

ReactDOM.render(
    <Router history={hashHistory}>
        <Route path="/page1" component={App1}></Route>
        <Route path="/page2" component={App2}></Route>
    </Router>, document.getElementById(‘container‘));

page及相关样式

.page-appear {
    opacity: 0.01;
    margin-left: 100%;
    -webkit-transition: all 1.5s;
    transition: all 1.5s;
}

.page-appear.page-appear-active {
    margin-left: 0px;
    opacity: 1;
}

.page-enter {
    opacity: 0.01;
    margin-left: 100%;
    -webkit-transition: all 1.5s;
    transition: all 1.5s;
}

.page-enter.page-enter-active {
    margin-left: 0px;
    opacity: 1;
}

.page-leave {
    opacity: 1;
    margin-left: 0px;
    -webkit-transition: all 1.5s;
    transition: all 1.5s;
}

.page-leave.page-leave-active {
    margin-left: -100%;
    opacity: 0.01;
}

.page1 {
    position: absolute;
    background-color: red;
    height: 100%;
    width: 100%;
    min-height: 400px;
}

.page2 {
    position: absolute;
    background-color: deepskyblue;
    height: 100%;
    width: 100%;
    min-height: 400px;
}

.Image {
    position: absolute;
    height: 400px;
    width: 400px;
}

.link-active {
    color: #bbbbbb;
    text-decoration: none;
}

样式定义参考:[Link]

时间: 2024-10-11 21:43:49

React-Router 动画 Animation的相关文章

React 附件动画API ReactCSSTransitionGroup

React为动画提供了一个附加组件ReactTransitionGroup,这个附加组件是动画的底层API,并且还提供了一个附件组件ReactCSSTransitionGroup,ReactCSSTransitionGroup能够简单的实现基于css的动画和转换 高级API ReactCSSTransitionGroup ReactCSSTransitionGroup是一个基于ReactTransitionGroup的API,当一个React组件插入或者移除DOM时,它是一种简单的去执行css转

最新的chart 聊天功能( webpack2 + react + router + redux + scss + nodejs + express + mysql + es6/7)

请表明转载链接:http://www.cnblogs.com/zhangkunweb/p/6853728.html 我是一个喜欢捣腾的人,没事总喜欢学点新东西,可能现在用不到,但是不保证下一刻用不到. 我一直从事的是依赖angular.js 的web开发,但是我怎么能一直用它呢?看看最近火的一塌糊涂的reactjs ,我的天啊,不学会它,怎么能睡好觉. 今天我分享一个依赖最新版本的webpack + react + router + redux + scss + nodejs + mysql +

React漫漫学习路之 React Router

React Router 是一个基于 React 之上的强大路由库,它可以让你向应用中快速地添加视图和数据流,同时保持页面与 URL 间的同步. 目前react-router最新版本已经到4.0+,因为新的版本是一次非常大的改动,所以这里直接讨论4.0以上版本. 引用 react-router // React Router 核心 react-router-dom // 用于 DOM 绑定的 React Router react-router-native // 用于 React Native

css3 动画(animation)-简单入门

css3之动画(animation) css3中我们可以使用动画,由于取代以前的gif图片,flash动画,以及部分javascript代码(相信有很多同学都用过jquery中的animate方法来做一些动画).具体如何使用呢??? 首先定义一个动画,然后引用动画. 定义一个动画要使用@keyframes,然后跟上你要定义的动画的名字.关键字"from"表示开始, "to"表示结束,等同于0% 和 100%.最好使用百分比来表示变化发生的时间,这样的话还可以定义从开

react router 4.0以上的路由应用

thead>tr>th{padding:8px;line-height:1.4285714;border-top:1px solid #ddd}.table>thead>tr>td,.table>tbody>tr>th,.table>tbody>tr>td,.table>tfoot>tr>th,.table>tfoot>tr>td{padding:8px;line-height:1.4285714;ver

《The CG Tutorial》阅读笔记——动画 Animation

这段时间阅读了英文版的NVidia官方的<The CG Tutorial>,通过它来学习基本的图形学知识和着色器编程. 在此做一个阅读记录. 动画 Animation 一.基于时间的运动 Movement in Time 实现动画渲染,需要应用程序对时间进行监测,并将它作为一个全局变量传递给着色器. 尽量在GPU上使用顶点着色器执行动画计算是一种高效的动画实现方式,它能够释放CPU,让CPU处理更多的复杂计算,比如碰撞检测,人工智能与游戏玩法. 二.一个做脉冲运动的对象 A Pulsating

[React Router v4] Use the React Router v4 Link Component for Navigation Between Routes

If you’ve created several Routes within your application, you will also want to be able to navigate between them. React Router supplies a Link component that you will use to make this happen. Import Link: import { BrowserRouter as Router, Route, Link

[React Router V4] Create Basic Routes with the React Router v4 BrowserRouter

React Router 4 has several routers built in for different purposes. The primary one you will use for building web applications is the BrowserRouter. In this lesson you will import the BrowserRouter and create some basic Route components. After create

动画animation的三个应用(漂浮的白云、旋转的星球、正方体合成)

× 目录 [1]漂浮的白云 [2]旋转的星球 [3]正方体合成 前面的话 前面介绍过动画animation的详细用法,本文主要介绍动画animation的三个效果 漂浮的白云 [效果演示] [简要介绍] 漂浮的白云主要通过远景白云和近景白云来实现立体漂浮效果.远景和近景分别使用两张背景图片,通过改变其背景定位来实现白云移动效果,通过设置不同的动画持续时间来实现交错漂浮的效果 [主要代码] .box{ position: relative; height: 300px; width: 500px;