Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
ngx-page-scroll
Advanced tools
Convenience directive for animated scrolling functionality for angular written in pure typescript
Animated scrolling functionality for angular written in pure typescript with no additional dependencies
Breaking Change: Starting with v6 this library consists of two modules: ngx-page-scroll-core
(the service) and ngx-page-scroll
(the directive). ➡ How to upgrade
href="#mytarget
) just by adding pageScroll
directiveFirst you need to install the core npm module:
npm install ngx-page-scroll-core --save
Then add the NgxPageScrollModule
to the imports array of your application module:
import { NgxPageScrollCoreModule } from 'ngx-page-scroll-core';
@NgModule({
imports: [
/* Other imports here */
NgxPageScrollCoreModule
]
})
export class AppModule {
}
Using the PageScrollService#scroll
method you may trigger scroll animations. Provide an options object that provides a reference to the document and the scroll target. Additional properties are optional.
import { DOCUMENT, Inject } from '@angular/common';
import { PageScrollService } from 'ngx-page-scroll-core';
export class MyComponent {
constructor(private pageScrollService: PageScrollService, @Inject(DOCUMENT) private document: any) {
}
ngOnInit(): void {
this.pageScrollService.scroll({
document: this.document,
scrollTarget: '.theEnd',
});
}
}
Note: The scroll()
method is a shorthand from creating a PageScrollInstance
(an object encapsulating all information
relevant for performing a scroll animation) using PageScrollService#create
and starting it using
the PageScrollService#start
method.
When importing the PageScrollCoreModule
one can provide option overrides:
imports: [
...
NgxPageScrollCoreModule.forRoot({duration: 2500, easingLogic: ...}),
]
Check the PageScrollConfig
interface
for possible options and their impact. The default values may be found in the defaultPageScrollConfig
object.
For ease of use a directive pageScroll
exists, which allows you to quickly add scroll animations to your angular app by
adding a property to your existing HTML a-tags. It can also work cross-routes, meaning it will start the scroll animation
after the target route has been loaded.
It utilizes the ngx-page-scroll-core module for that, thus requires it as a peer dependency.
First you need to install the directive npm module:
npm install ngx-page-scroll --save
Then add the NgxPageScrollModule
to the imports array of your application module:
import { NgxPageScrollModule } from 'ngx-page-scroll';
@NgModule({
imports: [
/* Other imports here */
NgxPageScrollModule
]
})
export class AppModule {
}
In your template you may add the pageScroll
attribute to elements with an href
attribute pointing towards an id on
the same page (e.g. #theId
). The onClick
event will be interrupted and the scroll animation starts.
Alternatively you may set the optional pageScrollTarget
property to a valid css selector to specify the
target element to scroll to.
@Component({
...
template: `...
<a pageScroll href="#awesomePart">Take me to the awesomeness</a>
<!-- Further content here -->
<h2 id="awesomePart">This is where the awesome happens</h2>
...`,
})
export class MyComponent {
}
Additional attributes may be set on an DOM element using the pageScroll
directive for customization.
They take precedence over the default settings specified in PageScrollConfig
class. Thereby it is
possible to have all page scroll-animations last e.g. 2 seconds, but a specific one should be performed with a custom easing function and a duration
of only 1 second.
Attribute | Type | Default | Description |
---|---|---|---|
pageScroll | Attribute adding scroll-animation behavior when the click -event happens on the element. | ||
pageScrollTarget | string | Optional attribute to set the element that should be scrolled to. Takes precedence over the ´href´-value | |
pageScrollHorizontal | boolean | false | Whether the scroll should happen in vertical direction (false , default) or horizontal (true ). |
pageScrollOffset | number | 0 | Pixels to offset from the top of the element when scrolling to (positive value = scrolling will stop given pixels atop the target element). |
pageScrollDuration | number | 1250 | Duration in milliseconds the whole scroll-animation should last. |
pageScrollSpeed | number | - | Speed in Pixel/Second the animation should take. Only applied if no duration is set. |
pageScrollInterruptible | boolean | true | Whether the scroll animation should stop if the user interferes with it (true ) or not (false ). |
pageScrollAdjustHash | boolean | false | Whether the routes hash/fragment should be updated to reflect to section that has been scrolled to |
pageScrollEasing | EasingLogic | linearEasing | Easing method to be used while calculating the scroll position over time (default is linear easing). |
Event | Type | Description |
---|---|---|
pageScrollFinish | boolean | Fired when the scroll-animation stops. Emits a boolean value which indicates whether the scroll animation finished successfully and reached its target (true ) or not (false ). Possible reasons for false: target not found or interrupted due to another scroll animation starting or user interaction. |
The following example will check whether the route Home is currently loaded. If this is true, the scroll-animation will be performed with the default properties. If a different route is loaded, a subscription for route changes will be made and the scroll-animation will be performed as soon as the new route is loaded.
<a pageScroll [routerLink]="['Home']" href="#myanchor">Go there</a>
Overriding all possible properties. doSmth()
and myEasing
are
defined in the component
<a pageScroll [pageScrollOffset]="0" [pageScrollDuration]="2000" [pageScrollEasing]="myEasing" [pageScrollInterruptible]="false" (pageScrollFinish)="doSmth($event)" href="#theanchor">Visit</a>
public myEasing: EasingLogic = (t: number, b: number, c: number, d: number): number => {
// easeInOutExpo easing
if (t === 0) {
return b;
}
if (t === d) {
return b + c;
}
if ((t /= d / 2) < 1) {
return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
}
return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
}
doSmth(reachedTarget: boolean): void {
if (reachedTarget) {
console.log('Yeah, we reached our destination');
} else {
console.log('Ohoh, something interrupted us');
}
}
6.0.0 (2019-05-22)
FAQs
Convenience directive for animated scrolling functionality for angular written in pure typescript
The npm package ngx-page-scroll receives a total of 5,288 weekly downloads. As such, ngx-page-scroll popularity was classified as popular.
We found that ngx-page-scroll demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.