简体   繁体   English

有没有办法显示溢出其父元素的 ul 元素的最后一个 li 元素?

[英]Is there a way to display the last li element of a ul element that overflows its parent?

Background背景

I'm working on a chat app in react where I have a ul element that displays messages using li elements.我正在开发一个聊天应用程序,其中我有一个 ul 元素,它使用 li 元素显示消息。 At some point the ul element overflows its parent element.在某些时候,ul 元素会溢出其父元素。

Problem问题

Now, the problem is when I switch chats.现在,问题是当我切换聊天时。 Is there a way I can make the ul element display the li elements that have overflowed, that is, display the last li elements of the ul element instead of displaying from the first li elements and have the user scroll to the bottom because that would make for a terrible user experience.有没有一种方法可以让 ul 元素显示溢出的 li 元素,即显示 ul 元素的最后一个 li 元素,而不是从第一个 li 元素显示,并让用户滚动到底部,因为那样会使糟糕的用户体验。

I tried using the scrollIntoView() method to make the ul element scroll to the last message whenever a new li element is added and this works fine while the user is in a chat but once the user switches chats, the new chat's ul element visibly scrolls to the last element in it.我尝试使用 scrollIntoView() 方法让 ul 元素在添加新的 li 元素时滚动到最后一条消息,这在用户聊天时工作正常,但是一旦用户切换聊天,新聊天的 ul 元素就会明显滚动到其中的最后一个元素。 I would like to display the elements from the bottom without the ul element scrolling at all.我想从底部显示元素而不滚动 ul 元素。

Code Sample代码示例

const MessagingScreen = ({ openChat }) => {
  const messagesEnd = useRef();
  const [message, setMessage] = useState("");

  const scrollToBottom = () => {
    if (openChat.messages.length > 0) {
      messagesEnd.current.scrollIntoView({ behaivor: "smooth", block: "start" });
    }
  }

  useEffect(() => {
    scrollToBottom();
  }, [openChat.messages])

  return (
    <main className="flex flex-column flex-grow mh-50">
        <ul className="chat-msgs flex-grow w-100 h-100 bg-white overflow-y-scroll instant">
          {openChat.messages.map(chatMessageDetails =>
            <ChatMessageItem {...chatMessageDetails} key={chatMessageDetails.message}/>)
          }
          <div ref={messagesEnd}/>
        </ul>
     </main>
  )
 }

I think this might be what you're looking for scrollTop : https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop我认为这可能是您正在寻找的scrollTophttps://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop

Explicitly setting scrollTop of an element will scroll it instantly to the value you specify.显式设置元素的scrollTop将立即将其滚动到您指定的值。

You would use this in conjunction with scrollHeight : https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight您可以将它与scrollHeight结合使用: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight

scrollHeight is the total amount the element can scroll, including the invisible bits that are overflowed. scrollHeight是元素可以滚动的总量,包括溢出的不可见位。

So an example would be:所以一个例子是:

var myListElement = document.getElementById('myList');
myListElement.scrollTop = myListElement.scrollHeight

throwing this in your start function should scroll the list to the bottom on init.把它扔到你的开始 function 应该在初始化时将列表滚动到底部。 scrollTop is 0 at the start and should be set to the max scrollHeight on init. scrollTop在开始时为 0,应在初始化时设置为最大scrollHeight

Hope this helps希望这可以帮助

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

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