您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用better-scroll怎么實現菜單和內容聯動效果,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link href="https://unpkg.com/element-ui/lib/theme-chalk/index.css" rel="stylesheet"> <style> *{ margin: 0; padding: 0; border: 0; outline: none; font-family: Arial; list-style: none; -webkit-font-smoothing: antialiased; } html, body, #app{ height: 100%; } #app{ padding: 20px 0; box-sizing: border-box; } .wrapper{ display: flex; height: 100%; overflow: hidden; max-width: 1200px; margin: 0 auto; } .menu{ width: 100px; height: 100%; padding-right: 20px; } .content{ flex: 1; height: 100%; overflow: hidden; position: relative; } .menu-item{ margin-bottom: 10px; } .menu-item-content{ width: 100%; padding: 7px 0; text-align: center; border: 1px solid #ddd; border-radius: 2px; color: #333; cursor: pointer; } .active{ border-color: #409EFF; background: #409EFF; color: #fff; } .content-item{ margin-bottom: 20px; } .content-item-text{ border-radius: 2px; border: 1px solid #ddd; } .content-item-text>img{ width: 100%; vertical-align: middle; } </style> </head> <body> <div id="app"> <div class="wrapper"> <div class="menu"> <ul> <li class="menu-item" v-for="(item, index) in menu" :key="index" @click="handleClick(index)"> <div class="menu-item-content" :class="{'active': getCurrentIndex() == index}" v-text="item.label"></div> </li> </ul> </div> <div class="content" ref="content"> <ul> <li class="content-item" v-for="(item, index) in content" :key="index"> <div class="content-item-text"> <img :src="item" alt=""> </div> </li> </ul> </div> </div> </div> <script src="https://cdn.jsdelivr.net/npm/better-scroll"></script> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src="https://unpkg.com/element-ui/lib/index.js"></script> <script> new Vue({ el: '#app', data: { contentScroll: null, listHeight: [], // 用來存儲每一個子盒子的高度 currentIndex: 0, scrollY: 0, menu: [ { label: '圖片一', id: 0, }, { label: '圖片二', id: 1, }, { label: '圖片三', id: 2, }, { label: '圖片四', id: 3, }, { label: '圖片五', id: 4, }, ], content: [ 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1575539093143&di=d3d787658bd0b0f21d2459d90b3bd19b&imgtype=jpg&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D1735688044%2C4235283864%26fm%3D214%26gp%3D0.jpg', 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1575539192878&di=f46b69a4c2db2ba2f07f0fe1cc7af952&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201210%2F04%2F20121004231502_NrBQG.jpeg', 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1575539157019&di=122152eaee12d6ea019b2cec2b80e468&imgtype=0&src=http%3A%2F%2Fpic44.nipic.com%2F20140723%2F18505720_094503373000_2.jpg', 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1575539175569&di=d33d35a826cc700add7b7bd53f9282c0&imgtype=0&src=http%3A%2F%2Fpic37.nipic.com%2F20140103%2F10616010_215644481373_2.jpg', 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1575539212191&di=ec438ea6559d06cc1a49a27b122e8edf&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fblog%2F201312%2F09%2F20131209151602_evtcw.jpeg', ] }, methods: { initScroll() { const content = this.$refs.content; this.contentScroll = new BScroll(content, { click: true, // 因為betterscroll默認會阻止點擊事件。這里要設置一下 mouseWheel: true, // 鼠標滾輪滾動 probeType: 3, scrollY: true, scrollbar: { fade: true, // 是否顯示滾動條 interactive: false, // 1.8.0 新增 }, preventDefault: false, // 阻止了瀏覽器默認選中行為 }); // 獲取滾動的值,賦值給scrollY this.contentScroll.on('scroll', pos => { this.scrollY = Math.abs(Math.round(pos.y)); }); }, getCurrentIndex() { for (let i = 0; i < this.listHeight.length; i++) { const height = this.listHeight[i]; const height1 = this.listHeight[i + 1]; // 解決當滾動到最后時,內容不足盒子高度時,菜單顯示問題。當滾動到最后,選中最后一個菜單 // maxScrollY:最大縱向滾動位置 if (Math.abs(this.contentScroll.maxScrollY) === Math.abs(this.scrollY)) { return this.content.length - 1; } if (!height1 || (this.scrollY < height1 && this.scrollY >= height)) { return i; } } }, handleClick(index) { const content = this.$refs.content; const contentList = content.getElementsByClassName('content-item'); const el = contentList[index]; // scrollToElement:滾動到目標元素 this.contentScroll.scrollToElement(el, 300); }, getHeightList() { this.listHeight = []; let height = 0; this.listHeight.push(height); const content = this.$refs.content; const contentList = content.getElementsByClassName('content-item'); for (let i = 0; i < contentList.length; i++) { const item = contentList[i]; height += item.clientHeight; this.listHeight.push(height); } this.initScroll(); } }, mounted() { this.$nextTick(function() { // 由于圖片太大,加載比較慢,造成還沒有完全加載完就初始化了,導致滾動不了,所以這里加個定時器延遲下 setTimeout(() => { this.getHeightList(); }, 400); }); }, }); </script> </body> </html>
關于使用better-scroll怎么實現菜單和內容聯動效果就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。