Could use some small comments here for each case e.g. Check if element is outside of view bounds