Skip to content

Commit

Permalink
Add scrollto margin
Browse files Browse the repository at this point in the history
  • Loading branch information
hojberg committed Aug 15, 2024
1 parent ca13d55 commit 6eb3626
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Lib/ScrollTo.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import Task

scrollTo : msg -> String -> String -> Cmd msg
scrollTo doneMsg containerId targetId =
scrollTo_ doneMsg containerId targetId 0


scrollTo_ : msg -> String -> String -> Float -> Cmd msg
scrollTo_ doneMsg containerId targetId marginTop =
Task.sequence
[ Dom.getElement targetId |> Task.map (.element >> .y)
, Dom.getElement containerId |> Task.map (.element >> .y)
Expand All @@ -15,7 +20,7 @@ scrollTo doneMsg containerId targetId =
(\ys ->
case ys of
elY :: viewportY :: viewportScrollTop :: [] ->
Dom.setViewportOf containerId 0 (viewportScrollTop + (elY - viewportY))
Dom.setViewportOf containerId 0 (viewportScrollTop + (elY - viewportY) - marginTop)
|> Task.onError (\_ -> Task.succeed ())

_ ->
Expand Down

0 comments on commit 6eb3626

Please sign in to comment.