返回顶部报错找不到原因老师帮忙看看

components/index.js
import './backtop.css';
import 'icons/iconfont.css';
//添加移除的名字
const CHANGED_CLASS_NAME = 'backtop-hidden';
// console.log(headerEl);
//初始状态
const INIT_STATE = 'init';
//改变状态
const CHANGED_STATE = 'changed';
class Backtop {
constructor(el, critical_point, scrollContainer, eventEl = scrollContainer) {
this.el = el;
this.critical_point = critical_point;
// 滚动条所在的容器
this.scrollContainer = scrollContainer;
// 监听滚动事件的元素
this.eventEl = eventEl;
this.setState(INIT_STATE);
this.bindEvent();
}
// 设置状态
setState(state) {
this.state = state;
}
// 绑定事件
bindEvent() {
this.eventEl.addEventListener(
'scroll',
() => {
if (this.needChange()) {
this.setState(CHANGED_STATE);
this.change();
} else if (this.needReset()) {
this.setState(INIT_STATE);
this.reset();
}
},
false
);
this.el.addEventListener('click',()=>{
this.scrollTo();
},false);
}
//滚动到指定位置
scrollTo(top=0,left=0){
this.scrollContainer.scrollTO({
top,
left,
//滚动行为 光滑行为
behavior:'smooth'
});
}
reset() {
this.el.classList.add(CHANGED_CLASS_NAME);
}
needReset() {
return (
this.state !== INIT_STATE &&
this.scrollContainer.scrollTop <= this.critical_point
);
}
// 变化
change() {
this.el.classList.remove(CHANGED_CLASS_NAME);
}
// 需要变化
needChange() {
return (
this.state !== CHANGED_STATE &&
this.scrollContainer.scrollTop > this.critical_point
);
}
}
export default Backtop;
index/components/index.js
import Backtop from 'components/backtop';
const scrollContainer=document.getElementById('index-page');
const backtopEl=scrollContainer.querySelector('backtop');
new Backtop(scrollContainer,window.innerHeight,backtopEl);
9
收起
正在回答 回答被采纳积分+1
1回答
相似问题
登录后可查看更多问答,登录/注册


恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星