【发布时间】:2016-01-01 10:07:36
【问题描述】:
我正在尝试监听“resize”事件并相应地更改图像大小。但是图像大小不会改变。我认为原因是我的“onResize”函数在错误的地方。但我不知道在这个框架中还有什么地方可以嵌入它。抱歉,如果这听起来微不足道,但我已经浏览了很长时间的文档并且找不到解决方案。完整的elm代码如下:
module App where
import Html exposing (..)
import Html.Attributes exposing (attribute, class, style, src, width, height)
import Html.Events exposing (on)
import Json.Decode exposing (object2, (:=), int)
import StartApp.Simple exposing (start)
-- MODEL
type alias Model =
{ width : Int
, height : Int
}
init : Model
init = { width = 800, height = 800 }
-- UPDATE
type Action = NewSize Int Int
update : Action -> Model -> Model
update (NewSize w h) model =
{ model | width = w, height = h }
-- VIEW
view : Signal.Address Action -> Model -> Html
view address model =
div
[ class "container", onResize address ]
[ div
[]
[ img
[ src "http://package.elm-lang.org/assets/elm_logo.svg"
, width model.width
, height model.height
]
[]
]
]
onResize : Signal.Address Action -> Attribute
onResize address =
on "resize" (getWidthHeight view) (\(w, h) -> Signal.message address (NewSize w h))
getWidthHeight : a -> Json.Decode.Decoder ( Int, Int )
getWidthHeight view =
object2 (,) ("innerWidth" := int) ("innerHeight" := int)
-- MAIN
main : Signal Html
main =
start
{ model = init
, update = update
, view = view
}
【问题讨论】:
标签: javascript css elm dom-events