2012-05-11 98 views
0

我一直在網上搜索幾天,並沒有找到解決我的問題。我正在建設一個移動網站,並且會有一些元素可以通過用手指在他們上面滑動來水平滾動。到目前爲止,這工作正常。暫時禁用touchmove上的滾動(手機)

爲了防止頁面在滑動時垂直滾動,我添加了event.preventDefault()到touchmove事件,這裏是問題;我希望用戶能夠在垂直滑動此元素的同時滾動頁面,但只有當他/她滑過它時,可以說60個像素(垂直)。我怎樣才能做到這一點?通過我使用的代碼,「else」(當滾動長度大於70px時)被執行,但不會發生滾動。我甚至想做甚麼,在這種情況下怎麼做?

下面是一些代碼什麼我已經試過:

$(Carousel_Wrapper).bind('touchstart', function(event) 
{ 
    // Some other stuff happens here 

    // Set last y-coord 
    Carousel_LastPageY = event.originalEvent.touches[0].pageY; 

    // Bind the touchmove event 
    $(Carousel_Wrapper).bind('touchmove', function(event) 
    { 
     // The function which scrolls the content of the element 
     Carousel_Drag(event.originalEvent.touches[0].pageX, event); 

     // Calculate the vertical swipe length 
     var Carousel_VerticalSwipeLength = event.originalEvent.touches[0].pageY - Carousel_LastPageY; 

     // Convert to a positive value 
     if(Carousel_VerticalSwipeLength < 0) 
     { 
      Carousel_VerticalSwipeLength = Carousel_VerticalSwipeLength * -1; 
     } 

     // If the vertical scroll-length is less than 70px 
     if(Carousel_VerticalSwipeLength < 70) 
     { 
      event.preventDefault(); 
     } 

     // The scroll-length was more than 70px, resume scrolling 
     else 
     { 
      // I've tried this: 
      return true; 

      // Then this: 
      $(Carousel_Wrapper).unbind('touchmove'); 

      // And at last this: 
      $(Carousel_Wrapper).unbind('touchmove'); 
      $(Carousel_Wrapper).trigger('touchmove'); 
     } 
    }); 
}); 

任何幫助表示讚賞!

感謝, 亨裏克

回答

0

你的變量應該存儲在觸摸差異的總和。你的變量看起來像只看每個差異的數量,而不是它們的總和。

+0

所以你的意思是其他(其中滾動長度超過70px)沒有被觸發?事實上,我已經通過向垂直滑動距離(從其他地方)寫入臨時元素來嘗試這種方式,並且它檢出,正在執行。如果那不是你的意思,請糾正我 – Henrik