<div id="app">
</div>
body {
width: 100vw;
height: 100vh;
overflow: hidden;
padding: 0;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(180deg, #f08, #d0e);
}
import React,{useState, StrictMode,useEffect} from 'https://esm.sh/react@18.2.0'
import ReactDOM from 'https://esm.sh/react-dom@18.2.0'
import {motion,useAnimationControls,useDragControls} from "https://esm.sh/framer-motion@8.3.3";
const btnStyle = {
color:'white',
fontFamily:'system-ui',
backgroundColor:'black',
padding:10,
paddingLeft:32,
paddingRight:32,
letterSpacing:1,
borderRadius:20,
cursor:'pointer',
whiteSpace:'nowrap'
}
const App = function() {
const controls = useDragControls()
function startDrag(event) {
controls.start(event)
}
return (
<div style={{
display:'flex',
flexDirection:'column',
justifyContent:'center',
alignItems:'center',
gap:52,
}}>
<motion.div
style={{
width:100,
height:100,
backgroundColor:'white',
borderRadius:'20px'
}}
// 这里
dragListener={false}
dragControls={controls}
drag='x'
>
</motion.div>
{/*
// 下面这个是小方块
*/}
<motion.div
style={{
width:20,
height:20,
backgroundColor:'white',
borderRadius:'6px'
}}
// 这里
onPointerDown={startDrag}
>
</motion.div>
<div
style={{
fontFamily:'system-ui',
color:'white',
opacity:0.8,
letterSpacing:2 ,
}}
>
<span>尝试拖动下面小方块</span>
</div>
</div>
)
}
const container = document.querySelector("#app")
console.log(container)
const root = ReactDOM.createRoot(container)
root.render(
<StrictMode>
<App />
</StrictMode>
)
View Compiled
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.