无法读取网页上的属性'addEventListener'为null

时间:2018-12-12 09:04:34

标签: javascript

我尝试通过鼠标事件使图像幻灯片可变形,但在网页上出现此错误。我该如何解决这个问题

下面的我的javascript代码;

var slider = document.querySelector('card-slider-wrapper');
let isDown = false;
let startX;
let scrollLeft;

slider.addEventListener('mousedown', (e) => {
  isDown = true;
  slider.classList.add('active');
  startX = e.pageX - slider.offsetLeft;
  scrollLeft = slider.scrollLeft;
  console.log(startX);
});
slider.addEventListener('mouseleave', () => {
  isDown = false;
  slider.classList.remove('active');
});
slider.addEventListener('mouseup', () => {
  isDown = false;
  slider.classList.add('active');
});
slider.addEventListener('mousemove', (e) => {
  if (!isDown) return;
  e.preventDefault();
  const x = e.pageX - slider.offsetLeft;
  const walk = (x - startX) * 3;
  slider.scrollLeft = scrollLeft - walk;
});

1 个答案:

答案 0 :(得分:5)

缺少类别或ID标识符

应该是

document.querySelector('.card-slider-wrapper')

document.querySelector('#card-slider-wrapper')