Determine click position on progress bar?

前端 未结 8 1862
傲寒
傲寒 2021-02-08 19:38

Is it possible to determine the value/position of a user\'s click on a progress bar using plain javascript?

Currently, I can detect a click on the element but can only g

8条回答
  •  日久生厌
    2021-02-08 20:17

    If you want the value clicked for a generic max value (a value between 0 and 1 in this case), you just need some simple math.

    document.getElementById('progressBar').addEventListener('click', function (e) {
    
        var value_clicked = e.offsetX * this.max / this.offsetWidth;
        alert(value_clicked);
    
    });
    

    Fiddle

提交回复
热议问题