4

我正在使用 purescript-halogen,当子组件的消息被捕获时,我想滚动到 div 的底部。但是,Halogen 中似乎没有滚动动作控制。那么,如何 滚动到 div 的底部?

我认为一种解决方案是在事件捕获时从 Main 调用其他进程,而不是 Halogen 进程。我不确定这个解决方案是否不错。

4

2 回答 2

4

设置滚动位置只是通过使用正常的 DOM 功能来完成,目标是渲染的节点。

为此,您需要将refHTML DSL 中的属性添加到要滚动的节点:

-- Define this in the same module as / in the `where` for a component
containerRef ∷ H.RefLabel
containerRef = H.RefLabel "container"

-- Use it with the `ref` property like so:
render =
  HH.div
    [ HP.ref containerRef ]
    [ someContent ]

然后在evalfor 组件中,您可以使用 获取实际创建的 DOM 元素,getHTMLElementRef然后更新其滚动位置:

eval (ScrollToBottom next) = do
  ref ← H.getHTMLElementRef containerRef
  for_ ref \el → H.liftEff do
    scrollHeight ← DOM.scrollHeight el
    offsetHeight ← DOM.offsetHeight el
    let maxScroll ← scrollHeight - offsetHeight 
    DOM.setScrollTop maxScroll el
  pure next

这里的片段是从一些现实世界的代码修改而来的,这些代码做类似的事情,所以应该这样做!

于 2017-06-14T11:20:50.760 回答
0

基本上与https://stackoverflow.com/a/44543329/1685973相同的答案,但我很难找到不同的进口:

import Halogen as H
import Halogen.HTML as HH
import Data.Foldable (for_)
import DOM.HTML.Types (htmlElementToElement)
import DOM.Node.Element (scrollHeight, setScrollTop)
import DOM.HTML.HTMLElement (offsetHeight)

...

-- Define this in the same module as / in the `where` for a component
containerRef ∷ H.RefLabel
containerRef = H.RefLabel "container"

-- Use it with the `ref` property like so:
render =
  HH.div
    [ HP.ref containerRef ]
    [ someContent ]

...

eval (ScrollToBottom next) = do
  ref <- H.getHTMLElementRef containerRef
  for_ ref $ \el -> H.liftEff $ do
    let hel = htmlElementToElement el
    sh <- scrollHeight hel
    oh <- offsetHeight el
    let maxScroll = sh - oh
    setScrollTop maxScroll hel
  pure next
于 2018-03-13T08:52:13.343 回答