<legend id='rP3Ky'><style id='rP3Ky'><dir id='rP3Ky'><q id='rP3Ky'></q></dir></style></legend>
  • <i id='rP3Ky'><tr id='rP3Ky'><dt id='rP3Ky'><q id='rP3Ky'><span id='rP3Ky'><b id='rP3Ky'><form id='rP3Ky'><ins id='rP3Ky'></ins><ul id='rP3Ky'></ul><sub id='rP3Ky'></sub></form><legend id='rP3Ky'></legend><bdo id='rP3Ky'><pre id='rP3Ky'><center id='rP3Ky'></center></pre></bdo></b><th id='rP3Ky'></th></span></q></dt></tr></i><div id='rP3Ky'><tfoot id='rP3Ky'></tfoot><dl id='rP3Ky'><fieldset id='rP3Ky'></fieldset></dl></div>

      • <bdo id='rP3Ky'></bdo><ul id='rP3Ky'></ul>

      <small id='rP3Ky'></small><noframes id='rP3Ky'>

      <tfoot id='rP3Ky'></tfoot>

        JQuery Slider,如何制作“step"尺寸变化

        时间:2024-04-18
          1. <i id='YGm2Y'><tr id='YGm2Y'><dt id='YGm2Y'><q id='YGm2Y'><span id='YGm2Y'><b id='YGm2Y'><form id='YGm2Y'><ins id='YGm2Y'></ins><ul id='YGm2Y'></ul><sub id='YGm2Y'></sub></form><legend id='YGm2Y'></legend><bdo id='YGm2Y'><pre id='YGm2Y'><center id='YGm2Y'></center></pre></bdo></b><th id='YGm2Y'></th></span></q></dt></tr></i><div id='YGm2Y'><tfoot id='YGm2Y'></tfoot><dl id='YGm2Y'><fieldset id='YGm2Y'></fieldset></dl></div>

            <legend id='YGm2Y'><style id='YGm2Y'><dir id='YGm2Y'><q id='YGm2Y'></q></dir></style></legend>
              <bdo id='YGm2Y'></bdo><ul id='YGm2Y'></ul>

                <small id='YGm2Y'></small><noframes id='YGm2Y'>

                  <tfoot id='YGm2Y'></tfoot>
                    <tbody id='YGm2Y'></tbody>
                • 本文介绍了JQuery Slider,如何制作“step"尺寸变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以使用 JQuery Slider(范围滑块/双滑块)来获得非线性(不一致的步长"大小)值?

                  Is it possible to use the JQuery Slider (range slider / dual slider) to have non-linear (non consistent "step" size) values?

                  我想让水平 Slider 看起来像:

                  I want to horizontal Slider to look like:

                  |----|----|----|----|----|--------|--------|-------------------------|--------------------------|...
                  0   500  750  1000  1250 1500     2000     2500                      75000                      100000...
                  

                  例如,我想要下面的 JQuery 代码:

                  For example, I want to have the following JQuery code:

                  var values = [0, 500, 750, 1000, 1250, 1500, 2000, 2500, 75000, 100000, 150000, 200000, 250000, 300000, 350000, 400000, 500000, 1000000];
                  var slider = $("#price-range").slider({
                      orientation: 'horizontal',
                      range: true,
                      min: 0,
                      max: 1000000,
                      values: [0, 1000000],
                      slide: function(event, ui) {
                              var includeLeft = event.keyCode != $.ui.keyCode.RIGHT;
                              var includeRight = event.keyCode != $.ui.keyCode.LEFT;
                              slider.slider('option', 'value', findNearest(includeLeft, includeRight, ui.value));
                              $("#price-amount").html('$' + ui.values[0] + ' - $' + ui.values[1]);
                              return false;
                      },
                      change: function(event, ui) { 
                          getHomeListings();
                      }
                  });
                  function findNearest(includeLeft, includeRight, value) {
                      var nearest = null;
                      var diff = null;
                      for (var i = 0; i < values.length; i++) {
                              if ((includeLeft && values[i] <= value) || (includeRight && values[i] >= value)) {
                                      var newDiff = Math.abs(value - values[i]);
                                      if (diff == null || newDiff < diff) {
                                              nearest = values[i];
                                              diff = newDiff;
                                      }
                              }
                      }
                      return nearest;
                  }
                  

                  上面的代码不完全有效,但对齐网格功能不起作用.

                  The code above is not exactly working but the snap to grid functionality doesn't work.

                  推荐答案

                  不确定您是否希望滑块比例与您的值成比例*,但如果是这样,我为其他提出相同问题的人提供了一个解决方案问题.你可以在这里找到我的解决方案.基本上,我使用 slide 事件,当您移动滑块以模拟步进时触发该事件,但基于定义步骤的自定义数组.这样,它只允许您步进"到您的预定义值,即使它们没有均匀分布.

                  Not sure if you want the slider scale to be in proportion to your values* but if so, I provided a solution to this for someone else who asked the same question. You can find my solution here. Basically I make use of the slide event that gets triggered when you move the slider to mimic the stepping, but based off a custom array defining the steps. This way it only allows you to "step" to your predefined values, even if they're not evenly spread.

                  *换句话说,如果您希望滑块看起来像这样:

                  *In other words if you want your slider to look like this:

                  |----|----|----|----|----|----|----|
                  0   10   20  100  1000 2000 10000 20000
                  

                  然后在此处使用其他解决方案之一,但如果您希望滑块看起来像这样(图表不按比例):

                  then go with one of the other solutions here, but if you want your slider to look like this (diagram not to scale):

                  |--|--|-------|-----------|-----------|--------------------|--------------------|
                  0 10 20     100         1000        2000               10000                20000
                  

                  那么我链接到的解决方案可能更符合您的需求.

                  Then the solution I linked to may be more what you're after.

                  好的,这个版本的脚本应该可以使用双滑块:

                  Ok, this version of the script should work with dual sliders:

                  $(function() {
                      var values = [0, 500, 750, 1000, 1250, 1500, 2000, 2500, 75000, 100000, 150000, 200000, 250000, 300000, 350000, 400000, 500000, 1000000];
                      var slider = $("#price-range").slider({
                          orientation: 'horizontal',
                          range: true,
                          min: 0,
                          max: 1000000,
                          values: [0, 1000000],
                          slide: function(event, ui) {
                              var includeLeft = event.keyCode != $.ui.keyCode.RIGHT;
                              var includeRight = event.keyCode != $.ui.keyCode.LEFT;
                              var value = findNearest(includeLeft, includeRight, ui.value);
                              if (ui.value == ui.values[0]) {
                                  slider.slider('values', 0, value);
                              }
                              else {
                                  slider.slider('values', 1, value);
                              }
                              $("#price-amount").html('$' + slider.slider('values', 0) + ' - $' + slider.slider('values', 1));
                              return false;
                          },
                          change: function(event, ui) { 
                              getHomeListings();
                          }
                      });
                      function findNearest(includeLeft, includeRight, value) {
                          var nearest = null;
                          var diff = null;
                          for (var i = 0; i < values.length; i++) {
                              if ((includeLeft && values[i] <= value) || (includeRight && values[i] >= value)) {
                                  var newDiff = Math.abs(value - values[i]);
                                  if (diff == null || newDiff < diff) {
                                      nearest = values[i];
                                      diff = newDiff;
                                  }
                              }
                          }
                          return nearest;
                      }
                  });
                  

                  请注意,它在最左端看起来有点滑稽,因为与右手端相比,跳跃非常接近,但如果您使用键盘箭头移动滑块,您可以看到它的步进.解决这个问题的唯一方法是改变你的规模,使其不那么急剧地呈指数级增长.

                  Note that it looks a little funny down the far left end, since the jumps are so close together compared to the right hand end, but you can see its stepping as desired if you use your keyboard arrows to move the slider. Only way to get around that is to change your scale to not be quite so drastically exponential.

                  编辑 2:好的,如果使用真值时间距过于夸张,可以为滑块使用一组假值.然后查找与您需要使用实际值时对应的实际值(以与此处建议的其他解决方案类似的方式).代码如下:

                  Edit 2: Ok, if the spacing is too exaggerated when you use the true values, you could use a set of fake values for the slider & then look up the real value this corresponds to when you need to use the real value (in a similar way to what the other solutions here suggested). Here's the code:

                  $(function() {
                      var trueValues = [0, 500, 750, 1000, 1250, 1500, 2000, 2500, 75000, 100000, 150000, 200000, 250000, 300000, 350000, 400000, 500000, 1000000];
                      var values =     [0,   1,   2,    3,    4,    5,    6,    7,    10,     15,     20,     25,     30,     40,     50,     60,     75,     100];
                      var slider = $("#price-range").slider({
                          orientation: 'horizontal',
                          range: true,
                          min: 0,
                          max: 100,
                          values: [0, 100],
                          slide: function(event, ui) {
                              var includeLeft = event.keyCode != $.ui.keyCode.RIGHT;
                              var includeRight = event.keyCode != $.ui.keyCode.LEFT;
                              var value = findNearest(includeLeft, includeRight, ui.value);
                              if (ui.value == ui.values[0]) {
                                  slider.slider('values', 0, value);
                              }
                              else {
                                  slider.slider('values', 1, value);
                              }
                              $("#price-amount").html('$' + getRealValue(slider.slider('values', 0)) + ' - $' + getRealValue(slider.slider('values', 1)));
                              return false;
                          },
                          change: function(event, ui) { 
                              getHomeListings();
                          }
                      });
                      function findNearest(includeLeft, includeRight, value) {
                          var nearest = null;
                          var diff = null;
                          for (var i = 0; i < values.length; i++) {
                              if ((includeLeft && values[i] <= value) || (includeRight && values[i] >= value)) {
                                  var newDiff = Math.abs(value - values[i]);
                                  if (diff == null || newDiff < diff) {
                                      nearest = values[i];
                                      diff = newDiff;
                                  }
                              }
                          }
                          return nearest;
                      }
                      function getRealValue(sliderValue) {
                          for (var i = 0; i < values.length; i++) {
                              if (values[i] >= sliderValue) {
                                  return trueValues[i];
                              }
                          }
                          return 0;
                      }
                  });
                  

                  您可以摆弄 values 数组(代表滑块停止点)中的数字,直到按您想要的方式将它们隔开.这样,从用户的角度来看,您可以让它感觉就像它与值成比例地滑动,但又不会被夸大.显然,如果您的真实值是动态创建的,您可能需要提出一种算法来生成滑块值,而不是静态定义它们...

                  You can fiddle with the numbers in the values array (which represent the slider stop points) until you get them spaced out how you want. This way you can make it feel, from the user's perspective, like it's sliding proportionally to the values, but without being as exaggerated. Obviously if your true values are dynamically created, you may need to come up with an algorithm to generate the slider values instead of statically defining them...

                  这篇关于JQuery Slider,如何制作“step"尺寸变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么单态和多态在 JavaScript 中很重要? 下一篇:使用 setInterval 时,如果我在 Chrome 中切换标签并返回,滑块会疯狂追赶

                  相关文章

                    • <bdo id='ki4i4'></bdo><ul id='ki4i4'></ul>
                    1. <tfoot id='ki4i4'></tfoot>
                      <i id='ki4i4'><tr id='ki4i4'><dt id='ki4i4'><q id='ki4i4'><span id='ki4i4'><b id='ki4i4'><form id='ki4i4'><ins id='ki4i4'></ins><ul id='ki4i4'></ul><sub id='ki4i4'></sub></form><legend id='ki4i4'></legend><bdo id='ki4i4'><pre id='ki4i4'><center id='ki4i4'></center></pre></bdo></b><th id='ki4i4'></th></span></q></dt></tr></i><div id='ki4i4'><tfoot id='ki4i4'></tfoot><dl id='ki4i4'><fieldset id='ki4i4'></fieldset></dl></div>

                      <legend id='ki4i4'><style id='ki4i4'><dir id='ki4i4'><q id='ki4i4'></q></dir></style></legend>

                      <small id='ki4i4'></small><noframes id='ki4i4'>