Giter Club home page Giter Club logo

Comments (3)

luiskabes-arch avatar luiskabes-arch commented on September 28, 2024

I'm using ScrollSmoother from GSAP btw

from cursor-magnetic-demo.

atulkadyan avatar atulkadyan commented on September 28, 2024

Hey Luis can you give your socials or LinkedIn i need your help

from cursor-magnetic-demo.

NSMNIA avatar NSMNIA commented on September 28, 2024

@luiskabes-arch I ran into the same problem with [email protected]. I got it working by not subtracting the window.pageXOffset or window.pageYOffset from the getBoundingClientRect() at this.x and this.y. I did this because the pageYOffset only increased as I scrolled. Below I created a typescript variant without jQuery. Hopefully this will solve your question as well 😀

import gsap from 'gsap';

export default class Magnetic {
    private el: HTMLElement;
    private options: {
        y: number;
        x: number;
        s: number;
        rs: number;
    };
    private y: number = 0;
    private x: number = 0;
    private width: number = 0;
    private height: number = 0;

    constructor(
        el: HTMLElement,
        options: Record<string, string | number> = {}
    ) {
        this.el = el;
        this.options = {
            y: 0.2,
            x: 0.2,
            s: 0.2,
            rs: 0.7,
            ...((this.el.dataset.magnetic as unknown as Record<
                string,
                string | number
            >) || options),
        };

        if (this.el.dataset.magneticInit) return;
        this.el.dataset.magneticInit = 'true';

        this.bind();
    }

    private bind() {
        this.el.addEventListener('mouseenter', () => {
            this.y = this.el.getBoundingClientRect().top;
            this.x = this.el.getBoundingClientRect().left;
            this.width = this.el.offsetWidth;
            this.height = this.el.offsetHeight;
        });

        this.el.addEventListener('mousemove', (e) => {
            const y = (e.clientY - this.y - this.height / 2) * this.options.y;
            const x = (e.clientX - this.x - this.width / 2) * this.options.x;
            this.move(x, y, this.options.s);
        });

        this.el.addEventListener('mouseleave', () => {
            this.move(0, 0, this.options.rs);
        });
    }

    private move(x: number, y: number, speed: number) {
        gsap.to(this.el, {
            y: y,
            x: x,
            force3D: true,
            overwrite: true,
            duration: speed,
        });
    }
}

from cursor-magnetic-demo.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.