Browse along a section clipped into a viewport using a mouse.
Vanilla Js - No dependencies - ~987B minified and gzipped
npm install @mystroken/mouse-explorer
Look at the sources files for more information.
import createMouseExplorer from '@mystroken/mouse-explorer';
const viewport = document.querySelector('#viewport');
const section = document.querySelector('#container');
const explorer = createMouseExplorer({ viewport, section });
explorer.on(({ x, y }) => {
section.style.transform = `translate3d(${x},${y},0)`;
});
const explorer = createMouseExplorer({
ease: 0.21,
viewport: document.querySelector('#viewport'),
section: document.querySelector('#container'),
center: true,
});
Determines the viewport that clips the section.
Default | Type | Required |
---|---|---|
Window |
HTMLElement |
No |
Determines the section to be explored.
Default | Type | Required |
---|---|---|
null |
HTMLElement |
Yes |
Set the ease of movement (section moving inside the viewport).
Default | Type | Required |
---|---|---|
0.21 |
Number |
No |
Tells the explorer to center or not the section on start.
Default | Type | Required |
---|---|---|
false |
Boolean |
No |
Add a callback to be called on each mouse move on the section.
Remove a callback.
Get position (in pixel) for a given [-1,1] coordinates.
Argument | Types | Info | Required |
---|---|---|---|
x | Number |
The given x-axis position (in pixel). | Yes |
y | Number |
The given y-axis position (in pixel). | Yes |
const explorer = createMouseExplorer({ viewport, section });
// Position at center
explorer.positionAt(0, 0);
// Returns corresponding
// amount of pixels to slide in order
// to place the section in
// the middle of the viewport.
// { x: 284, y: 97 }