榆木如何制作自定义事件解码器以在鼠标滚轮移动时获得鼠标x / y位置

时间:2019-04-06 23:14:36

标签: dom typeerror elm

在Elm 0.19编程语言的鼠标滚轮移动事件中,我试图获取鼠标的x和y坐标。 我尝试用这个包装。请参阅“高级用法”下的内容: https://package.elm-lang.org/packages/mpizenberg/elm-pointer-events/3.1.0/Html-Events-Extra-Wheel

程序包本身没有描述清楚的示例,因此我在类似的程序包中查找示例。 请参阅此页面中“高级用法”下的示例: https://package.elm-lang.org/packages/mpizenberg/elm-pointer-events/3.1.0/Html-Events-Extra-Mouse

该示例与我所需要的非常相似,但是我也无法使它正常工作。得到完全相同的问题。

这是我的示例代码,适合鼠标滚轮:

module WheelDecoder exposing(..)

import Html exposing (div, text)
import Html.Events.Extra.Wheel as Wheel
import Json.Decode as Decode


type alias WheelEventWithOffsetXY =
  { wheelEvent : Wheel.Event
  , offsetXY : {x: Float, y: Float}
  }

decodeWeelWithOffsetXY : Decode.Decoder WheelEventWithOffsetXY
decodeWeelWithOffsetXY =
  Decode.map2 WheelEventWithOffsetXY
    Wheel.eventDecoder
    offsetXYDecoder

offsetXYDecoder : Decode.Decoder {x: Float, y: Float}
offsetXYDecoder =
  Decode.map2 (\a b -> {x=a,y=b})
    (Decode.field "offsetY" Decode.float)
    (Decode.field "offsetY" Decode.float)

type Msg
  = WheelOffsetXY {x: Float, y: Float}

view = 
  div
    [ (onWheelOffsetXY (\wheelEvent -> WheelOffsetXY (wheelEvent.offsetXY))) ]
    [ (text "mousewheel here") ]


onWheelOffsetXY : (WheelEventWithOffsetXY -> msg) -> Html.Attribute msg
onWheelOffsetXY tag =
  let
    options = { stopPropagation = True, preventDefault = True }
    func = Decode.map tag decodeWeelWithOffsetXY
    attribute = Wheel.onWithOptions options func
  in
    attribute

当我尝试使用“ elm make”进行编译时,出现以下错误:

-- TYPE MISMATCH -------------------------------------- src/Map/WheelDecoder.elm

The 2nd argument to `onWithOptions` is not what I expect:

39|     attribute = Wheel.onWithOptions options func
                                                ^^^^
This `func` value is a:

    Decode.Decoder msg

But `onWithOptions` needs the 2nd argument to be:

    Wheel.Event -> msg

Hint: I always figure out the argument types from left to right. If an argument
is acceptable, I assume it is “correct” and move on. So the problem may actually
be in one of the previous arguments!

此错误消息很有意义,因为我可以看到类型不匹配,但是我不知道如何解决它。

1 个答案:

答案 0 :(得分:2)

似乎<a href="{{url('addappotouser', [$usersss->id, $date->id])}}" value="" class="btn btn-success btn-mini deleteRecord" type="submit">{{$date->Dates}}</a> 应该与Wheel.eventDecoderHtml.Events.on而不是Html.Events.onWithOptions一起使用。它们在0.19中被删除,以Wheel.onWithOptions为名,但是略有不同。以此代替Html.Events.custom似乎可行:

onWheelOffsetXY

PS:onWheelOffsetXY : (WheelEventWithOffsetXY -> msg) -> Html.Attribute msg onWheelOffsetXY tag = let options message = { message = message , stopPropagation = True , preventDefault = True } decoder = decodeWeelWithOffsetXY |> Decode.map tag |> Decode.map options in Html.Events.custom "wheel" decoder 中有一个错字,顺便说一句。我把错字留在原处了。

PPS:另外,您正在查看过时的文档。 Here's the documentation for the latest version