Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Slider exposing (Model, Msg(..), Slide, getFoot, getHead, getImg, init, main, slideArray, slideLenght, subscriptions, update, view)
- import Array
- import Browser
- import Html exposing (..)
- import Html.Attributes exposing (..)
- import Html.Events exposing (..)
- type alias Slide =
- { img : String
- , head : String
- , foot : String
- }
- slideArray : Array.Array Slide
- slideArray =
- Array.fromList
- [ { img = "img/cat.jpg"
- , head = "Cat"
- , foot = "Look at this cat!"
- }
- , { img = "img/dog.jpg"
- , head = "Dog"
- , foot = "Look at this dog too!"
- }
- , { img = "img/bandurria.jpg"
- , head = "Bandurria"
- , foot = "Don't you like bandurrias? They're awesome!"
- }
- ]
- slideLenght : Int
- slideLenght =
- Array.length slideArray
- getImg : Int -> String
- getImg n =
- case Array.get n slideArray of
- Nothing ->
- ""
- Just element ->
- element.img
- getHead : Int -> String
- getHead n =
- case Array.get n slideArray of
- Nothing ->
- ""
- Just element ->
- element.head
- getFoot : Int -> String
- getFoot n =
- case Array.get n slideArray of
- Nothing ->
- ""
- Just element ->
- element.foot
- type alias Model =
- Int
- type Msg
- = Next
- | Prev
- init : Model
- init =
- 0
- view : Model -> Html Msg
- view model =
- div []
- [ button [ onClick Prev ] [ text "<" ]
- , img [ src (getImg model) ] []
- , div [] [ text (getFoot model) ]
- , text "s"
- , button [ onClick Next ] [ text ">" ]
- ]
- update : Msg -> Model -> Model
- update msg model =
- case msg of
- Next ->
- if model == (slideLenght - 1) then
- 0
- else
- model + 1
- Prev ->
- if model == 0 then
- slideLenght - 1
- else
- model - 1
- subscriptions : Model -> Sub Msg
- subscriptions model =
- Sub.none
- main =
- Browser.sandbox
- { init = init
- , view = view
- , update = update
- }
Advertisement
Add Comment
Please, Sign In to add comment