微信小程序阻止swiper手動滑動的多種情況

    近日在微信小程序開發中遇到需要阻止swiper組件手動滑動的需求,看了一些博文,列舉的情景比較單一,這裏把我想到的情況分別針對處理一下,如有遺漏,歡迎補充

一、同頁面只有一個swiper,且swiper-item上沒有需要點擊的按鈕

    1.用css控制,在swiper上給個樣式pointer-events:none; 使點擊無法觸發

    2.在swiper-item綁定一個事件,catchtouchmove="catchtouchmove",在事件中return false

 

二、頁面中只有一個swiper,但swiper-item上有需要點擊的按鈕

    1.在swiper上給樣式pointer-events:none; 在需要點擊的按鈕上給樣式pointer-events:auto

    2.在swiper-item綁定一個事件,catchtouchmove="catchtouchmove",在事件中return false,同時在按鈕上綁定點擊事件

 

三、同頁面有多個swiper,且swiper-item上沒有需要點擊的按鈕

    當頁面有多個swiper,不能使用catchtouchmove的方式,因爲如果觸發了點擊,被點擊的swiper滑動將出現延遲,多個swiper之間的滑動將不同步

    所以我們可以添加樣式pointer-events:none 阻止觸發

 

四、同頁面有多個swiper,且swiper-item上有需要點擊的按鈕

     既要阻止手動滑動,又需要觸發按鈕,同時要保證多個swiper同步滑動

     解決方案:在swiper上給樣式pointer-events:none; 在需要點擊的按鈕上給樣式pointer-events:auto,然後js控制點擊結束後才允許自動滑動

    實現:在按鈕上監聽觸發開始catchtouchstart="catchtouchstart",觸發按鈕時將autoplay 設置爲false,再監聽觸摸結束catchtouchend="touchend",將autoplay 設置爲true(注:autopaly:true表示自動滑動)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章