Reputation: 29
I'm trying to make JS update a description when an image in a horizontally scrolling container is roughly centered. The issue is that there needs to be room for the image not to be perfectly centered because of no scroll snap. This requires JS to be able tell if 2 offsets/values are equal within, for instance, 40.
if (offsetleft == offsetright) { // plus or minus 40px (PROBLEM)
console.log("roughly centered")
$("#desc").text($(this).data("desc"))
}
Any help would be appreciated.
Upvotes: 0
Views: 46
Reputation: 402
So youre looking for the difference between the 2 points to be less than 40? So...:
if(Math.abs(offsetleft-offsetright)<=40){
//Do stuff
}
Upvotes: 2