微信小程序用swiper實(shí)現(xiàn)滑動(dòng)刻度尺
本文實(shí)例為大家分享了微信小程序用swiper實(shí)現(xiàn)滑動(dòng)刻度尺的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
思路:
利用微信swiper組件實(shí)現(xiàn)滑動(dòng)效果,創(chuàng)建一個(gè)數(shù)組arr,先存啟始數(shù)據(jù)進(jìn)去,然后分別在前面存放起始數(shù)–的數(shù)據(jù),后面添加起始數(shù)據(jù)++的數(shù)據(jù),循環(huán)數(shù)組arr創(chuàng)建swiper-item,每一個(gè)swiper-item都是一個(gè)小刻度線,達(dá)到緩沖和選擇數(shù)據(jù)的效果,滑動(dòng)的時(shí)候開始監(jiān)聽不停改變起始值,思路成立開始實(shí)踐。
實(shí)踐:
打算直接做成一個(gè)復(fù)用的組件
wxml:
swiper屬性:
display-multiple-items:當(dāng)前顯示的swiper-item數(shù)
current:當(dāng)前滑動(dòng)swiper-item個(gè)數(shù)
bindchange:滑動(dòng)時(shí)觸發(fā)的函數(shù)
<view style="height:165rpx;positive:relative;"> ? <view style="margin-bottom:20rpx;font-size:24rpx;position:absolute;width:570rpx;text-align: center;"> ? ? <text style="color: #4377E9 " ><text style="font-size: 44rpx;font-family: DIN-Bold, DIN;font-weight: bold;color: #4377E9 ;line-height: 54rpx;">{{currentNumber}}</text> {{ unit }}</text> ? ? <text class="line"></text> ? </view> ? <swiper duration="10" class="swiperss" bindchange="getCurrent" display-multiple-items="{{multipleItems}}" easing-function = "linear" current="{{ current }}"> ? ? <swiper-item class="swiperItems" wx:for="{{multipleItems + offset * 2}}" wx:key="index"> ? ? ? <view wx:if="{{arr[item]!=''}}" ? class="{{arr[item]%5==0?'linestwo':'lines'}}" ></view> ? ? ? <view style="color: #BBBEC3;">{{arr[item]%5==0?arr[item]:''}}</view> ? ? </swiper-item> ? </swiper> </view>
CSS
/* components/heightAndWeight/index.wxss */ .swiperss{ ? height: 100%; ? width: 100%; ? margin: 0 auto; ? overflow: visible; } .swiperItems{ ? font-size:24rpx; ? position:relative; ? margin-top: 74rpx; ? border-top:1px solid #F5F7F9;? ? height: 120rpx !important; ? width: calc(570rpx / 19) !important; ? overflow: visible; } .swiperItems > .lines{ ? background-color:#D2D6DF ?; ? margin-bottom: 10rpx; ? width:1px;height:35rpx; ? margin-left: 15rpx; } .linestwo{ ? margin: 0 auto; ? width:1px;height:63rpx; ? background-color:#D2D6DF ; ? margin-left: 16rpx; } .lines + view{ ? font-size: 24rpx; ? font-family: DIN-Regular, DIN; ? font-weight: 400; ? color: #D9D9D9; ? line-height: 30rpx; ? width: 100%; ? text-align: center; } .line{ ? position: absolute; ? left:50.4%; ? top: 64rpx; ? transform: translateX(-50%); ? width: 5rpx; ? height: 40rpx; ? background: #43A3FF; ? box-shadow: 0px 0px 2rpx 0px #43A3FF; ? z-index: 6; ? margin-right: 10rpx; }
js
字傳父數(shù)值:
min:刻度尺最小值
max:刻度尺最大值
unit:中間的固定線的單位
currentNumber:刻度線的起始默認(rèn)值
multipleItems:swiper可視區(qū)域的swiper-item有多少
offset:我們的選擇指針在中間,在滑動(dòng)到最小值或者最大值時(shí)最小的值反而在兩邊,我們需要在前后補(bǔ)上空白的刻度尺,讓最小值或者最大值滑動(dòng)到中間來
一個(gè)前面說的arr數(shù)組個(gè)數(shù)等于 multipleItems的個(gè)數(shù)+偏差值*2 然后循環(huán)就可以了
// components/heightAndWeight/index.js Component({ ? /** ? ?* 組件的屬性列表 ? ?*/ ? properties: { ? ? min: { ? ? ? type: Number ? ? }, ? ? max: { ? ? ? type: Number ? ? }, ? ? unit: { ? ? ? type: String ? ? }, ? ? currentNumber: { ? ? ? type: Number ? ? }, ? ? multipleItems: { ? ? ? type: Number, ? ? ? value: 9 ? ? }, ? ? offset: { ? ? ? type: Number, ? ? ? value: 4 ? ? } ? }, ? observers: { ? ? "current": function (current) { ? ? ? console.log('current-----currentNumber---', current, this.data.currentNumber) ? ? ? if (current < this.data.offset) { ? ? ? ? this.setData({ ? ? ? ? ? currentNumber: Math.max(this.data.currentNumber + current - this.data.offset, this.data.minNumber) ? ? ? ? }) ? ? ? } else if (current > this.data.offset) { ? ? ? ? this.setData({ ? ? ? ? ? currentNumber: Math.min(this.data.currentNumber + current - this.data.offset, this.data.maxNumber) ? ? ? ? }) ? ? ? } ? ? }, ? ? "currentNumber": function (currentNumber) { ? ? ? console.log('----currentNumber', currentNumber) ? ? ? let arr = [] ? ? ? for (let l = parseInt(this.data.multipleItems / 2) + this.data.offset; l > 0; l--) { ? ? ? ? arr.push(currentNumber - l >= this.data.minNumber ? currentNumber - l : '') ? ? ? } ? ? ? arr.push(currentNumber) ? ? ? for (let l = 1; l <= ?parseInt(this.data.multipleItems / 2) + this.data.offset; l++) { ? ? ? ? arr.push(currentNumber + l <= this.data.maxNumber ? currentNumber + l : '') ? ? ? } ? ? ? console.log('-----arr', arr) ? ? ? this.setData({ ? ? ? ? arr, ? ? ? ? current: this.data.offset ? ? ? }) ? ? } ? }, ? attached() { ? ? console.log('this.dddddddddddd', this.data.currentNumber) ? ? this.setData({ ? ? ? minNumber: this.data.min, ? ? ? maxNumber: this.data.max, ? ? ? current: 0, ? ? ? ? ? ? arr: [], ? ? }) ? ?? ? }, ? /** ? ?* 組件的初始數(shù)據(jù) ? ?*/ ? data: { ? ? minNumber: null, ? ? maxNumber: null, ? ? current: 0, ?? ?? ? ? arr: [] ? }, ? /** ? ?* 組件的方法列表 ? ?*/ ? methods: { ? ? getCurrent(e) { ? ? ? this.setData({ ? ? ? ? current: e.detail.current ? ? ? }) ? ? ? console.log('eeeeeeeeeeeeee', e.detail.current, this.data.currentNumber) ? ? ? this.triggerEvent('currentNumber', { ? ? ? ? current: this.data.currentNumber ? ? ? }) ? ? } ? } })
監(jiān)聽currentNumber的值根據(jù)根據(jù)這個(gè)值創(chuàng)造出循環(huán)arr的數(shù)組,這樣就可以不用創(chuàng)建一大堆數(shù)據(jù),直接動(dòng)態(tài)創(chuàng)建數(shù)據(jù),再監(jiān)聽刻度尺的滑動(dòng),根據(jù)他的往左往右 改變currentNumber的值,到最小或者最大時(shí)停止。
最后拋出getCurrent函數(shù) 把currentNumber值拋出,在頁面上就可以拿到當(dāng)前滑動(dòng)的值隨心所欲啦
組件使用頁面wxml:
<view class="flex" style="flex-wrap:wrap;margin-top: 20rpx"> ? ? <height-weight3 ? ?offset="10" ? ?min="1" max="150" unit="歲" ?multipleItems="19" ? ?currentNumber="{{ ageCurrentNumber }}" ?style="width:570rpx;margin: 0 auto;" bind:currentNumber="getCurrentNumberAge"></height-weight3> ? </view>
js
getCurrentNumberAge(e){ ?//年齡 ? ? console.log('獲取當(dāng)前current值年齡',e,e.detail.current) ? ? let result = e.detail.current; ? ? this.setData({ ? ? ? age:result? ? ? }) ? },
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用純JS實(shí)現(xiàn)checkbox的框選效果(鼠標(biāo)拖拽多選)
最近做了一個(gè)用js實(shí)現(xiàn)鼠標(biāo)拖拽多選的功能,于是整理了一下思路,寫了一個(gè)小demo,下面這篇文章主要給大家介紹了關(guān)于如何使用純JS實(shí)現(xiàn)checkbox的框選效果(鼠標(biāo)拖拽多選)的相關(guān)資料,需要的朋友可以參考下2022-05-05詳解JavaScript中的before-after-hook鉤子函數(shù)
最近看別人的代碼,接觸到一個(gè)插件,before-after-hook,百度搜一圈也沒有看到什么地方有教程,本文就來簡單介紹一下這個(gè)插件的使用方法,需要的可以參考一下2022-12-12Highcharts 非常實(shí)用的Javascript統(tǒng)計(jì)圖demo示例
官網(wǎng)實(shí)例中給出了各式各樣的demo,可以參照document修改自己需要的即可,本文實(shí)現(xiàn)的是一個(gè)學(xué)生成績走勢demo,有需求的朋友可以參考下哈,希望對(duì)大家有所幫助2013-07-07JS實(shí)現(xiàn)獲取進(jìn)今年第幾天是周幾的方法分析
這篇文章主要介紹了JS實(shí)現(xiàn)獲取進(jìn)今年第幾天是周幾的方法,結(jié)合實(shí)例形式對(duì)比分析了JavaScript進(jìn)行日期與天數(shù)運(yùn)算相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-06-06淺析JSONP技術(shù)原理及實(shí)現(xiàn)
這篇文章主要介紹了淺析JSONP技術(shù)原理及實(shí)現(xiàn) 的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06