UNPKG

@lxlib/theme

Version:

This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 9.1.1.

92 lines 7.06 kB
/** * @fileoverview added by tsickle * Generated from: src/services/scroll/scroll.service.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ import { DOCUMENT } from '@angular/common'; import { Inject, Injectable } from '@angular/core'; import { WINDOW } from '../../win_tokens'; import * as i0 from "@angular/core"; import * as i1 from "../../win_tokens"; import * as i2 from "@angular/common"; export class ScrollService { /** * @param {?} win * @param {?} doc */ constructor(win, doc) { this.win = win; this.doc = doc; } /** * 获取滚动条位置 * @param {?=} element 指定元素,默认 `window` * @return {?} */ getScrollPosition(element) { if (element && element !== this.win) { return [element.scrollLeft, element.scrollTop]; } else { return [this.win.pageXOffset, this.win.pageYOffset]; } } /** * 设置滚动条位置 * @param {?} element 指定元素 * @param {?} position * @return {?} */ scrollToPosition(element, position) { (element || this.win).scrollTo(position[0], position[1]); } /** * 设置滚动条至指定元素 * @param {?=} element 指定元素,默认 `document.body` * @param {?=} topOffset 偏移值,默认 `0` * @return {?} */ scrollToElement(element, topOffset = 0) { if (!element) element = this.doc.body; (/** @type {?} */ (element)).scrollIntoView(); /** @type {?} */ const w = this.win; if (w && w.scrollBy) { w.scrollBy(0, (/** @type {?} */ (element)).getBoundingClientRect().top - topOffset); if (w.pageYOffset < 20) { w.scrollBy(0, -w.pageYOffset); } } } /** * 滚动至顶部 * @param {?=} topOffset 偏移值,默认 `0` * @return {?} */ scrollToTop(topOffset = 0) { this.scrollToElement(this.doc.body, topOffset); } } ScrollService.decorators = [ { type: Injectable, args: [{ providedIn: 'root' },] } ]; /** @nocollapse */ ScrollService.ctorParameters = () => [ { type: undefined, decorators: [{ type: Inject, args: [WINDOW,] }] }, { type: undefined, decorators: [{ type: Inject, args: [DOCUMENT,] }] } ]; /** @nocollapse */ ScrollService.ɵprov = i0.ɵɵdefineInjectable({ factory: function ScrollService_Factory() { return new ScrollService(i0.ɵɵinject(i1.WINDOW), i0.ɵɵinject(i2.DOCUMENT)); }, token: ScrollService, providedIn: "root" }); if (false) { /** * @type {?} * @private */ ScrollService.prototype.win; /** * @type {?} * @private */ ScrollService.prototype.doc; } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2Nyb2xsLnNlcnZpY2UuanMiLCJzb3VyY2VSb290Ijoibmc6Ly9AbHhsaWIvdGhlbWUvIiwic291cmNlcyI6WyJzcmMvc2VydmljZXMvc2Nyb2xsL3Njcm9sbC5zZXJ2aWNlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7O0FBQUEsT0FBTyxFQUFFLFFBQVEsRUFBRSxNQUFNLGlCQUFpQixDQUFDO0FBQzNDLE9BQU8sRUFBRSxNQUFNLEVBQUUsVUFBVSxFQUFFLE1BQU0sZUFBZSxDQUFDO0FBQ25ELE9BQU8sRUFBRSxNQUFNLEVBQUUsTUFBTSxrQkFBa0IsQ0FBQzs7OztBQUcxQyxNQUFNLE9BQU8sYUFBYTs7Ozs7SUFDeEIsWUFBb0MsR0FBUSxFQUE0QixHQUFRO1FBQTVDLFFBQUcsR0FBSCxHQUFHLENBQUs7UUFBNEIsUUFBRyxHQUFILEdBQUcsQ0FBSztJQUFHLENBQUM7Ozs7OztJQU1wRixpQkFBaUIsQ0FBQyxPQUFpQjtRQUNqQyxJQUFJLE9BQU8sSUFBSSxPQUFPLEtBQUssSUFBSSxDQUFDLEdBQUcsRUFBRTtZQUNuQyxPQUFPLENBQUMsT0FBTyxDQUFDLFVBQVUsRUFBRSxPQUFPLENBQUMsU0FBUyxDQUFDLENBQUM7U0FDaEQ7YUFBTTtZQUNMLE9BQU8sQ0FBQyxJQUFJLENBQUMsR0FBRyxDQUFDLFdBQVcsRUFBRSxJQUFJLENBQUMsR0FBRyxDQUFDLFdBQVcsQ0FBQyxDQUFDO1NBQ3JEO0lBQ0gsQ0FBQzs7Ozs7OztJQU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