Determine click position on progress bar?

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

    $(document).ready(function() {
    
      $(".media-progress").on("click", function(e) {
    
        var max = $(this).width(); //Get width element
        var pos = e.pageX - $(this).offset().left; //Position cursor
        var dual = Math.round(pos / max * 100); // Round %
    
    
        if (dual > 100) {
          var dual = 100;
        }
    
    
        $(this).val(dual);
        $("#progress-value").text(dual);
    
      });
      
      
      });
    .media-progress {
      /*BG*/
      position: relative;
      width: 75%;
      display: inline-block;
      cursor: pointer;
      height: 14px;
      background: gray;
      border: none;
      vertical-align: middle;
    }
    
    .media-progress::-webkit-progress-bar {
      /*Chrome-Safari BG*/
      background: gray;
      border: none
    }
    
    .media-progress::-webkit-progress-value {
      /*Chrome-Safari value*/
      background: #17BAB3;
      border: none
    }
    
    .media-progress::-moz-progress-bar {
      /*Firefox value*/
      background: #17BAB3;
      border: none
    }
    
    .media-progress::-ms-fill {
      /*IE-MS value*/
      background: #17BAB3;
      border: none
    }
    
    
    

提交回复
热议问题