我创建了一个相对较小的动态横幅旋转脚本,底部带有图标,以使特定横幅成为焦点。在横幅上触发 mouseenter 会暂停显示,但有时当我从横幅上移出鼠标时,某些横幅的延迟会缩短。我什至可以理解它是否只发生过一次,但是每次横幅在轮换中返回时,延迟都会设置为较短的时间,并且通常缩短发生在横幅列表中的另一个位置,如好。有时,这可以通过一组尚未确定的行动来纠正。我开始怀疑我的逻辑在某个地方捕获了中间的循环,因此进程分支出来,运行两个循环,这似乎加快了 showNextBanner 函数的调用。不知道如何解决这个问题。一世'
我包括了我认为是下面代码的相关部分。
var firstRun = true;
var play = true;
var current = 0;
var banners = $$( '.banner' );
banners.invoke( 'hide' );
var images = $$( '.image' );
var icons = $$( '.icon' );
//dynamically clones an initial icon to match the number of banners
initIcons();
banners.invoke( 'observe', 'mouseenter', function( field ) {
play = false;
});
banners.invoke( 'observe', 'mouseleave', function( field ) {
if( !play ) {
play = true;
showNextBanner().delay(3);
}
});
icons.invoke( 'observe', 'click', function( field ) {
play = false;
hideBanner( current );
showBanner( findObj( icons, field.findElement()));
});
showNextBanner().delay(3);
function hideBanner( which ) {
icons[ which ].src = blankIconSRC;
banners[ which ].hide();
}
function showBanner( which ) {
icons[ which ].src = selectedIconSRC;
banners[ which ].show();
current = which;
}
// loops the hiding and showing of icons
// (mouseenter sets play to false)
function showNextBanner() {
if( play ) {
if( !firstRun ) {
if( ++current == banners.length ) current = 0;
var previous = 0;
( current == 0 )? previous = banners.length - 1: previous = current - 1;
hideBanner( previous );
} else {
icons[0].src = selectedIconSRC;
firstRun = false;
}
showBanner( current );
showNextBanner.delay(3);
}
}
}());
毕竟,客户想要一个 jQuery 解决方案,这样他就可以拥有通过 scriptaculous 无法获得的滑入效果。所以所有的工作都付诸东流。好消息是我可能只使用 jCarousel 并调整样式表。谢谢您的帮助!