Comments (3)
I'm using ScrollSmoother from GSAP btw
from cursor-magnetic-demo.
Hey Luis can you give your socials or LinkedIn i need your help
from cursor-magnetic-demo.
@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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cursor-magnetic-demo.