该插件默认是监控 html/body 的滚动,当需要指定参照元素时,可以在配置元素中增加一个 .content属性,然后增加判断条件: if
(_self.config.content) {
//list距离顶部的距离,然后减去
var listTop = $(_self.config.el).offset().top;
currentTop = currentTop - listTop;
console.log(listTop);
$(_self.config.content).scrollTo({
toT: currentTop,
callback: () => {
$('#hhb_prompt').remove();
}
});
}以及
let throttled = _self.throttle(function() {
let currentTop = document.documentElement.scrollTop || document.body.scrollTop;
if ($(_self.config.content)) {
currentTop = $(_self.config.content).scrollTop() + topArr[0];
}
let currentIndex = _self.getArrIndex(topArr, currentTop);
$(_self.config.elNav).find('li').removeClass('active').eq(currentIndex).addClass('active');
}, 300);
配置
init(options) {
//默认配置
this.config = $.extend(true, {}, {
content: '.indexed-content', //选填,滚动条主体,相对于哪一个主体页面滚动,默认为当前页面
el: '.indexed-list', //主列表DOM [选填,默认值:.indexed-list]
elNav: '.indexed-nav', //右侧列表DOM [选填,默认值:.indexed-nav]
datas: [] //自定义的数据 [必填,默认为空数组]
}, options);
this.autoCreate();
}