微信小程序?qū)崿F(xiàn)錨點(diǎn)跳轉(zhuǎn)
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)錨點(diǎn)跳轉(zhuǎn)的具體代碼,供大家參考,具體內(nèi)容如下
1、先上效果圖,看看是不是你想要的。
2、主要用到的微信小程序的scroll-view 組件實(shí)現(xiàn)該效果。核心主要是使用scroll-into-view屬性跳轉(zhuǎn)對(duì)應(yīng)的標(biāo)簽頁(yè)和標(biāo)簽內(nèi)容區(qū)域和bindscroll事件監(jiān)聽標(biāo)簽內(nèi)容區(qū)域距離頁(yè)面頂部的距離來(lái)判斷頂部的標(biāo)簽應(yīng)該處于哪個(gè)標(biāo)簽。
3、wxml代碼:
<!-- start 標(biāo)簽區(qū)域 --> <view class="text" style="height:{{height}}px " > <!-- scroll-into-view 可以跳轉(zhuǎn)到綁定值對(duì)應(yīng)的ID的標(biāo)簽區(qū)域 --> <scroll-view class='scroll-x-view1' scroll-x='true' scroll-into-view='{{indexMaodian}}'> <view bindtap="toDetail" data-id="a1" class='scroll-view-item1 bg_red' id="a" >1標(biāo)簽</view> <view bindtap="toDetail" data-id="b1" class='scroll-view-item1 bg_blue' id="b">2標(biāo)簽</view> <view bindtap="toDetail" data-id="c1" class='scroll-view-item1 bg_green' id="c">3標(biāo)簽</view> <view bindtap="toDetail" data-id="d1" class='scroll-view-item1 bg_yellow' id="d">4標(biāo)簽</view> <view bindtap="toDetail" data-id="e1" class='scroll-view-item1 bg_red' id="e">5標(biāo)簽</view> </scroll-view> <!-- end 標(biāo)簽區(qū)域 --> <!-- start 標(biāo)簽對(duì)應(yīng)內(nèi)容區(qū)域 --> <scroll-view class='scroll-x-view' scroll-y='true' bindscroll="onPageScroll" scroll-into-view='{{storeDetail}}'> <view class='scroll-view-item bg_red' id="a1">1標(biāo)簽對(duì)應(yīng)內(nèi)容區(qū)域</view> <view class='scroll-view-item bg_blue' id="b1">2標(biāo)簽對(duì)應(yīng)內(nèi)容區(qū)域</view> <view class='scroll-view-item bg_green' id="c1">3標(biāo)簽對(duì)應(yīng)內(nèi)容區(qū)域</view> <view class='scroll-view-item bg_yellow' id="d1">4標(biāo)簽對(duì)應(yīng)內(nèi)容區(qū)域</view> <view class='scroll-view-item bg_red' id="e1">5標(biāo)簽對(duì)應(yīng)內(nèi)容區(qū)域</view> </scroll-view> <!-- end 標(biāo)簽對(duì)應(yīng)內(nèi)容區(qū)域 --> </view>
4、wxss代碼:
.text{ width: 100%; display: flex; flex-direction: column; } .scroll-x-view { width: 100%; display: flex; flex: 1; } .scroll-x-view .scroll-view-item { margin-top: 50rpx; width: 750rpx; height: 800rpx; display:inline-block; } .bg_red { background: lightpink; } .bg_blue { background: lightblue; } .bg_green { background: lightgreen; } .bg_yellow { background: lightyellow; } .scroll-x-view1 { width: 100%; height: 100rpx; white-space: nowrap; } .scroll-x-view1 .scroll-view-item1 { width: 400rpx; height: 100rpx; display:inline-block; }
5、js代碼:
Page({ /** * 頁(yè)面的初始數(shù)據(jù) */ data: { // 標(biāo)簽錨點(diǎn)跳轉(zhuǎn)值 indexMaodian: 'a', // 標(biāo)簽詳情內(nèi)容錨點(diǎn)跳轉(zhuǎn) storeDetail: 'a1' }, // 監(jiān)聽頁(yè)面滑動(dòng)距離 onPageScroll(e) { // 通過(guò)滑動(dòng)的距離判斷頁(yè)面滑動(dòng)那個(gè)區(qū)域讓后讓頂部的標(biāo)簽欄切換到對(duì)應(yīng)位置 var height = Number(e.detail.scrollTop) if (height <= 400) { // 滑到1區(qū)域 this.setData({ indexMaodian: 'a' }); } else if (height > 400 && height<= 800) { // 滑到2區(qū)域 this.setData({ indexMaodian: 'b' }); } else if (height > 800 && height <= 1200) { // 滑到3區(qū)域 this.setData({ indexMaodian: 'c' }); } else if (height > 1200 && height <= 1600) { // 滑到4區(qū)域 后面難得寫了,以此類推即可 this.setData({ indexMaodian: 'd' }); } }, // 跳轉(zhuǎn)到對(duì)應(yīng)的標(biāo)簽詳情內(nèi)容區(qū) toDetail(e) { let id = e.target.dataset.id this.setData({ storeDetail: id }) }, /** * 生命周期函數(shù)--監(jiān)聽頁(yè)面加載 */ onLoad: function (options) { var systemInfo = wx.getSystemInfoSync(); var windowHeight = systemInfo.windowHeight; // 拿到導(dǎo)航欄以下可視區(qū)域的高度 this.setData({ height: windowHeight }); }, /** * 生命周期函數(shù)--監(jiān)聽頁(yè)面初次渲染完成 */ onReady: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁(yè)面顯示 */ onShow: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁(yè)面隱藏 */ onHide: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁(yè)面卸載 */ onUnload: function () { }, /** * 頁(yè)面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動(dòng)作 */ onPullDownRefresh: function () { }, /** * 頁(yè)面上拉觸底事件的處理函數(shù) */ onReachBottom: function () { }, /** * 用戶點(diǎn)擊右上角分享 */ onShareAppMessage: function () { } })
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序?qū)崿F(xiàn)錨點(diǎn)定位功能的方法實(shí)例
- 微信小程序scroll-view實(shí)現(xiàn)滾動(dòng)到錨點(diǎn)左側(cè)導(dǎo)航欄點(diǎn)餐功能(點(diǎn)擊種類,滾動(dòng)到錨點(diǎn))
- 微信小程序 scroll-view 實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)功能
- 微信小程序scroll-view錨點(diǎn)鏈接滾動(dòng)跳轉(zhuǎn)功能
- 微信小程序?qū)崿F(xiàn)錨點(diǎn)功能
- 小程序?qū)崿F(xiàn)錨點(diǎn)滑動(dòng)效果
- 微信小程序 scroll-view實(shí)現(xiàn)錨點(diǎn)滑動(dòng)的示例
- 微信小程序?qū)崿F(xiàn)錨點(diǎn)定位樓層跳躍的實(shí)例
- 微信小程序商城分類滾動(dòng)列表錨點(diǎn)的項(xiàng)目實(shí)踐
相關(guān)文章
JS onmousemove鼠標(biāo)移動(dòng)坐標(biāo)接龍DIV效果實(shí)例
這篇文章主要介紹了JS onmousemove鼠標(biāo)移動(dòng)坐標(biāo)接龍DIV效果實(shí)例,有需要的朋友可以參考一下2013-12-12javascript讀取Xml文件做一個(gè)二級(jí)聯(lián)動(dòng)菜單示例
這篇文章主要介紹了使用javascript中讀取Xml文件做成的一個(gè)二級(jí)聯(lián)動(dòng)菜單,需要的朋友可以參考下2014-03-03javascript-TreeView父子聯(lián)動(dòng)效果保持節(jié)點(diǎn)狀態(tài)一致
javascript-TreeView父子聯(lián)動(dòng)效果保持節(jié)點(diǎn)狀態(tài)一致...2007-08-08js實(shí)現(xiàn)按Ctrl+Enter發(fā)送效果
按Ctrl+Enter發(fā)送,思路是監(jiān)聽textarea的onkeydown事件,當(dāng)ctrl鍵被按下,并且,keycode為13(回車),時(shí),調(diào)用發(fā)送表單的函數(shù)2014-09-09js設(shè)置默認(rèn)時(shí)間跨度過(guò)程詳解
這篇文章主要介紹了js設(shè)置默認(rèn)時(shí)間跨度過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07