在js中改变css样式,样式名字都要改成驼峰形的,比如:z-index改成zIndex
- 将导航栏设置滚动事件
window.addEventListener('scroll', function () {
const n = document.documentElement.scrollTop
// indextop.style.position = n > goodsTop ? 'fixed' : 'relative'
// indextop.style.backgrund = n > goodsTop ? 'fixed' : 'relative'
if (n > goodsTop) {
indextop.style.position = 'fixed'
indextop.style.top = '0'
indextop.style.width = '100%'
indextop.style.background = '#fff';
indextop.style.zIndex = '999'
} else {
indextop.style.position = 'relative'
}
})