better-scroll 踩坑總結

来源:https://www.cnblogs.com/zhu-xl/archive/2020/07/09/13273517.html
-Advertisement-
Play Games

1、下載安裝 1 npm install better-scroll --save 2、在項目中使用該插件的頁面引入 1 import Bscroll from 'better-scroll' 3、實例化scroll 1 this.$nextTick(() => { 2 this.scroll = ...


1、下載安裝

1 npm install better-scroll --save

 

2、在項目中使用該插件的頁面引入

1 import Bscroll from 'better-scroll'

3、實例化scroll

1  this.$nextTick(() => {
2             this.scroll = new Bscroll(this.$refs.wrapper,{
3                 scrollY: true,
4                 click: true, //設置為true 滾動元素可點擊
5                 tap: true
6             })
7         })

 

4、項目實戰,我這邊主要寫一個類似於電話本的列表點擊定位到頁面頂端的功能  主要代碼:brandList.vue 組件

  1 <template>
  2     <div class="brandList">
  3         <!-- <div class="recommend">{{branchTitle}}</div> -->
  4         <div class="wrapper" ref="wrapper">
  5             <div class="content">
  6                 <div class="brand" v-for= "(item,key) of brandList" :key= "key" :class= "item.code" :ref = "item.code">
  7                     <div class="title">{{ item.code }}</div>
  8                     <ul class="itemList" v-if="item.brandList!='undefined'">
  9                         <li class="item "
 10                             v-for=" innerItem of item.brandList"
 11                             :key= "innerItem.id"
 12                             @click= "handleCityClick(innerItem.id)"
 13                         >
 14                             <img class="brandImg" v-lazy="innerItem.logo" alt="">
 15                             <span class="brandName">{{innerItem.name}}</span>
 16                         </li>
 17                     </ul>
 18                 </div>
 19             </div>
 20         </div>
 21     </div>
 22 </template>
 23 <script>
 24 import Bscroll from 'better-scroll'
 25 export default {
 26     name:'brandList',
 27     props:{
 28         brandList:Array,
 29         branchTitle:String,
 30         letter: String
 31     },
 32     watch: {
 33         letter () {
 34             if (this.letter) {
 35                 // 獲取dom元素 我這邊本來用this.$refs[this.letter][0]獲取元素,但是在手機上就是不滑動,改成了原生獲取就好用了
 36                 const element = document.getElementsByClassName(this.letter)[0];  40                 this.scroll.scrollToElement(element);
 41             }
 42         }
 43     },
 44     mounted() {
 45         // this.$refs.wrapper 獲取dom 元素
 46         this.$nextTick(() => {
 47             this.scroll = new Bscroll(this.$refs.wrapper,{
 48                 scrollY: true,
 49                 click: true,
 50                 tap: true
 51             })
 52         })
 53     },
 54     methods:{
 55         handleCityClick(id){
 56             this.$router.push({
 57                 path:'/brandDetail',
 58                 query:{
 59                     brandId:id
 60                 }
 61             })
 62         }
 63     }
 64 }
 65 </script>
 66 <style lang="stylus" scoped>
 67 .brandList
 68     overflow hidden
 69     .recommend
 70         height: 58px;
 71         font-size: 30px;
 72         color: #5b3719;
 73         text-align:center;
 74         line-height:58px;
 75         position fixed
 76         left 0
 77         right 0
 78         z-index 1
 79         background:linear-gradient(315deg,rgba(255,252,247,1) 0%,rgba(255,248,239,1) 100%);
 80     .wrapper
 81         background #f7f7f7
 82         position:fixed;
 83         top: 198px;
 84         bottom: 102px;
 85         left:0;
 86         right: 0;
 87         // z-index: 0;
 88         .title
 89             font-size: 40px;
 90             font-weight: bold;
 91             padding: 20px;
 92         .itemList
 93             width: 90%;
 94             padding-left : 20px;
 95             display: flex;
 96             flex-wrap:wrap;
 97             .item
 98                 width:25%;
 99                 margin-bottom : 20px;
100                 .brandImg
101                     width: 146px;
102                     height: 146px;
103                     border: 2px solid #eee;
104                     border-sizing:border-box;
105                     border-radius: 10px;
106                 .brandName
107                     display:block;
108                     line-height: 32px;
109                     font-size: 22px;
110                     color: #333;
111                     font-weight: 400;
112                     overflow: hidden;
113                     text-overflow: ellipsis;
114                     white-space: nowrap;
115                     text-align: center
116 </style>

5、右側字母組件 brandName.vue

 1 <template>
 2     <ul class="list">
 3         <li  class="item">#</li>
 4         <li class="item"
 5             v-for= "(item, index) of letters"
 6             :key= "item"
 7             :ref= "item"
 8             @click="handleLetterClick($event,index)"
 9         >{{ item }}
10             <fade-animation>
11                 <div class="keyCode" v-if="showIndex == index">
12                     <span class="icon">{{item}}</span>
13                 </div>
14             </fade-animation>
15         </li>
16         <li  class="item">#</li>
17     </ul>
18 </template>
19 <script>
20 import FadeAnimation from '@/components/FadeAnimation'
21 export default {
22     name:'brandName',
23     components:{
24         FadeAnimation
25     },
26     props:{
27         brandList:Array
28     },
29     data(){
30         return{
31             showIndex:null
32         }
33     },
34     computed:{
35         letters() {
36             const letters = [];
37             this.brandList.forEach(el => {
38                 letters.push(el.code)
39             })
40             return letters
41         }
42     },
43     methods:{
44         handleLetterClick(e,i) {
45             this.showIndex = i;
46             setTimeout(()=>{
47                 this.showIndex = null
48             },2000)
49             this.$emit('change', e.target.innerText)
50         }
51     }
52 }
53 </script>
54 <style lang="stylus" scoped>
55     .list
56         position: fixed
57         right: 0
58         top: 256px
59         bottom: 102px;
60         width: 40px;
61         display: flex
62         flex-direction: column
63         justify-content:center
64     .item
65         text-align: center
66         line-height: 30px
67         font-size: 22px;
68         color: #333
69         position: relative
70         font-weight bold
71         .keyCode
72             width: 138px;
73             height: 136px;
74             background:url('../../../assets/image/[email protected]')no-repeat center top;
75             background-size: 100% 100%;
76             position:absolute;
77             left:-138px;
78             top: -48px;
79             font-size: 50px;
80             line-height: 136px;
81             .icon
82                 color: #fff;
83                 font-weight:bold;
84                 margin-left: -30px;
85 </style>

6、在父組件中調用  classifiCation.vue 

》js引入

1 import brandList from './components/brandList'
2 import brandName from './components/brandName'

》組件註入

 components:{
        brandList,
        brandName
    },

》模板註入

1  <brand-list v-show= "contentIdx == '1'" 
2             :branchTitle= "branchTitle"
3             :brandList= "brandList"
4             :letter= "letter"
5         ></brand-list>
6         <brand-name @change= "handleLetterChange"
7             v-show= "contentIdx == '1'" 
8             :brandList= "brandList"
9         ></brand-name>

7、具體實現效果圖示

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在日常生活或學習中,我們經常需要在手機和電腦之間互傳文件,這本應該是一件容易的事。有人可能會說:微信和QQ就可以。但是微信和QQ必須聯網才能使用,並且微信傳不了大文件。用微信和QQ傳文件還存在這樣一個問題:電腦傳到手機上的文件你根本不知道存在哪裡了,找起來非常麻煩。 現在市面上也有不少文件互傳的軟體 ...
  • 一、String對象 1.字元串的所有方法,都不會修改字元串本身(字元串是不可變的,操作完成之後會返回一個新的字元串) 註意點:拼接大量的字元串會有性能問題,我們經常使用伺服器渲染和模板引擎來解決這個問題 常用的方法: (1)str.length 獲取字元串的長度 (2)str.charAt(ind ...
  • 解決select2開啟標簽後谷歌瀏覽器中文輸入異常的問題 出現問題的版本: select2-4.0.13 網上找了很多沒有找到好的方法,特意找了 GitHub 上的 issues 看了一下,發現已經有人提過這個問題,於是去找最新的版本下載再試一次,果然已經修複了。 目前的版本:select2-4.1 ...
  • LimitedNnumber('.eventBox', '.viewBox', 50) /* * eventBox:輸入框id或者class * viewBox:提示元素id或者class * textLength:限制長度 */ function LimitedNnumber(eventBox, ...
  • 基於vuejs和element-ui的驗證:迴圈表單驗證、迴圈表格表單驗證 代碼: <!-- * @lastEditors: lingyang * @Date: 2019-12-16 15:31:22 * @LastEditTime: 2020-07-09 17:26:04 --> <templat ...
  • 兩年多的彷徨、迷茫。最終又回歸初心,回歸前端開發。這次部門給我安排的任務是,做一個公司自己的組件庫。計劃爭取把做組件庫的每個關鍵過程 記錄下來,給 大家,給 自己 提供有用的幫助。 不多說了,切入正題。 提到組件庫,最先想到的就是element-ui, 我也是先那 它 做的demo實現。 先說思路了 ...
  • 隨著web前端的飛速發展,學習web前端的人員也是越來越多,在移動 互聯網 時代,相信我們每個人的手機上都裝有數十個APP,這些APP的開發其實也和當下熱門的 Web 前端開發息息相關。 事實上,如今一半以上的APP都是採用Hybrid混合模式開發,即結合安卓和Web端技術開發。而純 Web開發 的 ...
  • 前端都需要學什麼(可以分為八個階段) <1>第一階段:▪ HTML+CSS:HTML進階、 CSS進階、DIV+CSS佈局、HTML+CSS整站開發、▪ JavaScript基礎:Js基礎教程、js內置對象常用方法、常見DOM樹操作大全、ECMAscript、DOM、BOM、定時器和焦點圖。▪ JS ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...