微信小程序scroll-view實(shí)現(xiàn)滾動(dòng)到錨點(diǎn)左側(cè)導(dǎo)航欄點(diǎn)餐功能(點(diǎn)擊種類(lèi),滾動(dòng)到錨點(diǎn))

1.wxml代碼:
<view class="page">
<import src="../../components/catering-item/catering-item.wxml" />
<!-- 左側(cè)滾動(dòng)欄 -->
<view class='under_line'></view>
<view class="body">
<view style='float: left' class='left'>
<scroll-view scroll-y scroll-with-animation scroll-left="{{scrollLength}}" class='scrollY' style='height: {{winHeight}}px'>
<view class='all clear'>
<block wx:key="tabs" wx:for="{{tabs}}">
<view bindtap='jumpIndex' data-menuindex='{{index}}'data-anchor='{{item.anchor}}'>
<view class="text-style {{indexId==index?' activeView':''}}">
<text class="{{indexId==index?'active1':''}}">{{item.title}}</text>
</view>
</view>
</block>
</view>
</scroll-view>
</view>
<view class="right" style='height: {{winHeight}}px'>
<scroll-view scroll-y scroll-with-animation scroll-left="{{scrollLength}}" bindscroll="scrollToLeft" scroll-into-view="{{toTitle}}" class='scrollRight' style='height: {{winHeight}}px'>
<block wx:key="tabs" wx:for="{{tabs}}">
<view id="view-{{item.anchor}}">
<view class="title" id="title-{{item.anchor}}">{{item.title}}</view>
<view class="orders" wx:for="{{tabsList[item.anchor]}}">
<template is="cateringItem" data="{{...item}}" />
</view>
</view>
</block>
</scroll-view>
</view>
</view>
</view>
2.wxss代碼:
@import "../../components/catering-item/catering-item.wxss";
/* pages/catering.wxss */
.page {
display: flex;
flex-direction: column;
width: 100%;
/* background: #F7F4F8; */
background-image: linear-gradient(90deg, #FCFCFC 0%, #FCFCFC 99%);
/* padding-top: 16px; */
}
.under_line{
width: 100%;
border-top: 1rpx solid #efefef;
}
::-webkit-scrollbar{
width: 0;
height: 0;
color: transparent;
}
.body{
display: flex;
width: 100%;
}
.scrollY {
width: 200rpx;
/* position: fixed;
left: 0;
top: 0; */
background: #F5F5F5;
/* border-right: 1rpx solid #efefef; */
}
/* scrollRight{
flex: 1;
} */
.right{
flex: 1;
/* height: 200rpx; */
/* background: #00FF00; */
}
.left {
border-top: 1rpx solid #efefef;
border-right: 1rpx solid #efefef;
}
.text-style {
width: 200rpx;
height: 100rpx;
line-height: 100rpx;
text-align: center;
font-size: 28rpx;
font-family: PingFangSC-Semibold;
color: rgba(51, 51, 51, 1);
}
.active1 {
color: #E5D1A9;
/* background: #FFF; */
}
.activeView{
background: #FFF;
}
.active {
display: block;
width: 50rpx;
height: 6rpx;
background: #E5D1A9;
position: relative;
left: 75rpx;
bottom: 30rpx;
}
.title{
margin-left: 32rpx;
padding-top: 16rpx;
font-size: 28rpx;
/* padding-bottom: 16rpx; */
}
3.js代碼
// pages/catering.js
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
tabs: [
{ title: '特惠', anchor: 'a', },
{ title: '必點(diǎn)', anchor: 'b', },
{ title: '營(yíng)養(yǎng)湯', anchor: 'c', },
{ title: '主食', anchor: 'd', },
{ title: '套餐', anchor: 'e', },
{ title: '飲料', anchor: 'f', },
],
tabsList: {
a: [{
price: 10.1, anchor: "a", index: 0, num: 0
}, {
price: 10.2, anchor: "a", index: 1, num: 0
},
{
price: 10.3, anchor: "a", index: 2, num: 0
},],
b: [{
price: 10.4, anchor: "b", index: 0, num: 0
}, {
price: 10.5, anchor: "b", index: 1, num: 0
},
{
price: 10.6, anchor: "b", index: 2, num: 0
},],
c: [{
price: 10.7, anchor: "c", index: 0, num: 0
}, {
price: 10.8, anchor: "c", index: 1, num: 0
},
{
price: 10.9, anchor: "c", index: 2, num: 0
},],
d: [{
price: 11.0, anchor: "d", index: 0, num: 0
}, {
price: 11.1, anchor: "d", index: 1, num: 0
},
{
price: 11.2, anchor: "d", index: 2, num: 0
},],
e: [{
price: 11.3, anchor: "e", index: 0, num: 0
}, {
price: 11.4, anchor: "e", index: 1, num: 0
},
{
price: 11.5, anchor: "e", index: 2, num: 0
},],
f: [{
price: 11.6, anchor: "f", index: 0, num: 0
}, {
price: 11.7, anchor: "f", index: 1, num: 0
},
{
price: 11.8, anchor: "f", index: 2, num: 0
},]
},
indexId: 0,
toTitle:"title-c",
scrollTop:0,
top:[],
},
// 左側(cè)點(diǎn)擊事件
jumpIndex(e) {
let index = e.currentTarget.dataset.menuindex;
let anchor = e.currentTarget.dataset.anchor;
let that = this
that.setData({
indexId: index,
toTitle: "title-" + anchor
});
//可以設(shè)置定位事件
},
scrollToLeft(res){
console.log("scrollToLeft-res:" + JSON.stringify(res) + JSON.stringify(this.data.top));
// let top=res.detail.scrollTop;
this.setData({
scrollTop: res.detail.scrollTop
})
var length = this.data.top.length;
for(var i=0;i<this.data.top.length;i++){
if (this.data.top[i] - this.data.top[0] <= this.data.scrollTop && (i < length - 1 && this.data.top[i + 1] - this.data.top[0] > this.data.scrollTop)){
if(this.data.indexId!=i){
this.setData({
indexId: i,
});
}
}
}
// console.log("top:"+top);
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
*/
onLoad: function (options) {
var that = this
wx.getSystemInfo({
success: function (res) {
that.setData({
winHeight: res.windowHeight
});
var top2=new Array();
for(var i=0;i<that.data.tabs.length;i++){
wx.createSelectorQuery().select('#view-' + that.data.tabs[i].anchor).boundingClientRect(function (rect) {
var isTop=Number(rect.top);
top2.push(isTop);
console.log("view-c:" + JSON.stringify(rect));
}).exec();
}
that.setData({
top: top2
});
}
});
},
})
說(shuō)明:
wxml中的template是菜品的item,可根據(jù)自己的需求進(jìn)行定義。
使用到scroll-view的scroll-into-view屬性用于對(duì)左側(cè)菜單種類(lèi)點(diǎn)擊定位到右側(cè)菜單的具體位置,js中的jumpIndex為用戶點(diǎn)擊左側(cè)菜單,對(duì)應(yīng)選中位置改變,和對(duì)右側(cè)菜單進(jìn)行定位。
js中scrollToLeft用于實(shí)現(xiàn)用戶滾動(dòng)右側(cè)菜單,對(duì)左側(cè)菜單分類(lèi)進(jìn)行定位操作,主要思想是將右側(cè)菜單中的種類(lèi)標(biāo)簽的top位置記錄下來(lái),當(dāng)右側(cè)scroll-view滑動(dòng)的位置小于等于某一個(gè)top,而大于下一個(gè)top時(shí),則更換左側(cè)種類(lèi)菜單到指定位置。
總結(jié)
到此這篇關(guān)于微信小程序scroll-view實(shí)現(xiàn)滾動(dòng)到錨點(diǎn)左側(cè)導(dǎo)航欄點(diǎn)餐功能(點(diǎn)擊種類(lèi),滾動(dòng)到錨點(diǎn))的文章就介紹到這了,更多相關(guān)微信小程序 scroll-view實(shí)現(xiàn)滾動(dòng)到錨點(diǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 微信小程序?qū)崿F(xiàn)錨點(diǎn)定位功能的方法實(shí)例
- 微信小程序?qū)崿F(xiàn)錨點(diǎn)跳轉(zhuǎ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í)例
- 微信小程序商城分類(lèi)滾動(dòng)列表錨點(diǎn)的項(xiàng)目實(shí)踐
相關(guān)文章
BootStrap創(chuàng)建響應(yīng)式導(dǎo)航條實(shí)例代碼
這篇文章主要介紹了BootStrap創(chuàng)建響應(yīng)式導(dǎo)航條實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-05-05
javascript 實(shí)現(xiàn)文本使用省略號(hào)替代(超出固定高度的情況)
這篇文章主要介紹了javascript 實(shí)現(xiàn)文本使用省略號(hào)替代(超出固定高度的情況)的相關(guān)資料,需要的朋友可以參考下2017-02-02
javascript AutoScroller 函數(shù)類(lèi)
javascript AutoScroller 自動(dòng)滾動(dòng)類(lèi)代碼,學(xué)習(xí)類(lèi)的朋友可以參考下。2009-05-05
IE event.srcElement和FF event.target 功能比較
可以捕獲當(dāng)前事件作用的對(duì)象,如event.srcElement.tagName可以捕獲活動(dòng)標(biāo)記名稱(chēng)。2010-03-03
實(shí)例詳解JSON數(shù)據(jù)格式及json格式數(shù)據(jù)域字符串相互轉(zhuǎn)換
JSON(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式。JSON采用完全獨(dú)立于語(yǔ)言的文本格式,這些特性使JSON成為理想的數(shù)據(jù)交換語(yǔ)言2016-01-01
新手快速入門(mén)JavaScript裝飾者模式與AOP
這篇文章主要介紹了新手快速入門(mén)JavaScript裝飾者模式與AOP,在不改變對(duì)象)的情況下動(dòng)態(tài)的為其添加功能,這就是裝飾者模式,下面小編帶大家來(lái)深入學(xué)習(xí)一下吧2019-06-06
JS利用?clip-path?實(shí)現(xiàn)動(dòng)態(tài)區(qū)域裁剪功能
這篇文章主要介紹了JS利用?clip-path?實(shí)現(xiàn)動(dòng)態(tài)區(qū)域裁剪功能,文中主要通過(guò)使用 box-shadow 實(shí)現(xiàn),代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12

