mmdwc
mmdwc

Reputation: 1137

jquery get image offset right on horizontal scrolling

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

Upvotes: 1

Views: 214

Answers (1)

Evik Ghazarian
Evik Ghazarian

Reputation: 1791

add the scroll positioning to your calculation as well:

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

Upvotes: 0

Related Questions