checkIfStickySupport() {
let test = document.head.style
let perfix = ['', '-webkit-']
for (let index = 0; index < perfix.length; index++) {
const element = perfix[index];
test.position = `${element}sticky`
}
let support = Boolean(test.position)
test.position = ''
return support
}
2 针对不支持sticky定位的环境,fallback至fixed定位
if (!supportSticky) {
if (scrollTop > anchorOffsetTop) {
anchor.style.position = 'fixed'
} else {
anchor.style.position = ''
}
}
let scrollTop = document.body.scrollTop || document.documentElement.scrollTop
let tops = domList.map(item => item.offsetTop - offset)
let pos = 0
if (scrollTop <= tops[0]) {
// do nothing
} else {
for (let index = 0; index < tops.length; index++) {
const top = tops[index];
if (scrollTop > top) {
pos = index
}
}
}
鉴于chrome内核 56开始支持sticky定位,那么首先要解决tabs栏定位问题,此方案可以有兼容方案 sticky定位caniuse 1 检测sticky定位支持,如果支持,则导航栏直接使用sticky定位,检测方式摘自stickybits.js库
2 针对不支持sticky定位的环境,fallback至fixed定位
if (scrollTop <= tops[0]) { // do nothing } else { for (let index = 0; index < tops.length; index++) { const top = tops[index]; if (scrollTop > top) { pos = index } } }
if (tops.length === 3 && pos !== 0) { pos = pos + 1 }
this.activeIndex = pos
4.2 针对不支持的,但支持RAF的,fallback至RAF滚动
4.3 针对以上都不支持的,fallback至一般scrollIntoView方法(不支持options参数)