jquery get image offset right on horizontal scrolling

221 Views Asked by At

I'm using on my website jQuery Mousewheel to have an horizontal scrolling.

I'm trying to get the offset right of an image when scrolling.

when using $(document).ready it works, but when I try to use $(window).scroll I dont have the right offset and the offset is not updated when scrolling.

any ideas ?

here is my code :

$(document).ready(function() {

  $('html, body, *').mousewheel(function(e, delta) {

     this.scrollLeft -= (delta);
     e.preventDefault();
  });

});

$(window).scroll(function() {

  var $image = $(".image_test");
  var $rt = ($("body").width() - ($image.offset().left + $image.outerWidth()));

  console.log($rt);

});

here is a Jsfiddle :

https://jsfiddle.net/deapzuc4/

thanks

1

There are 1 best solutions below

3
On BEST ANSWER

add the scroll positioning to your calculation as well:

var $rt = ($("body").width() - ($image.offset().left + $(window).scrollLeft() + $image.outerWidth()));