Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for scroll delay #540

Open
wants to merge 4 commits into
base: v5-beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion bundled/locomotive-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,31 @@
return Math.abs(curr - target) < Math.abs(prev - target) ? curr : prev;
});
}
/**
* Linear interpolation between two numbers.
* @param {number} start
* @param {number} end
* @param {number} amt
* @returns {number}
*/
function lerp(start, end, amt) {
return (1 - amt) * start + amt * end;
}

function getTranslate(el) {
var translate = {
x: 0,
y: 0
};
var transform = el.style.transform;
var matches = transform.match(/translate3d\(([^)]+)\)/);
if (matches) {
var parts = matches[1].split(', ');
translate.x = parseFloat(parts[0]);
translate.y = parseFloat(parts[1]);
}
return translate;
}

/** Constants */
var INVIEW_CLASS = 'is-inview';
Expand Down Expand Up @@ -291,6 +316,7 @@
scrollCssProgress: this.$el.dataset['scrollCssProgress'] != null,
scrollEventProgress: (_this$$el$dataset$scr4 = this.$el.dataset['scrollEventProgress']) != null ? _this$$el$dataset$scr4 : null,
scrollSpeed: this.$el.dataset['scrollSpeed'] != null ? parseFloat(this.$el.dataset['scrollSpeed']) : null,
scrollDelay: this.$el.dataset['scrollDelay'] != null ? parseFloat(this.$el.dataset['scrollDelay']) : null,
scrollRepeat: this.$el.dataset['scrollRepeat'] != null,
scrollCall: (_this$$el$dataset$scr5 = this.$el.dataset['scrollCall']) != null ? _this$$el$dataset$scr5 : null,
scrollCallSelf: this.$el.dataset['scrollCallSelf'] != null,
Expand Down Expand Up @@ -377,7 +403,13 @@
var _progress = mapRange(0, 1, -1, 1, this.progress);
this.translateValue = _progress * wSize * this.attributes.scrollSpeed * -1;
}
this.$el.style.transform = this.scrollOrientation === 'vertical' ? "translate3d(0, " + this.translateValue + "px, 0)" : "translate3d(" + this.translateValue + "px, 0, 0)";
if (this.attributes.scrollDelay) {
var start = getTranslate(this.$el);
var lerped = this.scrollOrientation == 'vertical' ? lerp(start.y, this.translateValue, this.attributes.scrollDelay) : lerp(start.x, this.translateValue, this.attributes.scrollDelay);
this.$el.style.transform = this.scrollOrientation === 'vertical' ? "translate3d(0, " + lerped + "px, 0)" : "translate3d(" + lerped + "px, 0, 0)";
} else {
this.$el.style.transform = this.scrollOrientation === 'vertical' ? "translate3d(0, " + this.translateValue + "px, 0)" : "translate3d(" + this.translateValue + "px, 0, 0)";
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion bundled/locomotive-scroll.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/locomotive-scroll.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/locomotive-scroll.cjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/locomotive-scroll.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/locomotive-scroll.mjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/locomotive-scroll.modern.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/locomotive-scroll.modern.mjs.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/locomotive-scroll.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/locomotive-scroll.umd.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface IScrollElementOptions {
}
export interface IScrollElementAttributes {
scrollClass: string;
scrollDelay: number | null;
scrollOffset: string;
scrollPosition: string;
scrollModuleProgress: boolean;
Expand Down
8 changes: 8 additions & 0 deletions dist/types/utils/maths.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ export declare function normalize(min: number, max: number, value: number): numb
* @returns {number} - Closest number.
*/
export declare function closestNumber(array: number[], target: number): number;
/**
* Linear interpolation between two numbers.
* @param {number} start
* @param {number} end
* @param {number} amt
* @returns {number}
*/
export declare function lerp(start: number, end: number, amt: number): number;
4 changes: 4 additions & 0 deletions dist/types/utils/translate.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare function getTranslate(el: HTMLElement): {
x: number;
y: number;
};
29 changes: 24 additions & 5 deletions src/core/ScrollElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import {
scrollCallFrom,
scrollOrientation,
} from '../types';
import { clamp, closestNumber, normalize, mapRange } from '../utils/maths';
import { clamp, closestNumber, normalize, mapRange, lerp } from '../utils/maths';
import { getTranslate } from '../utils/translate';

/** Constants */
const INVIEW_CLASS = 'is-inview';
Expand Down Expand Up @@ -96,6 +97,10 @@ export default class ScrollElement {
this.$el.dataset['scrollSpeed'] != null
? parseFloat(this.$el.dataset['scrollSpeed'])
: null,
scrollDelay:
this.$el.dataset['scrollDelay'] != null
? parseFloat(this.$el.dataset['scrollDelay'])
: null,
scrollRepeat: this.$el.dataset['scrollRepeat'] != null,
scrollCall: this.$el.dataset['scrollCall'] ?? null,
scrollCallSelf: this.$el.dataset['scrollCallSelf'] != null,
Expand Down Expand Up @@ -205,10 +210,24 @@ export default class ScrollElement {
progress * wSize * this.attributes.scrollSpeed * -1;
}

this.$el.style.transform =
this.scrollOrientation === 'vertical'
? `translate3d(0, ${this.translateValue}px, 0)`
: `translate3d(${this.translateValue}px, 0, 0)`;
if (this.attributes.scrollDelay) {
const start = getTranslate(this.$el)

const lerped = this.scrollOrientation == 'vertical' ?
lerp(start.y, this.translateValue, this.attributes.scrollDelay) :
lerp(start.x, this.translateValue, this.attributes.scrollDelay)

this.$el.style.transform =
this.scrollOrientation === 'vertical'
? `translate3d(0, ${lerped}px, 0)`
: `translate3d(${lerped}px, 0, 0)`;
}
else {
this.$el.style.transform =
this.scrollOrientation === 'vertical'
? `translate3d(0, ${this.translateValue}px, 0)`
: `translate3d(${this.translateValue}px, 0, 0)`;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export interface IScrollElementOptions {
*/
export interface IScrollElementAttributes {
scrollClass: string;
scrollDelay: number | null;
scrollOffset: string;
scrollPosition: string;
scrollModuleProgress: boolean;
Expand Down
12 changes: 12 additions & 0 deletions src/utils/maths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,15 @@ export function closestNumber(array: number[], target: number): number {
return Math.abs(curr - target) < Math.abs(prev - target) ? curr : prev;
});
}


/**
* Linear interpolation between two numbers.
* @param {number} start
* @param {number} end
* @param {number} amt
* @returns {number}
*/
export function lerp(start: number, end: number, amt: number): number {
return (1 - amt) * start + amt * end;
}
15 changes: 15 additions & 0 deletions src/utils/translate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function getTranslate(el: HTMLElement) {
const translate = {x: 0, y: 0};

const transform = el.style.transform

let matches = transform.match(/translate3d\(([^)]+)\)/);

if (matches) {
const parts = matches[1].split(', ');
translate.x = parseFloat(parts[0]);
translate.y = parseFloat(parts[1]);
}

return translate;
}