Skip to content

Instantly share code, notes, and snippets.

@cobalamin
Created July 26, 2016 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cobalamin/6a5e009df6dcf53817030b4a4a226ecb to your computer and use it in GitHub Desktop.
Save cobalamin/6a5e009df6dcf53817030b4a4a226ecb to your computer and use it in GitHub Desktop.
Setting `scrollTop` doesn't work initially
module ScrollTest exposing (..)
import Html.App as Html
import Html exposing (Html, Attribute, div, text, button)
import Html.Attributes exposing (style, property)
import Html.Events exposing (onClick)
import Json.Encode as Json
import String
main : Program Never
main =
Html.beginnerProgram
{ model = model
, view = view
, update = update
}
-- MODEL
type alias Model =
Bool
model : Model
model =
True
-- UPDATE
type Msg
= Toggle
update : Msg -> Model -> Model
update msg model =
case msg of
Toggle ->
not model
-- VIEW
view : Model -> Html Msg
view model =
div []
[ button [ onClick Toggle ] [ text "Toggle" ]
, div (divStyle model)
[ text aLotOfText ]
, div [] [ text (toString model) ]
]
(=>) =
(,)
divStyle : Bool -> List (Attribute msg)
divStyle scrolled =
[ style
[ "border" => "1px solid black"
, "overflow" => "scroll"
, "width" => "100px"
, "height" => "100px"
]
]
++ if scrolled then
[ property "scrollTop" (Json.int 100) ]
else
[]
aLotOfText : String
aLotOfText =
String.repeat 20 "A lot of text! "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment