Get the offset position of an HTML5 range slider handle

后端 未结 4 972
一生所求
一生所求 2021-01-06 06:51

Is there any way to get the pixel position of the slider handle of an HTML5 range input ?



        
4条回答
  •  一整个雨季
    2021-01-06 07:09

    Here is an alternative solution, basically get the mouse position and use that instead of the slider position. (some tweaking required, possibly)

    Fiddle here: https://jsfiddle.net/brightertools/qdhp2g17/5/

    CSS:

    .sliderHolder{
      margin: 0;
      padding: 0;
      position: relative;
      border: 1px solid silver;
      #slider{
        width: 100%;
      }
      .tt{
        position: absolute;
        border: 1px solid grey;
        top: 20px;
        margin: 5px;
      }
    }
    

    HTML:

    JS:

    var dragging = false;
    jQuery('#slider').on('input',function(){
      dragging = true;
    });
    jQuery('#slider').on('mouseup',function(){
      dragging = false;
    });
    jQuery('#slider').on('mousemove',function(e){
    if (dragging == false)
    {
        return;
    }
    jQuery('.tt').remove();
    var mouseX = e.clientX - 14;
      jQuery('.tt').remove();
      var slider = jQuery('#slider');
        var sliderValue = jQuery('#slider').val();
       jQuery('.sliderHolder').append('
    ' + sliderValue + '
    '); jQuery('.tt').css({'left':mouseX + 'px'}); })

提交回复
热议问题