How to detect the end of a horizontal scroll in a div?
Asked Answered
P

4

18

I was trying to implement a horizontal scroll inside a div. My question is how can I detect the end of the horizontal scroll?

I tried something like this

$(function() {
var scrollLeftPrev=0;
$('#scrollquestion').scroll( function() {
  var newScrollLeft=$('#scrollquestion').scrollLeft();
  if(scrollLeftPrev===newScrollLeft){
    alert('right end');
  }
  if(newScrollLeft===0){
    alert('left end');
  }
  console.log($('#scrollquestion').width());
  console.log(newScrollLeft);
  scrollLeftPrev=newScrollLeft;
 });
});

left end alert works, since it will become 0 for all the device sizes. For right end, it depends on the device size.

Screen : Horizontal scroll

JS Fiddle : http://jsfiddle.net/arunslb123/trxe4n3u/

Poundfoolish answered 18/8, 2015 at 9:32 Comment(0)
A
30

Use scrollWidth and width along with your leftscrollwidth to get the difference. In your case there is offset of 8 so it will give the difference of 8 it may be because of your padding or margin.

var $elem=$('#scrollquestion');
var newScrollLeft = $elem.scrollLeft(),
    width=$elem.width(),
    scrollWidth=$elem.get(0).scrollWidth;
var offset=8;
if (scrollWidth- newScrollLeft-width==offset) {
    alert('right end');
}

Live Demo

Use the outerWidth() to get the offset including the width like,

var $elem=$('#scrollquestion');
var newScrollLeft = $elem.scrollLeft(),
    width=$elem.outerWidth(),
    scrollWidth=$elem.get(0).scrollWidth;
if (scrollWidth-newScrollLeft==width) {
    alert('right end');
}

Another Demo without using offset

Association answered 18/8, 2015 at 9:55 Comment(3)
can you tell me how offset is calculated as 8 here? EDIT : Thanks. Got it, it's because of padding is set as 4px.Poundfoolish
the offset is the padding left and right but you can do it without the offset as below (posted as an answer)Kamacite
In my case, I had to apply Math.ceil() under width, as it was different from scrollWidth-newScrollLeft by 0.1 px.Varanasi
R
11

Try http://jsfiddle.net/trxe4n3u/3/

$(function() {
    $('#scrollquestion').scroll( function() {
        var $width = $('#scrollquestion').outerWidth()
        var $scrollWidth = $('#scrollquestion')[0].scrollWidth; 
        var $scrollLeft = $('#scrollquestion').scrollLeft();

        if ($scrollWidth - $width === $scrollLeft){
            alert('right end');
        }
        if ($scrollLeft===0){
            alert('left end');
        }
    });
});
Report answered 18/8, 2015 at 9:50 Comment(3)
Tried your code. left end works, but right end is not working.Poundfoolish
@ArunPrakash change scrollLeft with var scrollLeft = Math.round($('#scrollquestion').scrollLeft());Pickings
@ArunPrakash you need to remove the decimal, change that line : if (~~($scrollWidth - $width) === ~~$scrollLeft)Kelt
K
2

Rohan kumar's answer is correct and works fine, but you can do this without calculating the offset manually

var newScrollLeft=$('#scrollquestion').scrollLeft();
      var divWidth = $('#scrollquestion').outerWidth();
      var scrollwidth =$('#scrollquestion').get(0).scrollWidth;
      if(newScrollLeft === scrollwidth - divWidth){
        alert('right end');
      }
Kamacite answered 18/8, 2015 at 10:10 Comment(0)
C
2

For my specific case, dangor's solution with the

if ($scrollWidth - $width === $scrollLeft) {
        alert('right end');
}

did not work for me because calculating ($scrollWidth - $width) created a very small decimal that made the left-side of the comparison a float, and the right-side ($scrollLeft), an integer. Changing it to the code below works perfect for me.

if (parseInt($scrollWidth - $width) === parseInt($scrollLeft)) {
            alert('right end');
}
Chronaxie answered 20/8, 2019 at 16:42 Comment(1)
Correctly answered!Conspicuous

© 2022 - 2024 — McMap. All rights reserved.