Incrementing value continuously on mouse hold

前端 未结 4 1771
终归单人心
终归单人心 2020-12-20 14:49

I have an HTML5 \'range\' control to which I want to add a plus (+) and minus (-) buttons on either sides.

The fiddle works fine, except that the value increase (or

4条回答
  •  感动是毒
    2020-12-20 15:33

    Edit, Updated

    Try

    var range = $("#range")
    , fx = function(elem, prop) {
      return elem
        .animate({
          value: range.prop(prop)
        }, {
          duration: 3000,
          easing: "linear",
          step: function(now) {
                 elem.val(now + prop === ("max","+"||"min","-") + elem.prop("step"))
          }
        })
    };
    
    $('#plus').mousedown(function(e) {
      fx(range, "max")
    });
    
    $('#minus').mousedown(function minus(e) {
      fx(range, "min")
    });
    
    $(document).mouseup(function(e) {
      range.stop(true, false)
    });
    

    jsfiddle http://jsfiddle.net/bnesu3h9/3/

    var range = $("#range")
    , fx = function(elem, prop) {
      return elem
        .animate({
          value: range.prop(prop)
        }, {
          duration: 3000,
          easing: "linear",
          step: function(now) {
            elem.val(now + prop === ("max","+"||"min","-") + elem.prop("step"))
          }
        })
    };
    
    $('#plus').mousedown(function(e) {
      fx(range, "max")
    });
    
    $('#minus').mousedown(function minus(e) {
      fx(range, "min")
    });
    
    $(document).mouseup(function(e) {
      range.stop(true, false)
    });
    #plus {
      width: 15px;
      height: 15px;
      background-color: red;
      float: left;
    }
    #minus {
      width: 15px;
      height: 15px;
      background-color: blue;
      float: left;
    }
    .range-container {
      float: left;
      overflow: auto;
    }
    
    
    

提交回复
热议问题