简体   繁体   English

如何编写使jQuery UI滑块自动滑动的脚本?

[英]How can I write a script that makes a jQuery UI slider automatically slide?

So I have some jQuery UI sliders on my webpage with the code 所以我的网页上有一些带有代码的jQuery UI滑块

var sliders = $("#sliders .slider");
var availableTotal = 100;


sliders.each(function () {
    var init_value = parseInt($(this).text());
    $(this).siblings('.value').text(init_value);
    $(this).empty().slider({      
        value: init_value,
        min: 0,
        max: availableTotal,
        range: "max",
        step: 5,
        animate: 0,
        slide: function (event, ui) {
             // ... 
        } 

    });
});

hooked up to them. 迷上了他们。 I'm trying to write a testing script that makes them automatically slide right or left, like 我正在尝试编写一个测试脚本,使它们自动向右或向左滑动,例如

for ( var k = 0; ; ++k )
{
    setTimeout(50,function(){ 
        $(sliders[k % sliders.length]).slide(...); 
    });
}

but I'm not sure what goes in the (...) . 但是我不确定(...) What exactly is event and ui and how can I pass in randomly generated valid values for my automated test? eventui到底是什么,如何为我的自动化测试传递随机生成的有效值?

If you want to test your sliders programmatically, you need to set a loop and adjust their values. 如果要以编程方式测试滑块,则需要设置一个循环并调整其值。 I setup the following Fiddle as an example: https://jsfiddle.net/Twisty/azybrcn6/ 我以以下小提琴为例: https : //jsfiddle.net/Twisty/azybrcn6/

Something is weird with my timer function... still working on it, but the idea is sound. 我的计时器功能有些奇怪...仍然可以使用,但是这个想法很合理。

JQuery jQuery查询

var timer;
var inc = "pos";

function slideInc(obj, n) {
  // Increment SLider Object by n
  var v = obj.slider("option", "value");
  obj.slider("option", "value", v + n);
  console.log("Slider was ", v, " set to ", obj.slider("option", "value"));
}

function slideDec(obj, n) {
  // Decrement Slider Object by n
  var v = obj.slider("option", "value");
  obj.slider("option", "value", v - n);
  console.log("Slider was ", v, " set to ", obj.slider("option", "value"));
}

$(function() {
  var k = 0;
  $("#slider").slider({
    min: 0,
    max: 100,
    range: "max",
    step: 5,
    animate: 0,
    value: 0,
    slide: function(e, ui) {
      $("#value").html(ui.value);
    }
  });
  $("#test, #end").button();
  $("#test").click(function() {
    console.log("Starting.");
    var $t = $("#slider");
    var min = $t.slider("option", "min");
    var max = $t.slider("option", "max");
    var st = $t.slider("option", "step");

    timer = setInterval(function() {
      console.log("Inc is ", inc);
      if (inc == "pos") {
        slideInc($t, st);
        if ($t.slider("option", "value") == max) {
          inc = "neg";
        }
      } else {
        slideDec($t, st);
        if ($t.slider("option", "value") == min) {
          inc = "pos";
        }
      }
      $("#value").html($t.slider("option", "value"));
      console.log("Next.");
    }, 1000);
  });
  $("#end").click(function() {
    console.log("Ending, value: " + $("#slider").slider("option", "value"));
    clearInterval(timer);
  });
});

When we start the timer, I will increment k by step until it reaches the max and then toggle to decrement. 当我们开始计时,我将递增k通过step ,直到它达到max ,然后切换到递减。 Each time the timer executes the function, it will check to see if we increment or decrement k , adjust the slider, and start over. 每次计时器执行该功能时,它都会检查是否增加k或减小k ,调整滑块并重新开始。

I cannot yet call this a working example. 我还不能称其为工作示例。 It's firing once and then not repeating. 它触发一次,然后不再重复。 Will keep working on it and update once I have it working as you described. 如我所述,它将继续工作并更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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