javascript - Check if scrollIntoView is completed - Stack Overflow

I am using the following function in a single page application in Angular. When I click on the menu ite

I am using the following function in a single page application in Angular. When I click on the menu item, I scroll to the relevant div.

scroll (el) {
    this.sharedService.isClicked.next(true);
    el.scrollIntoView({ behavior: 'smooth', block: 'start' });
}

How do I check if the element has finished scrolling? I want to avoid the setTimeout function.

I am using the following function in a single page application in Angular. When I click on the menu item, I scroll to the relevant div.

scroll (el) {
    this.sharedService.isClicked.next(true);
    el.scrollIntoView({ behavior: 'smooth', block: 'start' });
}

How do I check if the element has finished scrolling? I want to avoid the setTimeout function.

Share Improve this question edited Dec 21, 2020 at 15:45 Wai Ha Lee 8,83699 gold badges59 silver badges95 bronze badges asked Oct 17, 2018 at 4:42 user1288906user1288906 4492 gold badges13 silver badges25 bronze badges 1
  • Related: stackoverflow./questions/46795955/… – gbelisario Commented Oct 17, 2018 at 4:45
Add a ment  | 

3 Answers 3

Reset to default 2

This worked for me (I declare this snippet public domain, feel free to re-use):

scrollAndDo = function(currPageXOffset,currPageYOffset) {
    $('#SomeElement')[0].scrollIntoView({behavior: 'smooth',block:'nearest',inline: 'nearest'});
    currPageXOffset = window.pageXOffset;
    currPageYOffset = window.pageYOffset;
    var scrollDone = setInterval(function () {
        if ( currPageXOffset == window.pageXOffset && currPageYOffset == window.pageYOffset ) {
            clearInterval(scrollDone);
            console.log('I have finished scrolling');
        }
        currPageXOffset = window.pageXOffset;
        currPageYOffset = window.pageYOffset;
    },50);
};
scrollAndDo();

hope this helps..

  var scrollIntoviewCompleted = false;
  var currentVisible = false;
  var onceVisible = false;
  $(window).scroll(function(){
     $.fn.isOnScreen = function(){
        var element = this.get(0);
        var bounds = element.getBoundingClientRect();
        return bounds.top < window.innerHeight && bounds.bottom > 0;
    }
        if ($('.targetdiv').isOnScreen()) {
            currentVisible = true;
            onceVisible =true;
        } 
        else
        {
            currentVisible = false;
        }
        if(onceVisible == true && currentVisible == false){
            scrollIntoviewCompleted = true;
            console.log("scrollIntoViewCompleted");
        }

});

Based on an answer in this question I was able to make this for my tests... and this assumes that you are @Injecting window into your angular ponents

let win: Window;

const onScrollEnd = (fn: () => void): void => {
    let scrollTimeout: number | undefined;
    const listener = (): void => {
        win.clearTimeout(scrollTimeout);
        scrollTimeout = win.setTimeout(() => {
            win.removeEventListener('scroll', listener);
            fn();
        }, 100);
    };
    win.addEventListener('scroll', listener);
};

beforeEach(() => {
    //Standard ponent test setup stuff goes here...    

    win = TestBed.get('Window');
});

And then in my test I can do this:

it('should scroll the window down when...', (done: DoneFn) => {
    ponent.shouldScroll = true;
    fixture.detectChanges();

    onScrollEnd(() => {
        expect(win.scrollY).toBeGreaterThan(0);
        done();
    });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744872484a4598344.html

相关推荐

  • javascript - Check if scrollIntoView is completed - Stack Overflow

    I am using the following function in a single page application in Angular. When I click on the menu ite

    1天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信