温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序如何实现菜单左右联动效果

发布时间:2022-07-18 09:20:08 阅读:188 作者:iii 栏目:开发技术
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

今天小编给大家分享一下微信小程序如何实现菜单左右联动效果的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

原理

首先是获取数据,并且获取数据的长度(需要根据长度来计算元素的高度),通过遍历数据的内容通过题目和元素个数的相加得到高度,当消失高度小于等于某个元素的高度就设定索引值给左边的菜单目录实现右边滑动左边联动,左边的菜单点击事件联动比较简单就不说

代码实现

wxml

<view class="menu">
        <view class="left-box">
            <scroll-view class="left_menu" scroll-with-animation  scroll-y="true" scroll-into-view="{{leftViewId}}">
                <text class="menu-item {{index==currentIndex?'menu-active':''}}" wx:for="{{navList}}" wx:key="this" data-id="menu{{index}}" data-i="{{index}}" bindtap="changeMenu">{{item.c_name}}</text>
            </scroll-view>
        </view>
        <view class="right-box">
            <scroll-view class="right_menu" scroll-y='true'  scroll-with-animation scroll-into-view="{{rightViewId}}"  bindscroll="getScroll" enable-flex>
                <view wx:for="{{navList}}" wx:key="this" class='pro-item' id="menu{{index}}">
                    <view class="right_menu_head">{{item.c_name}}</view>
                    <view class="list-box">
                        <view class="menu_list" wx:for-item='items' wx:for="{{item.list}}" wx:key="this">
                        <image src="{{items.url}}"></image>
                        <view>{{items.goodsName}}</view>
                    </view>
                </view>
                </view>
            </scroll-view>
        </view>
</view>

wxss,这里使用的是less语法,vscode插件可编译

.menu{
    .left-box{
        width180rpx;
        border-right1px solid #dfdfdf;
        position: fixed;
        left0;
        z-index10;
        top370rpx;
        box-sizing: border-box;
        height90%;
        background#f0f0f0;

        .menu-item{
            display: inline-block;
            width180rpx;
            height88rpx;
            font-size26rpx;
            line-height88rpx;
            background#fff;
            text-align: center;
            border-bottom1px solid #dfdfdf;
        }

        .menu-active{
            background#f0f0f0;
            border-left10rpx  solid #333999;
        }
    }
    
    .right-box{
        width74%;
        position: fixed;
        top360rpx;
        height77%;
        right0;
        border-left20rpx;
        box-sizing: border-box;
        margin-top20rpx;

        .right_menu{
            height100%;
            box-sizing: border-box;

            .pro-item{

                .right_menu_head{
                    height80rpx;
                    line-height80rpx;
                    font-size26rpx;
                    font-weight600;
                }

                &:last-child{
                    margin-bottom156rpx;
                }

                .list-box{
                    width100%;
                    // min-height380rpx;
                    display: flex;
                    flex-wrap: wrap;
                    background#fff;
                    box-sizing: border-box;
                    border-radius10rpx;
                    font-size20rpx;

                    .menu_list{
                        width33.3%;
                        font-size20rpx;
                        padding20rpx 0;
                        text-align: center;
                        font-style'微软雅黑';

                        image{
                            width100rpx;
                            height100rpx;
                        }

                        view{
                            color#333;
                            text-overflow: ellipsis;
                            white-space: nowrap;
                            overflow: hidden;
                          }
                    }
                }
            }
        }
    }
}

js文件

data:{
    navList:[..],//数据
    currentIndex: 0,//左边对应的索引
    rightViewId: '',//点击事件右边的索引
}

getScroll(e) {//微信小程序中绑定滑动函数,每滑动一下都会触发
        let top = e.detail.scrollTop,
            h = 0,
            _this = this;


        for (let i = 0; i < this.data.navList.length; i++) {
            let dishSize = this.data.navList[i].list.length;//获取数据对应展示商品的json
            h += 38 + parseInt(dishSize / 3 * 80);//获取当前元素的高度,38是标题高度,80是元素高度

            if (top <= h) {//满足条件立刻停止循环,就会一直停留再当前索引,不满足当前就会自动到下一个菜单
                _this.setData({
                    currentIndex: i
                })
                break;
            }

        }

    },
    changeMenu(e) {
        console.log(this.data.heightArr, this.data.topArr);

        this.setData({
            currentIndex: e.currentTarget.dataset.i,
            rightViewId: e.currentTarget.dataset.id
        })

    }

展示图片

微信小程序如何实现菜单左右联动效果

微信小程序如何实现菜单左右联动效果

以上就是“微信小程序如何实现菜单左右联动效果”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×