2016-11-10 66 views
0

因此,对于目前正在开发中的我的投资组合,我需要使用特定div的偏移坐标。我得到抵消的div已经得到位置固定,并从顶部开始:150px。jQuery无法将偏移量结果与int /字符串进行比较

所以基本上我想CONSOLE.LOG AYY当偏移是150和550之间 的问题是,它总是返回AYY即使超过550 我想这不parseInt函数,并用,但两种方式不工作。

这是我的代码:

$(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'); 
    } 
}); 

这是我的控制台日志 Console log

任何帮助表示赞赏。

+0

删除'150'和'550'附近的引号。另外'offset()。top'返回一个int,所以'parseInt()'是多余的 –

+0

不需要转换为'int',if($('#offset')。offset()。top> 150 && $( '#o​​ffset')。offset()。top <550)' – Satpal

+0

试过,没有引号总是返回nay,反正感谢 – DarthFoot

回答

0

offset().top返回一个int所以parseInt()是多余的,你当你使用$(currentOffset)只需直接currentOffset使用它创建jQuery对象。

使用

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

注意:您没有使用()正常。

+0

谢谢Satpal,这工作。这也将长期帮助我。看来我不能将你的答案标记为答案。有关于此的时间限制吗? *是的,我需要再等6分钟,谢谢* – DarthFoot

相关问题