微信小程序自定義scroll-view的實例代碼
更新時間:2021年04月01日 15:19:37 作者:xianshengwen
這篇文章主要給大家介紹了關(guān)于微信小程序自定義scroll-view的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
小程序自定義 scroll-view 滾動條
話不多說, 直接上效果圖
效果圖
wxml代碼
<scroll-view scroll-x class="scroll-view" bindscroll="bindScroll"> <block wx:for="{{arr}}" wx:key="index"> <view class="scroll-item">scroll-view{{index}}</view> </block> </scroll-view> <!-- 滾動條 --> <view class="slide"> <view class='slide-bar'> <view class="slide-action" style="width:{{slideWidth}}rpx; margin-left:{{slideLeft<=1 ? 0 : slideLeft+'rpx'}};"> </view> </view> </view>
wxss代碼
page{ height: 100vh; background: rgb(111, 80, 65) } .scroll-view{ display: flex; width: 100%; white-space: nowrap; padding-top: 20rpx; } .scroll-item:nth-child(1){ margin-left: 40rpx; } .scroll-item { display: inline-block; width: 550rpx; height: 463rpx; background: rgba(199, 180, 165); border-radius: 20rpx; margin-right: 30rpx; color: #fff; } .slide{ background:rgb(111, 80, 65); width:100%; padding-top:20rpx; } .slide .slide-bar{ width:180rpx; margin:0 auto; height: 4rpx; background: rgba(255,255,255,.2); } .slide .slide-bar .slide-action{ height:100%; background:#fff; }
js代碼
/** * 頁面的初始數(shù)據(jù) */ data: { arr: 10, slideWidth: '', slideLeft: '' }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function (options) { // 計算比例 this.calcRatio(); }, /** * 計算比例 */ calcRatio() { var windowWidth = wx.getSystemInfoSync().windowWidth; // 計算列表總長度 var totalLength = (this.data.arr * 580) + 40; // 計算滑塊的比例 var slideRatio = 180 / totalLength * (750 / windowWidth); /** * 屏幕總長度 / 列表總長度 = 滑塊占滾動條長度的比例 * 滑塊占滾動條長度的比例 * 滾動列表的長度 = 滑塊的寬度 */ var sliderWidth = 750 / totalLength * 180; this.setData({ slideWidth: sliderWidth, totalLength: totalLength, slideRatio: slideRatio }) }, /** * 監(jiān)聽滾動 */ bindScroll(e) { this.setData({ slideLeft: e.detail.scrollLeft * this.data.slideRatio }) },
附:scroll-view可滾動視圖區(qū)域
總結(jié)
到此這篇關(guān)于微信小程序自定義scroll-view的文章就介紹到這了,更多相關(guān)微信小程序自定義scroll-view內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript DOM對象的學(xué)習(xí)實例代碼
javascript DOM對象的學(xué)習(xí)實例代碼2009-06-06JavaScript高級程序設(shè)計(第三版)學(xué)習(xí)筆記6、7章
這篇文章主要介紹了JavaScript高級程序設(shè)計(第三版)學(xué)習(xí)筆記6、7章 的相關(guān)資料,需要的朋友可以參考下2016-03-03js+canvas實現(xiàn)可自動吸附閉合的鼠標繪制多邊形
這篇文章主要為大家詳細介紹了js+canvas實現(xiàn)可自動吸附閉合的鼠標繪制多邊形,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07統(tǒng)一接口:為FireFox添加IE的方法和屬性的js代碼
統(tǒng)一接口:為FireFox添加IE的方法和屬性的js代碼...2007-03-03