6

我是一个完整的新手,正在寻找有关实现 javascript 的说明。我正在尝试用按钮和文本字段替换 YUI 滑块。我正在尝试实现按钮,当按住时,将继续使文本字段增加,最好以越来越快的速度增加。(http://www.blackbird502.com/white.htm)我在头部的java标签中有这个:

function holdit(btn, action, start, speedup) {
var t;

var repeat = function () {
    action();
    t = setTimeout(repeat, start);
    start = start / speedup;
}

btn.mousedown = function() {
    repeat();
}

btn.mouseup = function () {
    clearTimeout(t);
}

/* to use */
holdit(btn, function () { }, 1000, 2); 
/* x..1000ms..x..500ms..x..250ms..x */

我不知道如何在正文中实现按下并按住以下内容:

<form><input type=button value="UP"  class="btn" onClick="javascript:this.form.amount.value++;"><br /><input type=text name=amount value=5 class="text"><br /> <input type=button value="DOWN"  class="btn" onClick="javascript:this.form.amount.value--;" ></form>

是否可以?谢谢。

4

4 回答 4

5

这段代码应该做你正在寻找的一切;它非常松散地基于 tj111 的示例。我试图让它尽可能地可重用,并且它不需要将 JavaScript 与 HTML 混合。

您确实需要向按钮 (btnUPbtnDOWN) 和文本字段 ( amount) 添加 ID。window.onload您可以在语句中更改这些 ID 。

// This function creates a closure and puts a mousedown handler on the element specified in the "button" parameter.
function makeButtonIncrement(button, action, target, initialDelay, multiplier){
    var holdTimer, changeValue, timerIsRunning = false, delay = initialDelay;
    changeValue = function(){
        if(action == "add" && target.value < 1000)
            target.value++;
        else if(action == "subtract" && target.value > 0)
            target.value--;
        holdTimer = setTimeout(changeValue, delay);
        if(delay > 20) delay = delay * multiplier;
        if(!timerIsRunning){
            // When the function is first called, it puts an onmouseup handler on the whole document 
            // that stops the process when the mouse is released. This is important if the user moves
            // the cursor off of the button.
            document.onmouseup = function(){
                clearTimeout(holdTimer);
                document.onmouseup = null;
                timerIsRunning = false;
                delay = initialDelay;
            }
            timerIsRunning = true;
        }
    }
    button.onmousedown = changeValue;
}

//should only be called after the window/DOM has been loaded
window.onload = function() {
    makeButtonIncrement(document.getElementById('btnUP'), "add", document.getElementById('amount'), 500, 0.7);
    makeButtonIncrement(document.getElementById('btnDOWN'), "subtract", document.getElementById('amount'), 500, 0.7);
}
于 2009-03-31T22:58:49.900 回答
3

这有点快速和肮脏,但它应该给你一个开始。基本上,您想设置一些可以使用的初始“常量”来获得所需的行为。增量之间的初始时间为 1000 毫秒,并且在每次迭代中,如果变为该值的 90%(1000、990、891、... 100)并在 100 毫秒处停止变小。您可以调整此因素以获得更快或更慢的加速。其余的我相信非常接近我认为你想要的。看来您只是错过了活动分配。在window.onload您将看到我将onmouseup, 和事件分配给仅使用初始超时onmousedown调用increment()or函数的函数,或停止计数器的函数。 decrement()ClearTimeout()

编辑:我稍微改变了这个以修复错误。现在,如果您将鼠标指针从按钮上移开并释放,它将停止计数器。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title><!-- Insert your title here --></title>
    <script>

      // Fake Constants
      var INITIAL_TIME = 1000;
      var ACCELERATION = .9;
      var MIN_TIME = 100;

      // create global variables to hold DOM objects, and timer
      var up = null,
        down = null,
        count = null,
        timer = null;

      // Increment the counter
      function increment (time) {
        // decrease timeout by our acceleration factor, unless it's at the minimum
        time = (time * ACCELERATION > MIN_TIME) ? (time * ACCELERATION) : MIN_TIME;
        count.value ++ ;
        // set the timeout for the next round, and pass in the new smaller timeout
        timer = setTimeout(
                  function () {
                    increment(time);
                  }, time);
      }
      // Same as increment only subtracts one instead of adding.
      // -- could easily make one function and pass an pos/neg factor instead
      function decrement (time) {
        time = time * ACCELERATION > MIN_TIME ? (time * ACCELERATION) : MIN_TIME;
        count.value --;
        timer = setTimeout(
                  function () {
                    decrement(time);
                  }, time);
     }

     // Initialize the page after all the forms load
     window.onload = function () {
       // initialization function

       // assign DOM objects to our vars for ease of use.
       up = document.getElementById('up_btn');
       down = document.getElementById('dwn_btn');
       count = document.getElementById('count');

       // create event handlers for mouse up and down
       up.onmousedown = function () {
         increment(INITIAL_TIME);
       }
        down.onmousedown = function () {
         decrement(INITIAL_TIME);
       }

       document.onmouseup = function () {
         clearTimeout(timer);
       }

     }

  </script>
</head>
<body>
  <!-- Insert your content here -->

  <form name="the_form">
    <input type="button" value="Up" id="up_btn" /><br />
    <input type="button" value="Down" id="dwn_btn" /></br>

    <br />
    Count: 
    <input type="text" value="0" id="count" />

  </form> 

</body>
</html>
于 2009-03-31T22:59:34.440 回答
0

最简单的方法是为每个按钮添加一个 ID,然后使用它们来检索元素并添加事件。

//should only be called after the window/DOM has been loaded
window.onload = function() {
  //the buttons
  var btnUP = document.getElementById('btnUP');
  var btnDOWN = document.getElementById('btnDOWN');

  //the amount
  var amount = document.getElementById('amount');

  //actions to occur onclick
  var upClick = function() {
    amount.value++;
  }
  var downClick = function() {
    amount.value--;
  }

  //assign the actions here
  holdit(btnUP, upClick, 1000, 2);
  holdit(btnDOWN, downClick, 1000, 2); 

}


<form>
  <input type=button value="UP"  class="btn" id='btnUP'>
  <br />
  <input type=text name=amount value=5 class="text" id='amount'>
  <br /> 
  <input type=button value="DOWN"  class="btn" id='btnDOWN'>
</form>
于 2009-03-31T17:46:45.347 回答
0

一个不容忽视的方面是您正在挂钩 onclick 事件 - 这发生在完整的单击(鼠标键向下和向上键)上。听起来您想听另一个不同的事件,http://www.w3schools.com/jsref/jsref_onmousedown.asp'>onMouseDown。我认为,如果您要实施其他一些基于计时器的解决方案,那么您已经获得了您所要求的功能。

祝你好运!

于 2009-04-01T02:53:36.547 回答