Change the comparison mechanism:
visiblePartY = 'top' if viewportOffset.top <= elementOffset.top
visiblePartY = 'bottom' if viewportOffset.bottom >= elementOffset.bottom