本文共 357 字,大约阅读时间需要 1 分钟。
需求:当页面滚动距离大于某一值时,导航栏固定在页面顶部。向上滑动,当滚动距离小于某一值时,导航回到原位置。
问题:每次向下滑动鼠标,固定导航栏一直闪烁。
怀疑:
scroll
事件里获得,在scroll
事件外获得。因为在滚动鼠标时,一直在实时查询,导致闪烁。display:none
,元素直接不见了,获取的页面高度就需要重新减去这个元素高度,从而导致闪烁。建议页面本来的导航栏和固定导航设置成两个div,当高度大于某一值时,本来的导航栏设置是visibility:hidden
。这样元素依然在页面中占据一定的位置,但是看不见。当需要时,再设置导航栏visibility:visible
。后来实验了一下,第二种导致页面闪烁的可能性更大。
转载地址:http://sdhwi.baihongyu.com/