Determine click position on progress bar?

前端 未结 8 1860
傲寒
傲寒 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:31

    I found a simple solution with pure javascript.

    using getBoundingClientRect() there are all properties to calculate the percentage.

    document.getElementById('progressBar').addEventListener('click', function (e) {
        var bounds = this.getBoundingClientRect();
        var max = bounds.width //Get width element
        var pos = e.pageX - bounds.left; //Position cursor
        var dual = Math.round(pos / max * 100); // Round %
    
        console.log('percentage:', dual);
    });
    

提交回复
热议问题