I am adding a <div>
to the <div>
wrapper, and I need to be able to scroll to the last one added each time. How to do it in Elm?
<div class="messages" style="height: 7em; overflow: scroll"> <div>Anonymous: Hello</div> <div>John: Hi</div> </div>
Intuitively, it seems I could call port
, which runs the JavaScript code element.scrollTop = element.scrollHeight
:
AddChatMessage chatMessage -> ( { model | chatMessages = model.chatMessages ++ [ chatMessage ] } , scrollToBottomPort "div.messages" )
Problem: scrollToBottom
gets called before model
updated. So it doesnβt matter, I convert it to Task
. But still, even if the model
now being updated, the view
has not yet been updated. Therefore, I finish scrolling to the second element from the bottom!
This may lead to a more general question that interests me in Elm, how do you run Cmd
after updating the view due to a model change?
source share