【发布时间】:2014-10-25 22:26:00
【问题描述】:
在我找到一种方法让 iScroll 在动态上工作后,添加滚动容器和/或更改滚动容器内的内容长度后,我遇到了附加功能的问题,例如
myScroll.scrollToElement(document.querySelector('#scroller li:nth-child(10)'))
我没有使用固定 ID,而是使用动态添加的类名,就像在原始示例中一样,以 <body onload="loaded()"> 开头,就像这样
function loaded () {
var myScroll;
var iscroller = $('.iscroller');
iscroller.each(function(index){
myScroll = new IScroll('.iscroller'+index, { mouseWheel: true, click: true });
})
}
到目前为止,它对于滚动功能本身来说效果很好。每个滚动容器,在 html/php 代码之前有一个 class="iscroller",通过它的索引得到另一个唯一的类名 iscroller0 ...1 ...2 等等。
当内容被动态添加时,它仍然可以正常工作,比如更多<li>,到容器中,之后调用loaded()。滚动仍然按预期工作。
但是当我想调用像
myScroll.scrollToElement(document.querySelector('#scroller li:nth-child(10)'))
它仅适用于最后一个容器,这对我来说似乎合乎逻辑。
所以我尝试使用动态创建的对象,而不是像这样使用myScroll:
function loaded () {
var iscroller = $('.iscroller');
iscroller.each(function(index){
eval('var myScroll'+index+' = new IScroll(\'.iscroller'+index+'\', { mouseWheel: true, click: true });');
})
,}
在所有容器中像往常一样滚动仍然有效,但现在我希望能够调用
myScroll2.scrollToElement(document.querySelector('.iscroller2 li:nth-child(10)'))
但我收到控制台错误ReferenceError: myScroll2 is not defined。
我能做什么?
【问题讨论】:
标签: javascript jquery iscroll