react实现网格切片随机排列动画过渡效果代码
代码语言:html
所属分类:动画
代码描述:react实现网格切片随机排列动画过渡效果代码,将一个4*4的网格进行编号,点击按钮进行随机更换位置。
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> @layer flippable { .flippable_root[data-flip] { transition: transform 0.4s cubic-bezier(0.2, 0, 0.5, 1); transform: none; transform-origin: top left; } .flippable_root[data-flip="invert"] { transition: none !important; transform: var(--flip-translate) var(--flip-scale); --flip-translate: translate(calc(-1px * var(--dx)), calc(-1px * var(--dy))); --flip-scale: scale(calc(1 / var(--dw)), calc(1 / var(--dh))); --flip-radius: calc((var(--dw)) * var(--radius)) / calc((var(--dh)) * var(--radius)); } } /* ---------------------------------- */ :root { --color-1: #c9dabf; --color-2: #9ca986; --color-3: #808d7c; --color-4: #5f6f65; background: var(--color-1); color: var(--color-4); font-family: monospace; margin: 0; padding: 0; } body { margin: 0; padding: 0; } .grid-container { display: grid; place-items: center; place-content: center; height: 100dvh; } .shuffle-button { background: var(--color-2); padding: 1em 2em; font-family: inherit; border: thick double var(--color-4); color: var(--color-4); cursor: pointer; } .grid { margin: 1em; display: grid; grid-template-rows: repeat(var(--rows), 1fr); grid-template-columns: repeat(var(--cols), 1fr); border: thick double var(--color-2); } .grid-col { display: grid; aspect-ratio: 1 / 1; > * { grid-area: 1 / -1; } .grid-col-id { display: grid; aspect-ratio: 1 / 1; place-items: center; border: thick double var(--color-2); color: var(--color-3); font-size: 3vmin; padding: 2em; background: var(--color-1); transition-delay: calc(var(--i) * 50ms); cursor: pointer; } .grid-selected { transition-delay: 0s; transition-duration: 0.4s; transition-timing-function: cubic-bezier(0.5, 0, 0.2, 1); background: var(--color-2); border-color: var(--color-4); color: var(--color-4); position: relative; z-index: 2; } } </style> </head> <body translate="no"> <div id="app"></div> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/babel.7.18.13.js"></script> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/react.production.18.2.0.js"></script> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/react-dom.production.18.2.0.js"></script> <script type="text/babel"> const { useEffect, useRef, useState} = window.React; const { render,flushSync } = window.ReactDOM; console.clear(); const styles = { root: "flippable_root" }; function Flippable({ className, component: Component = "div", flipId, ...props }) { const ref = useFlippableRef(flipId); const classes = [styles.root, className].filter(Boolean).join(" "); return <Component ref={ref} {...props} className={classes} data-flip />; } const FLIP_TRANSITION_EVENT = "flip-transition"; const FLIP_TRANSITION_END_EVENT = "flip-transition-end"; /** For bigger DOM changes, like a full list transition, call `startFlipTransition` first to ensure the positions all get properly indexed. */ function startFlipTransition(callback, flipId) { document.dispatchEvent( new CustomEvent(FLIP_TRANSITION_EVENT, { detail: flipId }) ); requestAnimationFrame(() => { requestAnimationFrame(async () => { await callback?.(); requestAnimationFrame(() => { document.dispatchEvent( new CustomEvent(FLIP_TRANSITION_END_EVENT, { detail: flipId }) ); }); }); }); } const rects = {}; function useFlippableRef(flipIdProp) { const ref = React.useRef(); const flipIdRef = React.useRef(flipIdProp); flipIdRef.current = flipIdProp; const updateOnExit = React.useRef(true); React.useLayoutEffect(() => { co.........完整代码请登录后点击上方下载按钮下载查看
网友评论0