10 examples of 'angular 2 scroll to top' in JavaScript

Every line of 'angular 2 scroll to top' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
53scrollToTop(topOffset = 0) {
54 this.scrollToElement(this.doc.body, topOffset);
55}
68scrollToTop(): void {
69 window.scroll(0, 0);
70}
57scrollPageToTop() {
58 this.$.headerPanelMain.scrollToTop(true);
59}
38scrollToTop() {
39 $('html, body').animate({
40 scrollTop: 0,
41 }, 500);
42}
53function scrollToTop($rootScope, $document) {
54 $rootScope.$on('$stateChangeSuccess', function() {
55 $document.scrollTop(0);
56 $('#wrapper').scrollTop(0);
57 });
58}
435public scrollContent(y: number, isWheel: boolean, isJump = false) {
436 this._releaseScroll = false;
437 let delta: number = y;
438 const maxTop: number = this._me.offsetHeight - this._bar.offsetHeight;
439
440 if (isWheel) {
441 // move bar with mouse wheel
442 delta = parseInt(this._bar.style.top, 10) + y * this._options.wheelStep / 100 * this._bar.offsetHeight;
443
444 // move bar, make sure it doesn't go out
445 delta = Math.min(Math.max(delta, 0), maxTop);
446
447 // if scrolling down, make sure a fractional change to the
448 // scroll position isn't rounded away when the scrollbar's CSS is set
449 // this flooring of delta would happened automatically when
450 // bar.css is set below, but we floor here for clarity
451 delta = (y > 0) ? Math.ceil(delta) : Math.floor(delta);
452
453 // scroll the scrollbar
454 this._renderer.setStyle(this._bar, 'top', `${delta}px`);
455 }
456
457 // calculate actual scroll amount
458 this._percentScroll = parseInt(this._bar.style.top, 10) / (this._me.offsetHeight - this._bar.offsetHeight);
459 delta = this._percentScroll * (this._me.scrollHeight - this._me.offsetHeight);
460
461 if (isJump) {
462 delta = y;
463 let offsetTop = delta / this._me.scrollHeight * this._me.offsetHeight;
464 offsetTop = Math.min(Math.max(offsetTop, 0), maxTop);
465 this._renderer.setStyle(this._bar, 'top', `${offsetTop}px`);
466 }
467
468 // scroll content
469 this._me.scrollTop = delta;
470
471 // ensure bar is visible
472 this.showBar();
473
474 // trigger hide when scroll is stopped
475 this.hideBar();
476}
257public scrollToRight(speed: number = this.scrollSpeed, offset?: number): void {
258 // istanbul ignore else
259 if (this.scrollbar) {
260 this.scrollbar.scrollToRight(offset, speed);
261 }
262}
424scrollToTop(scrollDuration) {
425 if (!isBrowser) return;
426
427 const scrollHeight = window.scrollY;
428 const scrollStep = Math.PI / (scrollDuration / 15);
429 const cosParameter = scrollHeight / 2;
430
431 let scrollCount = 0;
432 let scrollMargin;
433 const scrollInterval = setInterval(() => {
434 if (window.scrollY > this.relativeTop) {
435 scrollCount += 1;
436 scrollMargin =
437 cosParameter - cosParameter * Math.cos(scrollCount * scrollStep);
438 window.scrollTo(0, scrollHeight - scrollMargin);
439 } else {
440 clearInterval(scrollInterval);
441 }
442 }, 16);
443}
40private scrollInView(parent: HTMLElement, target: HTMLElement) {
41 const parentRect = parent.getBoundingClientRect();
42 const targetRect = target.getBoundingClientRect();
43
44 const body = document.body;
45
46 const offset = (targetRect.top + body.scrollTop) - (parentRect.top + body.scrollTop);
47
48 const scroll = parent.scrollTop;
49
50 if (offset < 0) {
51 this.renderer.setProperty(parent, 'scrollTop', scroll + offset);
52 } else {
53 const targetHeight = targetRect.height;
54 const parentHeight = parentRect.height;
55
56 if ((offset + targetHeight) > parentHeight) {
57 this.renderer.setProperty(parent, 'scrollTop', scroll + offset - parentHeight + targetHeight);
58 }
59 }
60}
91private scrollToTheTop() {
92 // making sure that we are scrolled back to the correct position after opening the detail panel
93 if (!this.isPanelOpen()) {
94 window.scrollTo(0, SharedService.preScrollPosition);
95 }
96}

Related snippets