简体   繁体   English

光标跟随效果不会跟随用户滚动屏幕

[英]Cursor follow effect doesn't follow the user down the screen on scroll

I have a cursor follow effect on my new landing page:我的新登陆页面上有一个光标跟随效果:
https://tiny-cendol-e90774.netlify.app/ https://tiny-cendol-e90774.netlify.app/

For some reason when you scroll down the page, the cursor doesn't follow you down?出于某种原因,当您向下滚动页面时,光标没有跟随您向下滚动?

Here's the code below:这是下面的代码:

const Layout = ({children}) => {

  const [x, setX] = useState(0);
  const [y, setY] = useState(0);

  useLayoutEffect(() => {
    document.onmousemove = (event) => {
      var e = event;
      setX(e.clientX);
      setY(e.clientY);
    };
  });

  return (
    <div className={styles.layout}>
      <div style={{top: y - 14, left: x - 14}} className={styles.cursor} />;
      <Header />
      {children}
      <Footer />
    </div>
  )
}

And here's the styles:这是样式:

.cursor {
  width: 30px;
  height: 30px;
  display: none;
  border-radius: 50%;
  position: absolute;
  border: 2px solid $white;
  transition-duration: 0.4s;
  transition-timing-function: cubic-bezier(0.18, 0.89, 0.32, 1.28);

  &:after {
    top: 50%;
    left: 50%;
    content: '';
    width: 5px;
    height: 5px;
    position: absolute;
    background-color: $white;
    border-radius: 50%;
    transform: translate(-50%, -50%);
  }

  @media all and (min-width: $tablet) {
    display: block;
  }
}

I'm assuming that the e.clientX and e.clientY have no context of the browser dimensions, so aren't working on scroll.我假设 e.clientX 和 e.clientY 没有浏览器维度的上下文,所以不在滚动上工作。

You can use the event.pageX and event.pageY properties to obtain the mouse position from the page start.您可以使用event.pageXevent.pageY属性从页面开始获取鼠标位置。

const Layout = ({children}) => {

  const [x, setX] = useState(0);
  const [y, setY] = useState(0);

  useLayoutEffect(() => {
    document.onmousemove = (event) => {
      var e = event;
      setX(e.pageX);
      setY(e.pageY);
    };
  });

  return (
    <div className={styles.layout}>
      <div style={{top: y - 14, left: x - 14}} className={styles.cursor} />;
      <Header />
      {children}
      <Footer />
    </div>
  )
}

This will fix your issue, but it doesn't update the cursor while scrolling.这将解决您的问题,但它不会在滚动时更新光标。

I'm not sure if you want to use useState for the purpose of moving a cursor on the screen.我不确定你是否想使用useState来移动屏幕上的光标。 You can use a ref as well for this purpose.为此,您也可以使用 ref。 Also, don't forget to clear the event listener when the component un mounts (even when you never unmount the Layout component).另外,不要忘记在卸载组件时清除事件侦听器(即使您从未卸载 Layout 组件)。

const Layout = ({ children }) => {
  const cursorRef = useRef();

  useLayoutEffect(() => {
    const handleMouseMove = (event) => {
      if (cursorRef.current) {
        cursorRef.current.style.top = `${event.pageY}px`;
        cursorRef.current.style.left = `${event.pageX}px`;
      }
    };

    window.addEventListener("mousemove", handleMouseMove);

    return () => {
      window.removeEventListener("mousemove", handleMouseMove);
    };
  });

  return (
    <div className={styles.layout}>
      <div className="cursor" ref={cursorRef} />
      <Header />
      {children}
      <Footer />
    </div>
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM