第一种:onDrag的info.point.x

这种不能实时更新

视频:

onDrag.mov (1.95MB)

代码:

  1. import { Override, Data } from "framer"
  2. const data = Data({
  3. handleX: 0,
  4. })
  5. export function Handle(): Override {
  6. return {
  7. drag: "x",
  8. dragConstraints: {
  9. right: 320,
  10. left: -320,
  11. },
  12. dragElastic: 0.1,
  13. onDrag(event, info) {
  14. data.handleX = info.point.x
  15. },
  16. }
  17. }
  18. export function LeftPanel(props): Override {
  19. return {
  20. width: props.width + data.handleX,
  21. }
  22. }
  23. export function RightPanel(props): Override {
  24. return {
  25. width: props.width - data.handleX,
  26. }
  27. }
  28. //当这种Drag或者是Scroll的时候,拖动或者滑动的速度快一点,
  29. //通过onDrag传递的info.point.x的值和被驱动值不能实时同步。
  30. //为了解决这个问题,framer推出了motionValue

第二种:motionValue 和 useTransform

视频

motionValue.mov (1.57MB)

代码

import { Override, motionValue ,useTransform} from "framer"

const handleX = motionValue(0)

handleX.onChange( value => {
    console.log(value)
} )

export function Handle(): Override {
    return {
        drag: "x",
        dragConstraints: {
            right: 320,
            left: -320,
        },
        dragElastic: 0.1,
        x:handleX
    }
}

export function LeftPanel(props): Override {
    const width = useTransform(handleX, (value) => {
        return(
            props.width + value
        )
    })
    return {
        width: width
    }
}

export function RightPanel(props): Override {
    const width = useTransform(handleX, (value) => {
        return (
            props.width - value
        )
    })
    return {
        width: width
    }
}

源文件:

DragHandle_Starter.framerx.zip