简体   繁体   中英

JQuery UI Slider with Non-linear steps

I'm following skobaljic's excellent solution to a similar question... ( JQuery UI Slider with Non-linear/Exponential/Logarithmic steps )

var slider = $("#slider-range").slider({
    range: true,
    min: 0,
    max: 100,
    step: 1,
    values: [10, 80],
    slide: function (event, ui) {
        $("#amount").val("RM " + commafy(ui.values[0]) + "  to  RM " + commafy(ui.values[1]));
    }
});
$("#amount").val("RM " + commafy($("#slider-range").slider("values", 0)) +
    "  to  RM " + commafy($("#slider-range").slider("values", 1)));

function commafy(val) {
    /* Total range 0 - 2,500,000 */
    /* 70% from 25,000 to 200,000, what have left (2,325,000) share left (25,000) and right (2,300,000) */
    /* So, final dividing */
    var toPresent = 0;
    if (val < 50) {
        toPresent = (val / 50) * 25000;
    } else {
        toPresent = 250000 + (val - 50) / 50 * 2750000;
    };
    return String(toPresent).split("").reverse().join("")
        .replace(/(.{3}\B)/g, "$1,")
        .split("").reverse().join("");
}

His fiddle is here http://jsfiddle.net/kk7kuy6p/

I need a range selector that goes from 0 - 250000 in the first 50%, then to 3,000,000 in the second 50%. I have adjusted the code and all is well - mostly.

var slider = $("#slider-range").slider({
  range: true,
  min: 0,
  max: 100,
  step: 1,
  values: [64, 78],
  slide: function(event, ui) {
    $("#amount").val("£ " + commafy(ui.values[0]) + "  - £ " + commafy(ui.values[1]));
    $("#newpricemin").val(commafy(ui.values[0]));
    $("#newpricemax").val(commafy(ui.values[1]));

  }
});
$("#amount").val("£ " + commafy($("#slider-range").slider("values", 0)) +
  "  - £ " + commafy($("#slider-range").slider("values", 1)));

function commafy(val) {
  /* Total range 0 - 3,000,000 */
  /* 50% from 0 to 250,000, what have left (2,750,000) */
  /* So, final dividing */
  var toPresent = 0;
  if (val < 50) {
    toPresent = (val / 50) * 250000;
  } else {
    toPresent = 250000 + (val - 50) / 50 * 2750000;
  };
  return String(toPresent).split("").reverse().join("")
    .replace(/(.{3}\B)/g, "$1,")
    .split("").reverse().join("");
}

My fiddle http://jsfiddle.net/10231kq7/6/

I have two minor issues. The scale jumps oddly at 64% and 78% but I have no idea why. Also, I can't get the range to display in anything other than an input box - ideally I want it in a span. It's probably something very obvious, but java is really not my strong point, any help would be appreciated.

You have two issues. First, the strange jumps are a result of a floating point problem . This can be fixed by calling Math.floor on the output numbers.

Secondly, you are using the .val() command in jquery. To edit a span, you need to use .text()

As follows:

var slider = $("#slider-range").slider({
  range: true,
  min: 0,
  max: 100,
  step: 1,
  values: [64, 78],
  slide: function(event, ui) {
    $("#amount").text("£ " + commafy(ui.values[0]) + "  - £ " + commafy(ui.values[1]));
    $("#newpricemin").val(commafy(ui.values[0]));
    $("#newpricemax").val(commafy(ui.values[1]));

  }
});
$("#amount").val("£ " + commafy($("#slider-range").slider("values", 0)) +
  "  - £ " + commafy($("#slider-range").slider("values", 1)));

function commafy(val) {
  /* Total range 0 - 3,000,000 */
  /* 50% from 0 to 250,000, what have left (2,750,000) */
  /* So, final dividing */
  var toPresent = 0;
  if (val < 50) {
    toPresent = (val / 50) * 250000;
  } else {
    toPresent = 250000 + (val - 50) / 50 * 2750000;
  };
  toPresent=Math.floor(toPresent);
  return String(toPresent).split("").reverse().join("")
    .replace(/(.{3}\B)/g, "$1,")
    .split("").reverse().join("");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM