Angular2 Smoothscroll不适用于Internet Explorer 9

时间:2016-08-11 14:18:45

标签: javascript html angularjs

我遇到的问题是我的smoothscroll服务在Internet Explorer 9中无法正常运行。我已经在Chrome和Firefox中测试了这段代码,它似乎功能齐全。

我调用服务的HTML代码

<div (click)="scrollTo('scrollTarget')" class="center-text"><img class="img-responsive" src="/images/downArrow.png"></div>

服务:

import {Injectable} from '@angular/core';
@Injectable() 
export class SmoothScroll {
    constructor(){}
    _scrollTo(yPoint: number, duration: number) {
        setTimeout(() => {
            window.scrollTo(0, yPoint)
        }, duration);
        return;
    }
    smoothScroll(eID) {
        //Get Current/Target Positions

        let startY = window.scrollY, stopY = document.getElementById(eID).offsetTop;

        var distance = stopY > startY ? stopY - startY : startY - stopY;
        if (distance < 100) {
            window.scrollTo(0, stopY); return;
        }
        var speed = Math.round(distance / 10);
        if (speed >= 20) speed = 20;
        var step = Math.round(distance / 10);
        var leapY = stopY > startY ? startY + step : startY - step;
        var timer = 0;
        if (stopY > startY) {
            for (var i = startY; i < stopY; i += step) {
                this._scrollTo(leapY, timer * speed);
                leapY += step; if (leapY > stopY) leapY = stopY; timer++;
            } return;
        }
        for (var i = startY; i > stopY; i -= step) {
            this._scrollTo(leapY, timer * speed);
            leapY -= step; if (leapY < stopY) leapY = stopY; timer++;
        }
    }
}

0 个答案:

没有答案