Prevent mousedown event on input range html element and still let user drag slider

后端 未结 5 1530
情书的邮戳
情书的邮戳 2021-02-20 02:15

So is there a way to disable mousedown and / or click event on input range element but at the same time let user drag slider around and still fire on change event?

Disabl

5条回答
  •  攒了一身酷
    2021-02-20 03:01

    Here is my try:

    //First block control click + move event
    var current_value = $("#slider").val();
    $("#slider").on('mousedown', function(event) {
        $("#slider").on("mousemove", function (e) {
        if ($("#slider").val() == parseInt(current_value)+1
        || $("#slider").val() == parseInt(current_value)-1) {
          current_value = $(this).val();
        } else {
          $("#slider").val(current_value);
        }
        })
    });
    
    //Second block control click on line
    $("#slider").on("mouseup", function() {
          if ($("#slider").val() == parseInt(current_value)+1
        || $("#slider").val() == parseInt(current_value)-1) {
          current_value = $(this).val();
        } else {
          $("#slider").val(current_value);
        }
    })
    

    NB : if you move to fast the slider, it will jump step, and then back to initial position.

    JSFiddle : https://jsfiddle.net/xpvt214o/674502/

提交回复
热议问题