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

Why is this simple if/else statement not working?

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

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

我添加了一堆控制台日志来找出为什么应用了错误的class(总是'top',从来没有底部)。

案例 1 日志:

.top
half window: 314  top position: 172

案例 2 日志:

.top
half window: 314  top position: 389

在第二种情况下,class 应该是 .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

您必须将 windowheightposition.top 进行比较,而不是 position(就像在您的 console.log() 语句中那样)。