我使用了 2 个 jQuery 插件,一个称为 Cycle,用于在鼠标悬停时循环浏览图像(当鼠标在电视之外时停止循环),另一个称为 Cursometer,用于跟踪鼠标速度。我想将这两个插件集成在一起,以便循环的速度取决于鼠标的速度。但是,当我将 Cycle API 调用的部分添加到 Cursometer 的速度属性中时,Cycle 函数停止工作。我对 JQuery 很陌生,这与范围界定或立即调用有什么关系吗?当我将 Cycle 插件部分代码包含在其他插件调用(Cursometer)中时,为什么它不能工作?
Cycle 如何工作的示例:http://jsfiddle.net/sealife24/rwmLf63r/2/
JSfiddle 在这里: http://jsfiddle.net/sealife24/wsvoyue5/
$('.slideshow-block').cursometer({
onUpdateSpeed: function(mouseSpeed) {
// mouseSpeed is the current cursor speed
var currSpeed = parseInt(mouseSpeed);
$speedometer.text(mouseSpeed);
/* cycle plugin begin */
$('.slides').cycle({
fx: 'none',
speed: 100, // want to pass currSpeed here, but it didn't work, even doesn't work with a number here
timeout: 70
}).cycle("pause");
$('.slideshow-block').hover(function(){
$(this).find('.slides').addClass('active').cycle('resume');
}, function(){
$(this).find('.slides').removeClass('active').cycle('pause');
});
/* cycle plugin end */
}
/* if move the cycle plugin part here, cycle will work, but there is no way I could dynamically get the mouseSpeed from the Cursometer plugin */
});
最佳答案
看来我们无法为此插件动态设置速度或超时。我尝试过,这就是我能实现的目标。
var $speedometer = $('#speedometer');
$('.slideshow-block').cursometer({
onUpdateSpeed: function(mouseSpeed) {
var currSpeed = parseInt(mouseSpeed*100);
$speedometer.text(currSpeed);
var slide= $(this).find('.slides');
var opts = slide.data("cycle.opts");
opts.timeout = currSpeed==0?(0.1):currSpeed;
slide.cycle("resume");
/* cycle plugin begin */
/* cycle plugin end */
}
});
$('.slides').cycle({
fx: 'none',
// speed: 100, // want to pass currSpeed here, but it didn't work
timeout: 70
}).cycle("pause");;
$('.slideshow-block').hover(function(){
$(this).find('.slides').addClass('active').cycle('resume');
}, function(){
$(this).find('.slides').removeClass('active').cycle('pause');
});
关于javascript - 在另一个插件中调用 jQuery 插件会停止工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25982873/