最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I change the value of a range input by user scroll? - Stack Overflow

programmeradmin0浏览0评论

I want to change the value of an input type range via mouse scroll.

If the user scrolls up, the value of the range increases, and if the user scrolls down, the value decreases.

I found the wheel event, but I'm not sure how I can use it to acplish the task at hand.

By the way, I have 100vh on the body so I cant manipulate with scroll bar.

I want to change the value of an input type range via mouse scroll.

If the user scrolls up, the value of the range increases, and if the user scrolls down, the value decreases.

I found the wheel event, but I'm not sure how I can use it to acplish the task at hand.

By the way, I have 100vh on the body so I cant manipulate with scroll bar.

Share Improve this question edited May 22, 2021 at 16:53 Spectric 32k6 gold badges28 silver badges51 bronze badges asked May 22, 2021 at 16:35 xhxexhxe 1,4872 gold badges13 silver badges30 bronze badges 1
  • could you share some codes? – Alizadeh118 Commented May 22, 2021 at 16:36
Add a ment  | 

1 Answer 1

Reset to default 10

Detect the wheel event and check the deltaY.

var slider = document.getElementById("range");
slider.addEventListener("wheel", function(e){
  if (e.deltaY < 0){
    slider.valueAsNumber += 1;
  }else{
    slider.value -= 1;
  }
  e.preventDefault();
  e.stopPropagation();
})
body{
  margin:0;
  width:100%;
}
input{
  width:100%;
  margin:0;
}
Hover over the input and scroll up/down

<input type="range" min="1" max="100" value="50" id="range">

发布评论

评论列表(0)

  1. 暂无评论