2012-07-25 43 views
0

我在我的应用程序中有一个jQuery Mobile轮播,我需要在转盘旋转时立即填充数据,这是完成的。现在新的实现是,只要用户在传送带上停留时间> = 3秒,就应该只更新下拉列表。那么我想如何捕捉这段暂停时间呢?我使用的代码iscroll.js如何在jQuery Mobile carousel中捕获暂停时间?

有趣的部分是:

var myScroll; 
var old_page=0; 
function loaded() { 
    myScroll = new iScroll('wrapper', { 
     snap: true, 
     momentum: false, 
     hScrollbar: false, 
     onScrollEnd: function(){ 
      var currPage = myScroll.currPageX+1; 
      var firstPage = parseInt(document.querySelector('#indicator > li:first-child').innerHTML); 
      var lastPage = parseInt(document.querySelector('#indicator > li:last-child').innerHTML); 
      if(currPage <= lastPage && currPage >= firstPage){ 
       if(old_page < currPage){ 
        document.querySelector('#indicator > li.active').className = ''; 
        document.querySelector('#indicator > li:nth-child(' + (this.currPageX+1) + ')').className = 'active'; 
        document.getElementById("prev").style.visibility="visible"; 
       } 
       else if(old_page > currPage){ 
        document.querySelector('#indicator > li.active').className = ''; 
        document.querySelector('#indicator > li:nth-child(' + (this.currPageX+1) + ')').className = 'active'; 
        document.getElementById("next").style.visibility="visible"; 
       } 
       old_page = currPage; 
       if(old_page == lastPage){ 
        document.getElementById("next").style.visibility="hidden"; 
       } 
       else if(old_page == firstPage){ 
        document.getElementById("prev").style.visibility="hidden"; 
       } 
      } 
      else{ 
       myScroll.scrollToPage(lastPage-1,0); 
      } 
     } 
    }); 
} 

function gotoNextPage(){ 
    if(document.getElementById("prev").style.visibility == "hidden"){ 
     document.getElementById("prev").style.visibility="visible"; 
    } 
    var currPage = parseInt(document.querySelector('#indicator > li.active').innerHTML); 
    var lastPage = parseInt(document.querySelector('#indicator > li:last-child').innerHTML); 

    if(currPage == (lastPage-1)){ 
     document.getElementById("next").style.visibility="hidden"; 
    } 
    document.querySelector('#indicator > li.active').className = ''; 
    document.querySelector('#indicator > li:nth-child(' + (currPage+1) + ')').className = 'active'; 
    myScroll.scrollToPage('next', 750); 
} 

function gotoPrevPage(){ 
    if(document.getElementById("next").style.visibility == "hidden"){ 
     document.getElementById("next").style.visibility="visible"; 
    } 
    var currPage = parseInt(document.querySelector('#indicator > li.active').innerHTML); 
    var firstPage = parseInt(document.querySelector('#indicator > li:first-child').innerHTML); 
    if((currPage-1) == firstPage){ 
     document.getElementById("prev").style.visibility="hidden"; 
    } 
    document.querySelector('#indicator > li.active').className = ''; 
    document.querySelector('#indicator > li:nth-child(' + (currPage-1) + ')').className = 'active'; 
    myScroll.scrollToPage('prev', 750); 
} 

document.addEventListener('DOMContentLoaded', loaded, false); 

回答

0

整理并添加额外的功能,我得到这样的:

function loaded() { 
    var $$ = document.querySelector; 
    var firstPage = parseInt($$('#indicator > li:first-child').innerHTML, 10); 
    var lastPage = parseInt($$('#indicator > li:last-child').innerHTML, 10); 
    var myScroll = new iScroll('wrapper', { 
     snap: true, 
     momentum: false, 
     hScrollbar: false, 
     onScrollEnd: function() { 
      var currPage = myScroll.currPageX + 1; 
      if(currPage <= lastPage && currPage >= firstPage) { 
       $$('#indicator > li.active').className = ''; 
       $$('#indicator > li:nth-child(' + currPage + ')').className = 'active'; 
       document.getElementById("prev").style.visibility = (currPage == firstPage) ? "hidden" : "visible"; 
       document.getElementById("next").style.visibility = (currPage == lastPage) ? "hidden" : "visible"; 
      } 
      else { 
       myScroll.scrollToPage(lastPage-1,0); 
      } 
     } 
    }); 
    var hoverTimeout; 
    var wrapper = document.getElementById('wrapper'); 
    var items = wrapper.getElementsByTagName("???"); 
    for(var i=0; i<items.length; i++) { 
     var item = items[i]; 
     item.onmouseover = function() { 
      var that = this; 
      hoverTimeout = setTimeout(function() { 
       updateDropdown(that);//adjust this statement to call existing function 
      }, 3000); 
     }; 
     item.onmouseout = function() { 
      clearTimeout(hoverTimeout); 
     }; 
    } 
} 

得到它的工作,你需要于:

    var items = ...声明
  • ,改变???到其中的标签名e包装中的滚动元素(例如'img')。
  • setTimeout(...)语句中,调用某个更新下拉列表的现有函数。

编辑

在jQuery中这将是这样的:

$(function() { 
    var $wrapper = $('#wrapper'), 
     $indicatorElements = $('#indicator li'), 
     $prev = $("#prev"), 
     $next = $("#next"), 
     hoverTimeout; 
    var myScroll = new iScroll('wrapper', { 
     snap: true, 
     momentum: false, 
     hScrollbar: false, 
     onScrollEnd: function() { 
      var currPage = this.currPageX; 
      if(currPage <= ($indicatorElements.length-1) && currPage >= 0) { 
       $indicatorElements.removeClass('active').eq(currPage).addClass('active'); 
       $prev.css('visibility', (currPage == 0) ? "hidden" : "visible"); 
       $next.css('visibility', (currPage == ($indicatorElements.length-1)) ? "hidden" : "visible"); 
      } 
      else { 
       this.scrollToPage($indicatorElements.length-1, 0); 
      } 
     } 
    }); 
    $wrapper.find("img").each(function(i, item) { 
     $(item).hover(function() { 
      hoverTimeout = setTimeout(function() { 
       updateDropdown(item);//adjust this statement to call existing function 
      }, 3000); 
     }, function() { 
      clearTimeout(hoverTimeout); 
     }; 
    }); 
}); 

两个版本未经测试,可能需要调试。