【问题标题】:React scroll to bottom of Element without scrolling iFrame parent在不滚动 iFrame 父级的情况下将滚动反应到 Element 底部
【发布时间】:2020-12-09 21:31:51
【问题描述】:

我有一个反应组件,需要在给定特定触发器的情况下滚动到底部。 Following the answers in this SO question 在这些方面似乎是一个不错且简单的解决方案(完整示例可以在链接的问题中找到):


const scrollToBottom = () => {
  if (messagesEndRef.current) {
    messagesEndRef.current.scrollIntoView({ behavior: "smooth" });
  }
};

但是,在将应用程序嵌入 iFrame 后,结果发现这也会触发父页面滚动,这很糟糕。我尝试了this question 的答案中链接的解决方案的不同变体,但没有成功。

编辑:这些失败尝试的一些示例:

// document.documentElement.scrollTop = messagesEndRef.current.offsetTop;
// messagesEndRef.current.parentNode.scrollTop = messagesEndRef.current.offsetTop;

任何指向如何实现相同滚动行为但不影响 iFrame 父页面的指针将不胜感激。

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    没关系,this answer to another question fixed it

    未来访问者的代码:

    
    messagesEndRef.current.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'start' })
    
    

    编辑:嗯,这显然适用于除 Safari 和 IE 之外的所有浏览器......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-21
      • 2011-04-17
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多