Skip to content
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: 32 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions dist/scrollers/NativeScrollBehavior.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare class NativeScrollBehavior {
private readonly rootEl;
private originalValue;
private constructor();
static forWindowObject(): NativeScrollBehavior;
remove(): void;
restore(): void;
}
export declare const nativeScrollBehavior: NativeScrollBehavior;
export {};
37 changes: 37 additions & 0 deletions src/scrollers/NativeScrollBehavior.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
type ScrollBehavior = 'auto' | 'smooth' | 'inherit' | 'initial' | 'revert' | 'revert-layer' | 'unset';

class NativeScrollBehavior
{
private originalValue: ScrollBehavior|null = null;

private constructor(
private readonly rootEl: HTMLHtmlElement,
) {}

public static forWindowObject(): NativeScrollBehavior
{
return new this(
document.documentElement as HTMLHtmlElement,
);
}


public remove(): void
{
this.originalValue = window.getComputedStyle(this.rootEl).getPropertyValue('scrollBehavior') as ScrollBehavior;
this.rootEl.style.scrollBehavior = 'unset';
}

public restore(): void
{
if (this.originalValue === null) {
return;
}

this.rootEl.style.scrollBehavior = this.originalValue;
}

}

// export singleton instance
export const nativeScrollBehavior = NativeScrollBehavior.forWindowObject();
7 changes: 6 additions & 1 deletion src/scrollers/scrollToElement.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import * as Velocity from 'velocity-animate';
import {EASE_IN_SKIP_OUT_EASING} from '../easing/bindEasingToVelocity';
import {nativeScrollBehavior} from './NativeScrollBehavior';

export function scrollToElement(element: HTMLElement, onScrollFinishedCallback?: () => void): void
{
nativeScrollBehavior.remove();
Velocity.animate(element, 'scroll', {
duration: 1200, // todo: different depending on offset from page top?
easing: EASE_IN_SKIP_OUT_EASING,
complete: () => onScrollFinishedCallback !== undefined && onScrollFinishedCallback(),
complete: () => {
nativeScrollBehavior.restore();
onScrollFinishedCallback !== undefined && onScrollFinishedCallback();
},
});
}
7 changes: 6 additions & 1 deletion src/scrollers/scrollToOffset.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as Velocity from 'velocity-animate';
import {EASE_IN_SKIP_OUT_EASING} from '../easing/bindEasingToVelocity';
import {nativeScrollBehavior} from "./NativeScrollBehavior";

export function scrollToOffset(
topOffset: number,
onScrollFinishedCallback?: () => void,
): void
{
nativeScrollBehavior.remove();
/**
* Setting `<html>` as the element to scroll to with offset simulates `window.scrollTo()` behavior.
* See last paragraph at http://velocityjs.org/#scroll
Expand All @@ -14,6 +16,9 @@ export function scrollToOffset(
duration: 1200, // todo: different depending on offset from page top?
offset: topOffset,
easing: EASE_IN_SKIP_OUT_EASING,
complete: onScrollFinishedCallback,
complete: () => {
nativeScrollBehavior.restore();
onScrollFinishedCallback !== undefined && onScrollFinishedCallback();
},
});
}