0

I have a structure something like this:

<div>

    <div id="scrollID" style="height:100px;">
         content here
    </div>

</div>

<script>
document.getElementById("myDIV").addEventListener("touchstart", myFunction);
function myFunction() {
    // want to get my position
}
</script>

I want to get the distance from top, when user start scrolling (actually touching in mobile). My final purose is implementing pull to refresh for child div.

Any idea?

Vahid Najafi
  • 4,654
  • 11
  • 43
  • 88
  • 2
    Have you taken a look at this post? https://stackoverflow.com/questions/3714628/jquery-get-the-location-of-an-element-relative-to-window – ajc2000 Jul 25 '17 at 14:46
  • This [jQuery plugin](https://github.com/sjovanovic/xpull) is designed for mobile pull to refresh of a child div: [xpull](https://github.com/sjovanovic/xpull) – trevorp Jul 25 '17 at 14:49

1 Answers1

2

Try the following.

<div>

    <div id="scrollID" style="height:100px;" onscroll="myFunction();">
         content here
    </div>

</div>

<script>
document.getElementById("myDIV").addEventListener("touchstart", myFunction);
function myFunction() {
    document.getElementById("scrollID").scrollTop;
    // want to get my position
}
</script>
Lime
  • 13,400
  • 11
  • 56
  • 88
  • Thanks. How can I get position from bottom? `document.getElementById("scrollID").scrollbottom` gives me undefined. – Vahid Najafi Jul 26 '17 at 07:08
  • 1
    @vahidnajafi something like this I believe should work `document.getElementById("scrollId").scrollHeight - document.getElementById("scrollID").scrollTop` you might to also subtract the box height this should really be another question. – Lime Jul 26 '17 at 16:37