为什么这个简单的if / else语句不起作用?

时间:2015-04-23 16:01:51

标签: jquery coffeescript

我试图弄清楚为什么这不起作用......

我有两个不同的元素,一个位于页面顶部,另一个位于页面的下半部分。

我添加了一堆控制台日志,以找出应用错误类的原因(始终'顶部'永不底部)。

案例1日志:

.top
half window: 314  top position: 172

案例2日志:

.top
half window: 314  top position: 389

在第二种情况下,类应该是 .bottom,但由于某种原因,这两个数字的简单比较不起作用,我不知道为什么......

我的代码:

enable_dropdown = (el) ->
  el.closest('.shareable').addClass('active')

  windowheight = $(document).height() / 2
  position = el.offset()
  if position >= windowheight
    el.find('.panel').addClass 'bottom'
    console.log '.bottom'
  else
    el.find('.panel').addClass 'top'
    console.log '.top'
  console.log 'half window: ' + windowheight + '  top position: ' + position.top

1 个答案:

答案 0 :(得分:2)

您必须将windowheightposition.top进行比较,而不是position(例如console.log()声明)。

相关问题