格式:var obj = new BScroll(object,{[option1,],.,.});
注意:
1、要確保object元素的高度比其父元素高
2、使用時(shí),一定要確保object所在的dom渲染后,再用上面的語句,或者obj.refresh()
Options 參數(shù)
- startX: 0 開始的X軸位置
- startY: 0 開始的Y軸位置
- scrollY: true 滾動方向?yàn)?Y 軸
- scrollX: true 滾動方向?yàn)?X 軸
- click: true 是否派發(fā)click事件,通常判斷瀏覽器派發(fā)的click還是betterscroll派發(fā)的click,可以用event._constructed,若是bs派發(fā)的則為true
- directionLockThreshold: 5
- momentum: true 當(dāng)快速滑動時(shí)是否開啟滑動慣性
- bounce: true 是否啟用回彈動畫效果
- selectedIndex: 0 wheel 為 true 時(shí)有效,表示被選中的 wheel 索引
- rotate: 25 wheel 為 true 時(shí)有效,表示被選中的 wheel 每一層的旋轉(zhuǎn)角度
- wheel: false 該屬性是給 picker 組件使用的,普通的列表滾動不需要配置
- snap: false 該屬性是給 slider 組件使用的,普通的列表滾動不需要配置
- snapLoop: false 是否可以無縫循環(huán)輪播
- snapThreshold: 0.1 用手指滑動時(shí)頁面可切換的閾值,大于這個(gè)閾值可以滑動的下一頁
- snapSpeed: 400, 輪播圖切換的動畫時(shí)間
- swipeTime: 2500 swipe 持續(xù)時(shí)間
- bounceTime: 700 彈力動畫持續(xù)的毫秒數(shù)
- adjustTime: 400 wheel 為 true 有用,調(diào)整停留位置的時(shí)間
- swipeBounceTime: 1200 swipe 回彈 時(shí)間
- deceleration: 0.001 滾動動量減速越大越快,建議不大于0.01
- momentumLimitTime: 300 符合慣性拖動的最大時(shí)間
- momentumLimitDistance: 15 符合慣性拖動的最小拖動距離
- resizePolling: 60 重新調(diào)整窗口大小時(shí),重新計(jì)算better-scroll的時(shí)間間隔
- preventDefault: true 是否阻止默認(rèn)事件
- preventDefaultException: { tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/ } 阻止默認(rèn)事件
- HWCompositing: true 是否啟用硬件加速
- useTransition: true 是否使用CSS3的Transition屬性
- useTransform: true 是否使用CSS3的Transform屬性
- probeType: 1 滾動的時(shí)候會派發(fā)scroll事件,會截流。2滾動的時(shí)候?qū)崟r(shí)派發(fā)scroll事件,不會截流。 3除了實(shí)時(shí)派發(fā)scroll事件,在swipe的情況下仍然能實(shí)時(shí)派發(fā)scroll事件
Events 事件
代碼實(shí)例:
let scroll = new BScroll(document.getElementById('wrapper'),{ probeType: 3}) scroll.on('scroll', (pos) => { console.log(pos.x + '~' + pos.y)})
- beforeScrollStart - 滾動開始之前觸發(fā)
- scrollStart - 滾動開始時(shí)觸發(fā)
- scroll - 滾動時(shí)觸發(fā)
- scrollCancel - 取消滾動時(shí)觸發(fā)
- scrollEnd - 滾動結(jié)束時(shí)觸發(fā)
- touchend - 手指移開屏幕時(shí)觸發(fā)
- flick - 觸發(fā)了 fastclick 時(shí)的回調(diào)函數(shù)
- refresh - 當(dāng) better-scroll 刷新時(shí)觸發(fā)
- destroy - 銷毀 better-scroll 實(shí)例時(shí)觸發(fā)
函數(shù)列表
scrollTo(x, y, time, easing):滾動到某個(gè)位置,x,y 代表坐標(biāo),time 表示動畫時(shí)間,easing 表示緩動函數(shù)scroll.scrollTo(0, 500)
scrollToElement(el, time, offsetX, offsetY, easing):滾動到某個(gè)元素,el(必填)表示 dom 元素,time 表示動畫時(shí)間,offsetX 和 offsetY 表示坐標(biāo)偏移量,easing 表示緩動函數(shù)
refresh():強(qiáng)制 scroll 重新計(jì)算,當(dāng) better-scroll 中的元素發(fā)生變化的時(shí)候調(diào)用此方法
getCurrentPage():snap 為 true 時(shí),獲取滾動的當(dāng)前頁,返回的對象結(jié)構(gòu)為 {x, y, pageX, pageY},其中 x,y 代表滾動橫向和縱向的位置;pageX,pageY 表示橫向和縱向的頁面索引。用法如:getCurrentPage().pageX
goToPage(x, y, time, easing)
snap 為 true,滾動到對應(yīng)的頁面,x 表示橫向頁面索引,y 表示縱向頁面索引, time 表示動畫,easing 表示緩動函數(shù)(可省略不寫)
enable()啟用 better-scroll,默認(rèn)開啟
disable() 禁用 better-scroll
destroy() 銷毀 better-scroll,解綁事件
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。