微信小程序?qū)崿F(xiàn)左右聯(lián)動(dòng)的實(shí)戰(zhàn)記錄
前言
最近在學(xué)習(xí)小程序,實(shí)現(xiàn)了左右聯(lián)動(dòng)的功能,記錄一下思緒,方便以后參考。
最終的界面如下, 點(diǎn)擊左邊任意一個(gè)項(xiàng)目,右邊會(huì)跳到相應(yīng)項(xiàng)目的起始位置,右邊滑動(dòng),左則會(huì)跳到相應(yīng)的位置。

實(shí)現(xiàn)思路
在這里,右則每一項(xiàng)的高度都是固定的,方便定位當(dāng)前滑動(dòng)距離在哪一個(gè)大項(xiàng)(左則)里。右則的 scroll-view 使用了一項(xiàng)關(guān)鍵的屬性:scroll-into-view,這個(gè)屬性用來(lái)確定 scrollTop 的值的,當(dāng) scroll-into-view 的值和 scroll-view 里面的元素的id的值相等時(shí),scroll-view 會(huì)定位到該元素,scrollTop 的值就是滑動(dòng)到該元素的值。
做這個(gè)功能的時(shí)候,遇到一個(gè)問(wèn)題,就是右則的小項(xiàng)種類不多的時(shí)候,例如某個(gè)類目只有1~2個(gè),那么點(diǎn)擊左則的大項(xiàng)的時(shí)候,會(huì)出現(xiàn)點(diǎn)擊不到的現(xiàn)象。這里可以用點(diǎn)小技巧來(lái)解決:
點(diǎn)擊左則大項(xiàng)的時(shí)候,設(shè)置當(dāng)前點(diǎn)擊標(biāo)記為true,設(shè)置 classifySeleted 為當(dāng)前點(diǎn)擊的項(xiàng)目。 然后在滑動(dòng)觸發(fā)函數(shù)(onGoodsScroll)里面,判斷當(dāng)前觸發(fā)滑動(dòng)是否點(diǎn)擊產(chǎn)生的,如果是,則不設(shè)置 classifySeleted 的值,否則就計(jì)算 classifySeleted 的值并設(shè)置。
示例代碼:
wxml代碼如下:
<view class="content-container">
<scroll-view class="classify-container" scroll-y="true">
<view class="classify {{classifySeleted==classify.typeId?'active':''}}" wx:for="{{cakeTypes}}" wx:for-item="classify" wx:key="id" data-id="{{classify.typeId}}" bindtap="tapClassify">
<view class="name">{{classify.typeName}}</view>
</view>
</scroll-view>
<scroll-view class="goods-container" scroll-y="true" scroll-into-view="{{'inToView' + typeIndex}}" bindscroll="onGoodsScroll" scroll-top="{{scrollTop}}">
<view wx:for="{{cakeTypes}}" wx:for-item="classify" wx:key="id">
<view class="title" id="{{'inToView'+classify.typeId}}">{{classify.typeName}}</view>
<view class="goods" wx:for="{{classify.productIds}}" wx:for-item="cake" wx:key="id">
<image class="pic" src="{{cake.imgSrc}}" data-src="{{cake.imgSrc}}" data-list="{{cake.imgSrc}}" bindtap="tapImg"></image>
<view class="name ellipsis">{{cake.name}}</view>
<view class="sold">{{cake.introduce}}</view>
<view class="price">¥{{cake.price}}</view>
</view>
</view>
</scroll-view>
</view>
js代碼如下:
onGoodsScroll: function (e) {
var scrollTop = e.detail.scrollTop;
var h = 0;
var classifySeleted = this.data.classifySeleted;
var titleHeight = Math.ceil(70 * this.data.percent);
var itemHeight = Math.ceil(180 * this.data.percent);
this.data.cakeTypes.forEach(function (classify, i) {
console.log("h:" + h + " scrollTop:" + scrollTop);
var _h = titleHeight + classify.productIds.length * itemHeight;
if (scrollTop >= h - 10) {
classifySeleted = classify.typeId;
}
h += _h;
});
if (this.data.isTap) {
this.setData ({
isTap: false
})
} else {
this.setData({
classifySeleted: classifySeleted
});
}
},
tapClassify: function (e) {
var id = e.target.dataset.id;
this.setData({
isTap: true,
classifySeleted: id,
typeIndex: id
});
},
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- 微信小程序?qū)崿F(xiàn)聯(lián)動(dòng)菜單
- 微信小程序?qū)崿F(xiàn)菜單左右聯(lián)動(dòng)
- 微信小程序scroll-view實(shí)現(xiàn)左右聯(lián)動(dòng)效果
- 微信小程序?qū)崿F(xiàn)左右聯(lián)動(dòng)
- 微信小程序scroll-view實(shí)現(xiàn)左右聯(lián)動(dòng)
- 微信小程序?qū)崿F(xiàn)購(gòu)物頁(yè)面左右聯(lián)動(dòng)
- 微信小程序?qū)崿F(xiàn)左右列表聯(lián)動(dòng)
- 微信小程序?qū)崿F(xiàn)菜單左右聯(lián)動(dòng)效果
相關(guān)文章
Angular2仿照微信UI實(shí)現(xiàn)9張圖片上傳和預(yù)覽的示例代碼
本篇文章主要介紹了Angular2仿照微信UI實(shí)現(xiàn)9張圖片上傳和預(yù)覽的示例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10
AngularJS 入門(mén)教程之HTML DOM實(shí)例詳解
本文主要介紹AngularJS HTML DOM,這里幫大家整理了詳細(xì)的資料,并附實(shí)例代碼詳細(xì)講解,有需要的小伙伴可以參考下2016-07-07
AngularJS折疊菜單實(shí)現(xiàn)方法示例
這篇文章主要介紹了AngularJS折疊菜單實(shí)現(xiàn)方法,結(jié)合完整實(shí)例形式分析了AngularJS實(shí)現(xiàn)折疊菜單的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
angular過(guò)濾器實(shí)現(xiàn)排序功能
這篇文章主要為大家詳細(xì)介紹了angular過(guò)濾器實(shí)現(xiàn)排序功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
angularjs實(shí)現(xiàn)搜索的關(guān)鍵字在正文中高亮出來(lái)
這篇文章主要介紹了angularjs實(shí)現(xiàn)搜索的關(guān)鍵字在正文中高亮出來(lái),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
詳解angularJs中關(guān)于ng-class的三種使用方式說(shuō)明
本篇文章主要介紹了angularJs中關(guān)于ng-class的三種使用方式說(shuō)明,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Angular實(shí)現(xiàn)的敏感文字自動(dòng)過(guò)濾與提示功能示例
這篇文章主要介紹了Angular實(shí)現(xiàn)的敏感文字自動(dòng)過(guò)濾與提示功能,結(jié)合實(shí)例形式分析了AngularJS針對(duì)字符串的輸入判定及實(shí)時(shí)顯示相關(guān)操作技巧,需要的朋友可以參考下2017-12-12

