gpt4 book ai didi

javascript - 在 react-transition-group 中退出延迟动画

转载 作者:数据小太阳 更新时间:2023-10-29 04:16:34 27 4
gpt4 key购买 nike

我在 ReactJSGatsbyJS (V2) 项目中使用 react-transition-group

我的页面转换使用动画,但是当 Link 退出时,exiting 动画被缩短,因为下一页已准备好进入。

我已经尝试延迟 Link 操作,但是当页面更改被延迟时,exit 动画不会被触发,直到 delay 结束并且 Link 已被操作。

如何延迟页面更改,同时启动exiting动画onClick?或者,是否有更好的方法或可用的 props

这是我的代码

Layout.js

class Layout extends React.Component {
...
return (
<Transition>{children}</Transition>
);
}

Transition.js

class Transition extends React.Component {
constructor(props) {
super(props);
this.state = { exiting: false };
this.listenerHandler = this.listenerHandler.bind(this);
}

listenerHandler() {
this.setState({ exiting: true });
}

componentDidMount() {
window.addEventListener(historyExitingEventType, this.listenerHandler);
}

componentWillUnmount() {
window.removeEventListener(historyExitingEventType, this.listenerHandler);
}

static getDerivedStateFromProps({ exiting }) {
if (exiting) {
return { exiting: false };
}
return null;
}

render() {
const transitionProps = {
timeout: {
enter: 0,
exit: timeout
},
appear: true,
in: !this.state.exiting
};

return (
<ReactTransition {...transitionProps}>
{status => (
<div
style={{
...getTransitionStyle({ status, timeout })
}}
>
{this.props.children}
</div>
)}
</ReactTransition>
);
}
}

export default Transition;

gatsby-config.js

import createHistory from 'history/createBrowserHistory';

const timeout = 1500;
const historyExitingEventType = `history::exiting`;

const getUserConfirmation = (pathname, callback) => {
const event = new CustomEvent(historyExitingEventType, {
detail: { pathname }
});
window.dispatchEvent(event);
setTimeout(() => {
callback(true);
}, timeout);
};

let history;
if (typeof document !== 'undefined') {
history = createHistory({ getUserConfirmation });
history.block(location => location.pathname);
}

export const replaceHistory = () => history;

export { historyExitingEventType, timeout };

getTransitionStyle.js

const getTransitionStyles = timeout => {

return {
entering: {
transform: `scale(1.05) translateZ(0)`,
opacity: 0
},
entered: {
transition: `transform 750ms ease, opacity ${timeout}ms ease`,
transitionDelay: `750ms`,
transform: `scale(1) translateZ(0)`,
opacity: 1
},
exiting: {
transition: `transform 750ms ease, opacity ${timeout}ms ease`,
transform: `scale(0.98) translateZ(0)`,
opacity: 0
}
};
};

const getTransitionStyle = ({ timeout, status }) =>
getTransitionStyles(timeout)[status];

export default getTransitionStyle;

最佳答案

Gatsby v2 使用 Reach Router 而不是 React Router,因此使用 getUserConfirmationreplaceHistory 将不再有效。在 Gatsby v2 RC 中,您可以使用 react-pose 更直接地创建页面转换:

gatsby-browser.jsgatsby-ssr.js:

import React from "react"
import Transition from "./src/components/transition"
export const wrapPageElement = ({ element, props }) => {
return <Transition {...props}>{element}</Transition>
}

transition.js 组件:

import React from "react"
import posed, { PoseGroup } from "react-pose"

const timeout = 250

class Transition extends React.PureComponent {
render() {
const { children, location } = this.props

const RoutesContainer = posed.div({
enter: { delay: timeout, delayChildren: timeout },
})

// To enable page transitions on mount / initial load,
// use the prop `animateOnMount={true}` on `PoseGroup`.
return (
<PoseGroup>
<RoutesContainer key={location.pathname}>{children}</RoutesContainer>
</PoseGroup>
)
}
}

export default Transition

在您的页面内:

// Use `posed.div` elements anywhere on the pages.

const Transition = posed.div({
enter: {
opacity: 1,
},
exit: {
opacity: 0,
},
})

// ...

<Transition>Hello World!</Transition>

检查 official example用于工作演示。

关于javascript - 在 react-transition-group 中退出延迟动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51780212/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com