简体   繁体   English

如何防止使用 Socket.io 滚动类似事件? - Angular 7

[英]How to prevent scrolling on like events using Socket.io? - Angular 7

When Array Element Update Use Socket.io then Page Scrolled Down in Angular 7. Please Tell How to stop page scroll down in like event.当数组元素更新使用 Socket.io 然后页面在 Angular 中向下滚动 7. 请告诉如何在类似事件中停止页面向下滚动。

在此处输入图像描述

this.socket.on(discussion.id, (reponse: any) => 
      {
        for (const [index, discussion] of this.discussionList.entries()) {
          if (reponse.data.id == discussion.id)
          {
            reponse.data.displayComment = discussion.displayComment;
            this.discussionList[index] = reponse.data;
            console.log('Updated');
            break; //Stop this loop, we found it!
          }
          console.log('finding....');
        }
      });

You basically updating some random item in array which is rendered as list.您基本上更新了数组中呈现为列表的一些随机项。 Of course your list will change content and new content can appear in current view.当然,您的列表会更改内容,新内容会出现在当前视图中。 Its how html in browser works - nothing related to "Angular 7".它是 html 在浏览器中的工作方式——与“Angular 7”无关。 Scroll not changed - changed what is visible in current scroll.滚动未更改 - 更改了当前滚动中可见的内容。

What you want to do is plain JS madness:你想要做的是简单的 JS 疯狂:

  1. Before updating array 'remember' what was visible in current scrolled view.在更新数组之前,“记住”当前滚动视图中可见的内容。 For example you read scrollTop and they loop over each item in list and check it's offset from top - closest to scrollTop is win.例如,您阅读 scrollTop 并且它们遍历列表中的每个项目并检查它与顶部的偏移量 - 最接近 scrollTop 的是 win。
  2. After list updated find that element and scroll back to it.列表更新后找到该元素并滚动回到它。

It is messy, heavy for performance but thats a price of changing random piece of html on page.它很乱,性能很重,但这是改变页面上随机 html 的代价。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM