better-scroll應用


一、n安裝

  1、在package.json文件的dependencies中添加依賴"better-scroll": "^0.1.7",在終端:npm install

  2、npm安裝:npm install better-scroll --save-dev

二、使用

  1、在js中:import BScroll from 'better-scroll';

  2、異步請求數據更新后在this.$nextTick中創建BScroll實例:this.scroll = new BScroll(this.$refs.wrapper,{probeType3});

   1)this.$refs.wrapper是DOM

     2){Options}:     

  • startX: 0 開始的X軸位置
  • startY: 0 開始的Y軸位置
  • scrollY: true 滾動方向
  • click: true 是否啟用click事件
  • directionLockThreshold: 5
  • momentum: true 是否開啟拖動慣性
  • bounce: true 是否啟用彈力動畫效果,關掉可以加速
  • selectedIndex: 0
  • rotate: 25
  • wheel: false 該屬性是給 picker 組件使用的,普通的列表滾動不需要配置
  • snap: false 是否開啟捕捉元素,當為 true 時,捕捉的元素會根據可滾動的位置和滾動區域計算得到可滑動幾頁。
  • snapLoop: false 是否創建當前滾動元素子集的拷貝
  • snapThreshold: 0.1 滑動的長度限制,只有大於這個距離才會觸發事件
  • swipeTime: 2500 swipe 持續時間
  • bounceTime: 700 彈力動畫持續的毫秒數
  • adjustTime: 400
  • swipeBounceTime: 1200
  • deceleration: 0.001 滾動動量減速越大越快,建議不大於0.01
  • momentumLimitTime: 300 慣性拖動的回彈時間
  • momentumLimitDistance: 15 慣性拖動的回彈距離
  • resizePolling: 60 重新調整窗口大小時,重新計算better-scroll的時間間隔
  • probeType: 1 監聽事件的觸發時間,1為即時觸發,3為延遲到事件完畢后觸發
  • preventDefault: true 是否阻止默認事件
  • preventDefaultException: { tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/ } 阻止默認事件
  • HWCompositing: true 是否啟用硬件加速
  • useTransition: true 是否使用CSS3的Transition屬性,否則使用-requestAnimationFram代替
  • useTransform: true 是否使用CSS3的Transform屬性
  • probeType: 1 滾動的時候會派發scroll事件,會截流。2滾動的時候實時派發scroll事件,不會截流。 3除了實時派發scroll事件,在swipe的情況下仍然能實時派發scroll事件

  3、Events 事件(監聽事件)

    let scroll new BScroll(document.getElementById('wrapper'),{

       probeType3//option的probeType必須要傳入
    })
 
    scroll.on('scroll'(pos=> {
      console.log(pos.'~posx.y)
    })

  Events 列表

  • beforeScrollStart - 滾動開始之前觸發
  • scrollStart - 滾動開始時觸發
  • scroll - 滾動時觸發
  • scrollCancel - 取消滾動時觸發
  • scrollEnd - 滾動結束時觸發
  • flick - 觸發了 fastclick 時的回調函數
  • refresh - 當 better-scroll 刷新時觸發
  • destroy - 銷毀 better-scroll 實例時觸發

  4、派發滾動

  let scroll new BScroll(document.getElementById('wrapper'))

  scroll.scrollTo(0500)
 
  scrollToElement(el, time, offsetX, offsetY, easing) 滾動到  某個元素,el(必填)表示 dom 元素,time 表示動畫,offsetX 和 offsetY 表示坐標偏移量,easing 表示緩動函數


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM