Vue中利用better-scroll組件實現(xiàn)橫向滾動功能
最近在學習vue的過程中,仿照去哪兒網(wǎng)的移動端寫了個小項目,旨在實踐和鞏固基礎(chǔ)知識,但是今天發(fā)現(xiàn)去哪兒的首頁上有一個組件用戶體驗較差,即一個橫向列表使用瀏覽器的原生滾動實現(xiàn),列表滾動起來較為生澀,很難受,于是乎決定由better-scroll重寫這個組件。
better-scroll介紹better-scroll是黃軼大神(沒錯,我學長)寫的基于i-scroll的一個滾動組件,項目地址:https://github.com/ustbhuangyi/better-scroll
一、滾動的實現(xiàn)原理better-scroll的滾動原理和瀏覽器原生滾動原理是一樣的,當子盒子的高度大于父盒子的高度,就會出現(xiàn)縱向滾動:
同理,如果子盒子的寬度大于父盒子的寬度,那么就會出現(xiàn)橫向滾動 ( 根本原理 )。
二、在Vue中使用better-scroll在Vue中使用better-scroll最需要注意的點就是必須等到頁面渲染完成再去執(zhí)行BScroll的實例化,因為better-scroll必須要得到滾動區(qū)域的尺寸和父盒子的尺寸,來計算出是否能滾動,所以我們必須要對Vue的生命周期有一定的了解。
這里是一個小demo,通過這個demo你將會了解到如何使用better-scroll
<template> <div ref='wrapper'> // 在vue中獲取dom元素最簡便的方法就是利用 this.$refs <ul class='content'> <li>...</li> <li>...</li> ... </ul> </div></template><script> import BScroll from ’better-scroll’ //導入better-scroll export default { mounted() { this.$nextTick(() => { // 使用 this.$nextTick 為了確保組件已經(jīng)渲染完畢this.scroll = new Bscroll(this.$refs.wrapper, {}) // 實例化BScroll接受兩個參數(shù),第一個為父盒子的dom節(jié)點 }) } }</script>三、在Vue中實現(xiàn)橫向滾動
1. 效果圖對比
使用原生滾動:
使用better-scroll:
2. 代碼(請看注釋)
<template> <div class='recommand-wrap'> <div class='title'> <img src='https://imgs.qunarzz.com/piao/fusion/1711/16/bfbb9874e8f11402.png' alt='本周熱門榜單'> <span class='title-hotrec'>本周熱門榜單</span> <span class='title-allrec'>全部榜單</span> </div> <div ref='wrapper'> /* 這里是父盒子*/ <ul ref='cont'> /* 這里是子盒子,即滾動區(qū)域*/<li v-for='item of recommendList' :key='item.id'> <div class='cont-img'> <img :src='http://www.cgvv.com.cn/bcjs/item.url' :alt='item.text'> </div> <div class='cont-dest'>{{item.text}}</div> <div class='cont-price'> <span class='price'>¥{{item.price}}</span> <span>起</span> </div></li> </ul> </div> </div></template><script>import BScroll from ’better-scroll’export default { name: ’HomeRecommand’, props: { recommendList: { type: Array, required: true } }, components: { }, data () { return { } }, methods: { verScroll () { let width = this.recommendList.length * 110// 動態(tài)計算出滾動區(qū)域的大小,前面已經(jīng)說過了,產(chǎn)生滾動的原因是滾動區(qū)域?qū)挾却笥诟负凶訉挾? this.$refs.cont.style.width = width + ’px’ // 修改滾動區(qū)域的寬度 this.$nextTick(() => {if (!this.scroll) { this.scroll = new BScroll(this.$refs.wrapper, { startX: 0, // 配置的詳細信息請參考better-scroll的官方文檔,這里不再贅述 click: true, scrollX: true, scrollY: false, eventPassthrough: ’vertical’ })} else { this.scroll.refresh() //如果dom結(jié)構(gòu)發(fā)生改變調(diào)用該方法} }) } }, mounted () { this.$nextTick(() => { let timer = setTimeout(() => { // 其實我也不想寫這個定時器的,這相當于又嵌套了一層$nextTick,但是不這么寫會失敗if (timer) { clearTimeout(timer) this.verScroll()} }, 0) }) }}</script><style lang='scss' scoped> .recommand-wrap { height: 0; padding-bottom: 50%; margin-top: .2rem; background: #fff; padding-left: .24rem; width: 100%; .title { position: relative; height: 40px; display: flex; padding: 12px 0; box-sizing: border-box; .title-img {width: 15px;height: 15px; } .title-hotrec {font-size: 16px;margin-left: 4px; } .title-allrec {position: absolute;padding-top: 2px;font-size: 13px;right: 20px;color: gray; } } .cont { list-style: none; // overflow-x: scroll;white-space: nowrap; font-size: 12px; text-align: center; padding-right: .24rem; .cont-item {position: relative;display: inline-block;padding: .06rem 0 .2rem;width: 2rem;margin: 0 .1rem;.cont-img { overflow: hidden; width: 2rem; height: 0; padding-bottom: 100%; .img { width: 100%; }}.cont-dest { margin: .1rem 0;}.cont-price { .price { color: #ff8300; }} } } }</style>
參考鏈接
作者:黃軼
鏈接:https://zhuanlan.zhihu.com/p/27407024
總結(jié)到此這篇關(guān)于Vue中利用better-scroll組件實現(xiàn)橫向滾動的文章就介紹到這了,更多相關(guān)Vue better-scroll橫向滾動內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP.NET MVC通過勾選checkbox更改select的內(nèi)容2. IntelliJ IDEA設(shè)置默認瀏覽器的方法3. idea設(shè)置提示不區(qū)分大小寫的方法4. HTTP協(xié)議常用的請求頭和響應(yīng)頭響應(yīng)詳解說明(學習)5. 使用IntelliJ IDEA 配置安卓(Android)開發(fā)環(huán)境的教程詳解(新手必看)6. .NET SkiaSharp 生成二維碼驗證碼及指定區(qū)域截取方法實現(xiàn)7. phpstudy apache開啟ssi使用詳解8. 存儲于xml中需要的HTML轉(zhuǎn)義代碼9. CentOS郵件服務(wù)器搭建系列—— POP / IMAP 服務(wù)器的構(gòu)建( Dovecot )10. ASP中實現(xiàn)字符部位類似.NET里String對象的PadLeft和PadRight函數(shù)
