Is there an Elm equivalent to jQuery.offset?

If I want how far is my element from the top of the screen in Elm, how do I do it?

In context, I am trying to figure out how to determine if an element is out of view in Elm, and according to that , which is the first component I need to do this.

+3


source to share


2 answers


In Elm 0.19, Browser.Dom.getElement

will return a (task returned) record Element

containing all the required information. Then you can do:

distFromTop : Element -> Int
distFromTop elementInfo =
    elmentInfo.element.y - elementInfo.viewport.y

      



(Note that this distFromTop

may return a negative number if the viewport is currently displaying a portion of the page below your element.)

You can see a complete example of the mechanism Task

you need to do this in a function getImgPosition

in my Elbum project.

+1


source


It looks like you want the y function in the Dom library . Please note that this kind of function is not clean (downward distance can vary), so you need to use a task to work with it. This adds a bit of cognitive overhead for the developer, but of course gives you the other assurances that Elm offers.



0


source







All Articles