Because headerHeight in your js should take offsetHeight of nav and when the fixed class is added you need to adjust nav width accordingly too.