Javascript 防止jQuery高级新闻代码中的循环(分叉/修改)
我意识到这个问题是相当利基,应该针对作者或其他 但是,这个插件已经5年多没有更新了,作为一个相对简单的插件,我正在尝试对它进行修改,以便在单击Javascript 防止jQuery高级新闻代码中的循环(分叉/修改),javascript,jquery,news-ticker,Javascript,Jquery,News Ticker,我意识到这个问题是相当利基,应该针对作者或其他 但是,这个插件已经5年多没有更新了,作为一个相对简单的插件,我正在尝试对它进行修改,以便在单击Previous或Next时防止它循环元素 有关工作示例,请参见此处: 如您所见,在1上按下Previous按钮时,它将进入3 类似地,如果它在3上,您单击Next,它将返回到1 正如在小提琴中看到的,我在插件函数中给出了一个长度变量(var listlength=$(“#newsticker li”).length;),它告诉我列表中有多少列表(或li
Previous
或Next
时防止它循环元素
有关工作示例,请参见此处:
如您所见,在1
上按下Previous
按钮时,它将进入3
类似地,如果它在3
上,您单击Next
,它将返回到1
正如在小提琴中看到的,我在插件函数中给出了一个长度变量(var listlength=$(“#newsticker li”).length;
),它告诉我列表中有多少列表(或li
)项
下一次修改必须在以下行内:
if(this.options.nextButton && typeof(this.options.nextButton[0]) !== 'undefined')
this.options.nextButton.click(function(e) {
this.moveNext();
this.resetInterval();
}.bind(this));
if(this.options.prevButton && typeof(this.options.prevButton[0]) !== 'undefined')
this.options.prevButton.click(function(e) {
this.movePrev();
this.resetInterval();
}.bind(this));
但我一直关注这样一个事实:这个插件通过定义一组高度
来工作,并且不修改不可见的li
项
以下是我正在尝试的:
添加var计数=0;
并转到下一步
按钮
this.options.nextButton.click(function(e) {
if (count == 3) { return false; }
else { count++; }
this.options.prevButton.click(function(e) {
if (count == 1) { alert(count); return false; }
else if (count > 0) {
count--;
alert(count);
this.movePrev();
this.resetInterval();
}
}.bind(this));
以及上一个按钮
this.options.prevButton.click(function(e) {
if (count == 0) { return false; }
else { count--; }
它在某种程度上是有效的,但仍然有bug,并且没有给出正确的计数来返回false
如何在上面的
Previous
和Next
脚本函数中创建一个变量,以知道它当前在哪个列表项上(在JSFIDLE示例中-1
、2
或3
)当单击“上一个”按钮时,如果“我的列表项”处于启用状态,则随后防止触发事件;如果显示的列表项位于3
,则防止触发下一个按钮(从而防止如上所述的循环功能).我确实找到了一个解决方案——它看起来相当基本,但至少在我有限的示例中起作用:
js函数中的新变量
并修改Next
按钮
this.options.nextButton.click(function(e) {
if (count == 3) {
alert(count);
return false;
}
else if (count < 3) {
count++;
alert(count);
this.moveNext();
this.resetInterval();
}
}.bind(this));
如果有人发现这种方法存在任何潜在的问题,请随时提出更好的答案(就像我说的,它看起来确实很基本)!谢谢
编辑:
实际上,对于动态变量,这似乎在js代码中起作用:
. . . . .
Plugin.prototype = {
init: function() {
var count = 1;
var listlength = $("li").length;
. . . . .
. . . . .
. . . . .
. . . . .
if (this.options.nextButton && typeof(this.options.nextButton[0]) !== 'undefined')
this.options.nextButton.click(function(e) {
if (count == listlength) {
return false;
} else if (count < listlength) {
count++;
this.moveNext();
this.resetInterval();
}
}.bind(this));
if (this.options.prevButton && typeof(this.options.prevButton[0]) !== 'undefined')
this.options.prevButton.click(function(e) {
if (count == 1) {
return false;
} else if (count > 0) {
count--;
this.movePrev();
this.resetInterval();
}
}.bind(this));
。
Plugin.prototype={
init:function(){
var计数=1;
变量listlength=$(“li”).length;
. . . . .
. . . . .
. . . . .
. . . . .
if(this.options.nextButton&&typeof(this.options.nextButton[0])!=“未定义”)
this.options.nextButton.click(函数(e){
如果(计数==列表长度){
返回false;
}else if(计数<列表长度){
计数++;
this.moveNext();
这是resetInterval();
}
}.约束(这个);
if(this.options.prevButton&&typeof(this.options.prevButton[0])!=“未定义”)
此.options.prevButton.单击(函数(e){
如果(计数=1){
返回false;
}否则,如果(计数>0){
计数--;
this.movePrev();
这是resetInterval();
}
}.约束(这个);
您的问题是,自动取款机没有选项来阻止在设置动画时不断自动删除/追加自动取款机项目:
你可以考虑它有一个选项:<代码> AutoAppEng/<代码>,你可以将它设置为<代码> false ,然后我希望在没有循环的情况下,TIKER在结束时停止(如果你手动控制它).
Hm.没有遇到过。我在下面发布了一个计数函数。据我所知,它消除了自动删除和附加的需要(虽然是的,这也是我最初想要绑定的)。谢谢你的建议。 . . . . .
Plugin.prototype = {
init: function() {
var count = 1;
var listlength = $("li").length;
. . . . .
. . . . .
. . . . .
. . . . .
if (this.options.nextButton && typeof(this.options.nextButton[0]) !== 'undefined')
this.options.nextButton.click(function(e) {
if (count == listlength) {
return false;
} else if (count < listlength) {
count++;
this.moveNext();
this.resetInterval();
}
}.bind(this));
if (this.options.prevButton && typeof(this.options.prevButton[0]) !== 'undefined')
this.options.prevButton.click(function(e) {
if (count == 1) {
return false;
} else if (count > 0) {
count--;
this.movePrev();
this.resetInterval();
}
}.bind(this));