DarthFoot DarthFoot - 29 days ago 18
jQuery Question

jQuery cannot compare offset result with int/string

So for my portfolio which is currently under development I need to work with the offset coords of a specific div. The div which I am getting the offset of has got position fixed and starts with top:150px.

So basically I want to console.log ayy when the offset is between 150 and 550.
The problem is it always returns ayy even though it surpasses 550.
I tried this without parseInt and with but both ways don't work.

This is my code:

$(window).scroll(function() {
var currentOffset = $('#offset').offset().top;
currentOffset = parseInt(currentOffset);
console.log(currentOffset);
if(($(currentOffset) >= '150') && ($(currentOffset <= '550'))) {
console.log('ayy');
}
else {
console.log('nay');
}
});


This is what my console logs
Console log

Any help is appreciated.

Answer

offset().top returns an int so parseInt() is redundant and you are creating jQuery object when you are using $(currentOffset) just use it directly currentOffset.

Use

$(window).scroll(function() {
    var currentOffset = $('#offset').offset().top; 
    if (currentOffset >= 150 && currentOffset <= 550) {
        console.log('ayy');
    } else {
        console.log('nay');
    }
});

Note: You are not using () properly.