jQuery滑块“更改"事件:如何确定谁调用了它? [英] jQuery slider “change” event: How do I determine who called it?

查看:116
本文介绍了jQuery滑块“更改"事件:如何确定谁调用了它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个滑块,用作音乐播放器中的时间线.最小值是0,最大值是歌曲长度(以秒为单位).每秒(我使用计时器执行此操作),滑块移动,并且该值设置为当前时间.此代码行如下所示:

I got a slider that is used as a time-line in my music player. The min value is 0 and the max vlaue is the song length (in seconds). Each second (I do this with a timer), the slider moves and the value is set to the current time. This code line looks like that:

$("#sliderTime").slider("option", "value", document.sound.controls.currentPosition);

用户可以通过单击功能"play(startPlayFromHere)"来滑动/单击滑块并跳到歌曲中的另一点.看起来像这样:

The user is able to slide/click the slider and jump to another point in the song, this is by firing the function 'play(startPlayFromHere)'. It looks like that:

$("#sliderTime").slider({
   ...
  change: function (event, ui) { play(ui.value) },
});

问题在于计时器中的代码行和用户都在调用滑块的相同更改"事件,并且用户无法移动滑块.

The problem is that both the code line in the timer and the user are calling the same 'change' event of the the slider, and the user can't move the slider.

所以我的问题是如何确定用户是否调用了更改事件(这就是计时器)?

我希望已经足够清楚了, 谢谢!

I hope it's clear enough, Thanks!

推荐答案

您可以通过在变更处理程序中测试event.originalEvent来确定是手动还是以编程方式发生变更事件.

You can determine whether a change event arose manually or programmatically by testing event.originalEvent in the change handler.

$('#slider').slider({
    change: function(event, ui) {
        if (event.originalEvent) {
            //manual change
            play(ui.value);
        }
        else {
            //programmatic change
        }
    }
});

请参见小提琴.

这篇关于jQuery滑块“更改"事件:如何确定谁调用了它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆