微信小程序scroll-view組件實(shí)現(xiàn)滾動(dòng)動(dòng)畫
本文實(shí)例為大家分享了scroll-view組件實(shí)現(xiàn)索引列表滾動(dòng)動(dòng)畫效果,供大家參考,具體內(nèi)容如下
效果圖
實(shí)現(xiàn)原理
利用scroll-view的scroll-into-view屬性進(jìn)行定位;
利用scroll-view的scroll-with-animation屬性實(shí)現(xiàn)滾動(dòng)動(dòng)畫過度。
WXML
<view class="right-nav">
<view bindtap="getCurrentCode" class="{{chooseIndex == index ? '.city-list-active' : ''}}" wx:for="{{cityList}}" style="height:{{codeHeight}}px" data-code="{{item.code}}" data-index="{{index}}">
{{item.code}}
</view>
</view>
<view class="city-layer {{isShowLayer ? '' : 'layer-hide'}}">
{{codeY}}
</view>
<view class="current-choose-city">當(dāng)前選擇機(jī)場(chǎng):{{chooseCity}}</view>
<scroll-view class="city-scroll" scroll-y="true" scroll-into-view="{{codeY}}" scroll-with-animation="true" style="height:{{cityHeight}}px" bindscroll="scroll">
<view class="city-box" wx:for="{{cityList}}" wx:key="{{item.code}}">
<view class="city-code" id="{{item.code}}">{{item.code}}</view>
<view class="city-list" wx:for="{{item.cityList}}" wx:for-item="city" bindtap="getChooseCity" data-city="{{city}}">
{{city}}
</view>
</view>
</scroll-view>
WXSS
.current-choose-city{
position: fixed;
width: 100%;
height: 50px;
line-height: 50px;
padding: 0 10px;
top: 0;
left: 0;
background-color: #fff;
z-index: 10;
}
.right-nav{
width: 30px;
color: #888;
text-align: center;
position: fixed;
bottom: 0;
right: 0;
background-color: rgb(200, 200, 200);
z-index: 9;
}
.city-scroll{padding-top: 50px;}
.city-code{
background-color: #f7f7f7;
}
.city-list,.city-code{
height: 39px;
line-height: 40px;
padding: 0 30px 0 10px;
overflow: hidden;
border-bottom: 1px solid #c8c7cc;
}
.city-list-active{color:#007aff;}
/*提示點(diǎn)擊的字母 */
.city-layer{
width: 70px;
height: 70px;
line-height: 70px;
text-align: center;
border-radius: 50%;
color: #fff;
background-color: rgba(0, 0, 0, .7);
position: fixed;
top: calc(50% - 35px);
left:calc(50% - 35px);
z-index: 11;
}
.layer-hide{display: none;}
JS
var city_list = require('./city.js');
Page({
data: {
cityList: city_list.city,
chooseCity: '您還未選擇機(jī)場(chǎng)!',
isShowLayer: false,
chooseIndex: 0,
codeY: 'A',
codeHeight: null,
cityHeight:null
},
onLoad (options) {
var windowHeight = wx.getSystemInfoSync().windowHeight;
this.setData({
codeHeight: (windowHeight - 50) / this.data.cityList.length,
cityHeight: windowHeight - 50,
});
},
getCurrentCode(e){
var self = this;
this.setData({
codeY: e.target.dataset.code,
chooseIndex: e.target.dataset.index,
isShowLayer: true
})
setTimeout(() => {
self.setData({ isShowLayer: false })
},500);
},
getChooseCity(e){
this.setData({ chooseCity: e.target.dataset.city });
}
})
對(duì)比
微信小程序—-全國(guó)機(jī)場(chǎng)索引列表(MUI索引列表)
對(duì)比結(jié)果總結(jié)
- 由于scroll-view的scroll-into-view屬性是滾動(dòng)到指定id位置,所以,在列表的字母行加上id屬性;
- 由于scroll-view的scroll-into-view屬性實(shí)現(xiàn)了滾動(dòng)到指定位置,所以減少了scrollTop的計(jì)算;
- 由于scroll-view的scroll-with-animation屬性,實(shí)現(xiàn)了滾動(dòng)動(dòng)畫過度效果;
- 減少了計(jì)算scrollTop的循環(huán)消耗;
- js代碼量減少,減少this.setData方法的變量設(shè)置。
DEMO下載
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序?qū)崿F(xiàn)一個(gè)自定義遮罩層效果
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)一個(gè)自定義遮罩層,大概效果是點(diǎn)擊按鈕Show顯示遮罩層,再次點(diǎn)擊屏幕任何地方隱藏遮罩層,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09
Code Review 方法論與實(shí)踐總結(jié)梳理
這篇文章主要為大家介紹了Code Review 方法論與實(shí)踐總結(jié)梳理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
基于Bootstrap3表格插件和分頁插件實(shí)例詳解
這篇文章主要介紹了基于Bootstrap3表格插件和分頁插件實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-05-05
基于javascript實(shí)現(xiàn)日歷功能原理及代碼實(shí)例
這篇文章主要介紹了基于javascript實(shí)現(xiàn)日歷效果原理及代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
基于JavaScript開發(fā)一個(gè)有趣的分組抽簽小程序
在團(tuán)隊(duì)合作開發(fā)中,經(jīng)常需要將團(tuán)隊(duì)成員分組,來完成各自的任務(wù),而抽簽的方式自然是最公平、最簡(jiǎn)單的方法之一,所以本文就來開發(fā)一個(gè)有趣的分組抽簽小程序吧2023-05-05
Javascript實(shí)現(xiàn)DIV滾動(dòng)自動(dòng)滾動(dòng)到底部的代碼
一個(gè)比較特殊的客戶要求,在一個(gè)頁面用表格顯示數(shù)據(jù),數(shù)據(jù)量不是很多,不希望使用瀏覽器的滾動(dòng)條,只能在Div中滾動(dòng)table中的數(shù)據(jù),但是有個(gè)特殊的要求,就是必須將滾動(dòng)條自動(dòng)滾動(dòng)到底部2012-03-03
引用其它js時(shí)如何同時(shí)處理多個(gè)window.onload事件
引用其它js時(shí),其js卻使用了window.onload事件,這樣的話,引入的頁面的onload事件就有可能執(zhí)行不了,那么該如何解決呢?祥看下文2014-09-09
基于JavaScript實(shí)現(xiàn)瀑布流效果
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)瀑布流效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
如何獲取select下拉框的值(option沒有及有value屬性)
獲取select下拉框的值分為option沒有value屬性及有value屬性時(shí)的兩種情況,下面分別給出具體的實(shí)現(xiàn)代碼,需要的朋友可以參考下2013-11-11
javascript實(shí)現(xiàn)十六進(jìn)制顏色值(HEX)和RGB格式相互轉(zhuǎn)換
這篇文章主要介紹了javascript實(shí)現(xiàn)十六進(jìn)制顏色值(HEX)和RGB格式之間的轉(zhuǎn)換,使用正則的方法實(shí)現(xiàn)RGB顏色轉(zhuǎn)換為16進(jìn)制,需要的朋友可以參考下2014-06-06

