You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:为进度条添加mouseover事件实现滑块移动的JS代码故障排查

修复鼠标悬停进度条滑块的问题

你的代码存在几处错误,导致鼠标悬停时进度条滑块无法正常响应,以下是问题点和修正方案:

问题点梳理

  • getElementById的参数不需要加#,该方法直接接收纯ID字符串,而非CSS选择器格式
  • 获取元素后直接用变量名操作即可,无需写成document.myProgressElm
  • 样式属性widht是拼写错误,正确名称为width

修正后的代码

const myProgressAnim = document.getElementById('Progress_Status');
myProgressAnim.addEventListener("mouseover", progressBar);

function progressBar(event){
  const myProgressElm = document.getElementById('myprogressBar');
  myProgressElm.style.width = '100px';
  myProgressElm.style.backgroundColor = "red";
  myProgressElm.style.color = 'black';
}

额外提示

如果要实现滑块平滑移动的效果,可将固定宽度值改为百分比(如width: 70%),或结合transform: translateX()属性来调整位置,适配不同布局需求。

内容的提问来源于stack exchange,提问作者HaNdev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 16:15:31