Best way to get currently scrolled to position

Best way to get currently scrolled to position

jLinuxjLinux Posts: 981Questions: 73Answers: 75
edited November 2015 in Scroller

Im trying to find the best way to get the exact position that the viewer has scrolled to, using the scroller extension.

Heres a JSBin of what I've tried thus far.

I tried to use the last rowIndex that was created by createdRow, but that obviously gets the very bottom row, not the very top row.

I thought that maybe one idea that would be possible, is get the last rowIndex of the last created row, then subtract however many rows makes the height of the table. But that doesn't seem to work at all... And even if it did, if they do something like use the code in the vertical page fitting blog (which is really cool btw), then that would obviously throw off the math. And it would obviously be susceptible to some complications if the scroller.boundaryScale or scroller.displayBuffer were utilized

I figure theres gotta be a better way to do that anyways. I didn't see any events related to the scroller extension (which is really weird by the way... I would think this would be an event), or anything in the API to get the current index that was scrolled to.

I thought this would be a relatively simple task, lol

Thanks!

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    I found some DataTables settings that should be relevant

    • oScroller.s.baseRowTop
    • oScroller.s.topRowFloat

    However they both have the same value... what would be the best one to go with?

    And is there anything I can attach to to see if they have been changed? Short of just looping something to see if the value is any different.

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Oh, I guess the draw works...

    $('#example')
        .on( 'draw.dt', function (e, dtSettings) {
            console.debug('Current Row:',dtSettings.oScroller.s.topRowFloat);
            console.debug('Current Row:',dtSettings.oScroller.s.baseRowTop);
        } )
        .DataTable({
            ajax:           "dataSrc.txt",
            deferRender:    true,
            scrollY:        200,
            scrollCollapse: true,
            scroller:       true
        });
    

    So I guess all thats left is... whats the difference between topRowFloat and baseRowTop? :)

  • allanallan Posts: 63,572Questions: 1Answers: 10,482 Site admin

    exact position

    As in the pixel position? $().scrollTop() on the controlling container is probably best.

    Allan

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    I just mean the position that can be used with row().scrollTo()

    Im using it for the keepConditions plugin, including the scroller position as one of the conditions. So I just need to find the best way to get the current and accurate "position" that can be used with row().scrollTo()

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited November 2015

    Yeah I think I got it.

    To get the current position, in the draw, I log

    var hash.scroller = Math.round(_dtSettings.oScroller.s.baseRowTop)
    

    to the hash, then when the page loads, in the init, I grab the value, and basically:

    api.row( parseInt( hash.scroller ) ).scrollTo();
    

    Seems to work fine.

    @allan - You think updating the hash via the draw might cause a little too much overhead? I can't think of a better way to do it. It needs to be the latest current row.

This discussion has been closed.