在具有特定类的所有元素上触发iScroll 4

以这种方式触发脚本iScroll4

$(document).ready(function() { var myScroll; myScroll = new iScroll('wrapper'); }); 

然而,假设有一个带有ID’包装’的元素。 我想在类名为“scrollable”的所有元素上触发它。 我已经在这个脚本的插件上看到了这个。 任何想法如何在这里做?

您可以使用相同的类“可滚动”分配不同的ID,并且……

 $(document).ready(function() { //Created an array for adding n iScroll objects var myScroll = new Array(); $('.scrollable').each(function(){ id = $(this).attr('id'); myScroll.push(new iScroll(id)); }); }); 

我在移动网站上回答过:在iPhone上缺少水平滚动

使用jQuery和iScroll,您可以进行以下操作:

HTML:

  Test   

Header

Lorem ipsum dolor sit amet.

Another header

text text:

div to be scrolled yay
div to be scrolled yay
div to be scrolled yay
div to be scrolled yay
div to be scrolled yay
div to be scrolled yay
div to be scrolled yay

Another header

text text:

div to be scrolled yay
div to be scrolled yay
div to be scrolled yay
div to be scrolled yay
div to be scrolled yay
div to be scrolled yay
div to be scrolled yay

Another header

text text:

div to be scrolled yay
div to be scrolled yay
div to be scrolled yay
div to be scrolled yay
div to be scrolled yay
div to be scrolled yay
div to be scrolled yay

Another header

text text:

div to be scrolled yay
div to be scrolled yay
div to be scrolled yay
div to be scrolled yay
div to be scrolled yay
div to be scrolled yay
div to be scrolled yay


Lorem ipsum dolor sit amet.

CSS:

  

JS / jQuery的:

  

对于我指出的另一个问题,这是测试用例 ( http://portableideas.net/myRepo/trunk/stackoverflow/www/questions_7869572.html )。

这是一个2年的对话,到我写作的时候,只要StackOverflow还活着,这个对话仍会留在这里。

通过搜索引擎我和这个对话有关,因为我有类似的问题。

但是,这些都没有解决我的问题。

在我自己的想法基于弗兰兹的想法,你可以轻松实现这样一个完美而简单的解决方案:

 var contentToScroll; var idNum=1; $('.scrollable').each(function(){ //eg 
  • Contents
  • //
  • Contents
  • etc $(this).attr("id", 'conT'+idNum);//assign ids here var id = $(this).attr('id'); idNum++; contentToScroll=new IScroll('#'+id,{scrollbars: true}); });

    动态地将ID分配给预期的可分割元素,并告诉iScroll5使它们成为可滚动的。

    这可能对某人有帮助