2011-06-18 43 views
4

我有我的標記爲如何處理多個DOM元素與iScroll(同時使用jQTouch)

<div id="home" class="current"> 
    <div class="header">iScroll</div> 
    <div class="wrapper"> 
     <div id="scroller"> 
      <ul id="thelist" class="plastic"><!-- li items --></ul> 
     </div> 
    </div> 
    <div class="footer">Footer</div> 
</div> 
    <!-- Events Details --> 
<div id="events"> 
    <div class="header">iScroll</div> 
    <div class="wrapper"> 
     <div id="scroller"> <!-- stuffsss --></div> 
    </div> 
    <div class="footer">Footer</div> 
</div> 

對於iScroll(http://cubiq.org/iscroll)工作,我需要的#scroller爲ID(按照我使用初始化iScroll的JavaScript代碼。

//for iScroll 
var myScroll = new iScroll('scroller', {desktopCompatibility:true}); 

// Load iScroll when DOM content is ready. 
document.addEventListener('DOMContentLoaded', loaded, false); 

但因爲我不能有相同的ID兩種不同元素(請注意我有在同一ID滾動兩個元素我的標記在上面),一些衝突在那裏,iScroll工作不正常。

我希望能夠通過將id更改爲類來實現標記上的iScroll。我試圖將它們改爲類,看看它是否有效,但我無法做到。

任何人都可以幫助我更改代碼,以便它通過實現類而不是ID?

回答

4

Rob是對的,但你可以像你說的那樣將你的代碼更改爲Scroller類。 然後獨特的包裝內初始化的滾動條是這樣的:

var scroll1, scroll2; 
function loaded() { 
scroll1 = new iScroll('wrapper1'); 
scroll2 = new iScroll('wrapper2'); 
} 
2

我並不十分清楚你想要達到的目標,但是如果你想讓頁面的兩個部分滾動,我建議將ID改爲唯一,並用不同的ID實例化兩個iScrolls。

+0

如上所述,iScroll需要特定的ID才能工作。 – Vasily

0

我相信你已經想通了,但是對於其他用戶提供類似的佈局(多滾動條)仍然在努力,並希望使他們的工作。下面是從其他線程

https://stackoverflow.com/a/7584694/1232232

的答案,但這個工作需要分配的ID給你歸類(DIV容器) 像

<div id="home" class="current"> 
<div class="header">iScroll</div> 
<div id="wrapper-1" class="scrollable"> 
<div class="scroller"> 
<ul class="thelist" class="plastic"><!-- li items --></ul> 
</div> 
</div> 
<div class="footer">Footer</div> 
</div> 
<div id="home2" class="current"> 
<div class="header">iScroll</div> 
<div id="wrapper-1" class="scrollable"> 
<div class="scroller"> 
<ul class="thelist" class="plastic"><!-- li items --></ul> 
</div> 
</div> 
<div class="footer">Footer</div> 
</div> 

請注意:不要分配相同的ID到多個元素,總是使用類來達到這個目的。