Forum Moderators: open

Message Too Old, No Replies

scrollTop() relative to position

         

csdude55

12:36 am on Apr 21, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have an element that opens via Ajax to take the place of a generic "select" menu. And if the user has already selected something in advance then, when they click to open it, I want to automatically scroll down to that selected position.

I've been doing it like this, but the position is off by about 15px; it should be at 246, but instead it's at 231:

// assume they pre-selected option_3
<div id="select">
<div id="option_1">Blah</div>
<div id="option_2">Blah</div>
<div id="option_3">Blah</div>
</div>

$('#select').scrollTop($('#option_3').position().top);
</script>


The best way I can think to do this is to find parent container's offset().top, then find the selected item's offset().top... subtract one from the other, and then use scrollTop to drop down to that location. But that's a lot of math for what SHOULD be simple:

<script>
var starter = $('#select').offset().top;
var ender = $('#option_3').offset().top;

var offset = ender - starter;

$('#select').scrollTop(offset);
</script>


Or, of course, I can just add 15 to the position, but that seems like a work-around that might not be right for every browser instead of the "right" way to do it:

$('#select').scrollTop($('#option_3').position().top + 15);


What do you guys think? Any guess as to why position().top is off by 15px?

NickMNS

1:28 am on Apr 21, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This is how I do it in pure JS

document.getElementById("option_3").scrollIntoView();
window.scrollTo(window.scrollX, window.scrollY - 15);


window.ScrollX gets the X position and Y the Y position from which I deduct 15px.

This seems to coincide with your second example.

csdude55

1:45 am on Apr 21, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hmm, so the 15px discrepancy is consistent, then, and not just me... I wonder why? I thought it might be the height of my option_3 element, but that was 21px so it didn't quite match up.

NickMNS

1:55 am on Apr 21, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No I changed that to make it consistent. My page is laid out with a sticky menu at the top of the screen that is 55px tall, and I like to peak the bottom of an that appears above the insertion point so add another 100px this rounds to 150px.

csdude55

2:14 am on Apr 21, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh, I see. I'm stuck on why position() is off 15px from offset(), then... maybe I'll stick with the second one, then, after all

NickMNS

2:31 am on Apr 21, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your 15px likely comes from padding or a margin in your css. If you find the class you can probably set it box-sizing: border-box; which will include border and padding in the size of the div.