javascript - 如何在 JavaScript 中重复或重置函数?

标签 javascript

我有 5 个缩略图并排在一起,还有 2 个用于上下滑动它们的箭头。 现在,您可以向上单击两次,向下单击两次,仅此而已,没有任何移动。我的目标是能够返回并多次向上和向下单击。

http://jsfiddle.net/acfS6/

$('#tmbDown').css ('opacity', '.6');

var timesClickedUp = 0;     

$('#tmbUp').bind('click', function (event) {

  $("#tmbHolder").animate({"marginTop": "-=100px"}, "slow");
  timesClickedUp++;

  if (timesClickedUp >= 2) {
    $(this).unbind(event);
    $('#tmbDown').css ('opacity', '1');
    $('#tmbUp').css ('opacity', '.6');    

  }

});


var timesClickedDown = 0;

$('#tmbDown').bind('click', function (event) {

  $("#tmbHolder").animate({"marginTop": "+=100px"}, "slow")
  timesClickedDown++;

  if (timesClickedDown >= 2) {
    $(this).unbind(event);    
    $('#tmbDown').css ('opacity', '.6');
    $('#tmbUp').css ('opacity', '1');
  }



});
​

最佳答案

我认为您可能错过的最重要的事情是,当您单击向下箭头时,您需要减少 timesClickedUp ,反之亦然。

然后,您需要根据相应的“timesClicked”值淡入/显示两个箭头。

我是这样做的:

http://jsfiddle.net/acfS6/2/

var timesClickedUp = 0,
    timesClickedDown = 2;

function updateOpacities() {
    var $tmbUp = $('#tmbUp'),
        $tmbDown = $('#tmbDown');

    timesClickedUp >= 2 ? $tmbUp.css('opacity', '.6') : $tmbUp.css('opacity', '1');
    timesClickedDown >= 2 ? $tmbDown.css('opacity', '.6') : $tmbDown.css('opacity', '1');
}

// Call updateOpacities to initialize the arrows.
updateOpacities();

$('#tmbUp').bind('click', function(event) {

    if (timesClickedUp < 2) {
        $("#tmbHolder").animate({
            "marginTop": "-=100px"
        }, "slow");
        timesClickedUp++;
        timesClickedDown--;
    }

    updateOpacities();
});



$('#tmbDown').bind('click', function(event) {

    if (timesClickedDown < 2) {
        $("#tmbHolder").animate({
            "marginTop": "+=100px"
        }, "slow")
        timesClickedDown++;
        timesClickedUp--;
    }
    updateOpacities();
});​

关于javascript - 如何在 JavaScript 中重复或重置函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13651301/

相关文章:

javascript - 数据表不排序和分页数据

javascript - 在 javascript 中 2 秒内使用函数不超过 1 次

javascript - 如何使用 JavaScriptExecutor 获取浏览器状态栏上显示的文本

javascript - Angular javascript 缩小导致错误 : $injector:modulerr Module Error

javascript - Express - 在中间件函数之间传递数据的更好模式

javascript - Chrome 扩展程序在 Javascript 循环运行时播放加载 Gif

javascript - Bootstrap DateTimePicker 和 jQuery 问题

javascript - 我开始脚本的方式有问题吗?尝试让数组为每个答案输出不同的响应

javascript - 使用 select 过滤数据库

javascript - 浏览器 : Is it possible to to reload font without refreshing page?