鸿蒙js开发9 <list>和<swiper>组件实现导航菜单的构建 原创

发布于 2021-1-29 00:52
浏览
0收藏

本文展示了通过<list>和<swiper>组件的搭配使用实现导航菜单的构建

视图及样式:

<div class="container">
    <!--左侧导航栏-->
    <div class="leftdiv">
        <list class="listdiv">
            <block for="{{leftmenus}}">
                <list-item class="{{currentIndex==$idx?'listitem1':'listitem0'}}" onclick="onclick({{$idx}})">
                    <text class="{{currentIndex==$idx?'text1':'text0'}}">{{$item}}</text>
                </list-item>
            </block>
        </list>
    </div>
    <!--右侧内容区-->
    <div class="rightdiv">
        <swiper id="swiperid" onchange="changeswiper" vertical="true" class="swiperdiv" indicator="false">
            <div class="box one">
                <div class="boxtopdiv">
                    <text class="boxtext">one title</text>
                </div>
                <div class="boxcontentdiv">
                    <text class="boxtext">one content</text>
                </div>
            </div>
            <div class="box two">
                <div class="boxtopdiv">
                    <text class="boxtext">two title</text>
                </div>
                <div class="boxcontentdiv">
                    <text class="boxtext">two content</text>
                </div>
            </div>
            <div class="box three">
                <div class="boxtopdiv">
                    <text class="boxtext">three title</text>
                </div>
                <div class="boxcontentdiv">
                    <text class="boxtext">three content</text>
                </div>
            </div>
            <div class="box four">
                <div class="boxtopdiv">
                    <text class="boxtext">four title</text>
                </div>
                <div class="boxcontentdiv">
                    <text class="boxtext">four content</text>
                </div>
            </div>
            <div class="box five">
                <div class="boxtopdiv">
                    <text class="boxtext">five title</text>
                </div>
                <div class="boxcontentdiv">
                    <text class="boxtext">five content</text>
                </div>
            </div>
            <div class="box six">
                <div class="boxtopdiv">
                    <text class="boxtext">six title</text>
                </div>
                <div class="boxcontentdiv">
                    <text class="boxtext">six content</text>
                </div>
            </div>
            <div class="box seven">
                <div class="boxtopdiv">
                    <text class="boxtext">seven title</text>
                </div>
                <div class="boxcontentdiv">
                    <text class="boxtext">seven content</text>
                </div>
            </div>
            <div class="box eight">
                <div class="boxtopdiv">
                    <text class="boxtext">eight title</text>
                </div>
                <div class="boxcontentdiv">
                    <text class="boxtext">eight content</text>
                </div>
            </div>
            <div class="box nine">
                <div class="boxtopdiv">
                    <text class="boxtext">nine title</text>
                </div>
                <div class="boxcontentdiv">
                    <text class="boxtext">nine content</text>
                </div>
            </div>
        </swiper>
    </div>
</div>
.container {
    width: 100%;
    height: 1300px;
    display: flex;
    flex-direction: row;
}
.leftdiv{
    width: 30%;
    height: 100%;
    border-right: 5px solid snow;
}
.listdiv{
    width: 100%;
    height: 100%;
    background-color: silver;
    display: flex;
}
.listitem0{
    width: 100%;
    height: 12%;
    border-bottom: 3px solid white;
    display: flex;
    justify-content: center;
    align-items: center;
}
.listitem1{
    width: 100%;
    height: 12%;
    border-bottom: 3px solid white;
    background-color: #3CB371;
    display: flex;
    justify-content: center;
    align-items: center;
}
.text0{
    font-size: 50px;
    color: white;
}
.text1{
    font-size: 50px;
    color: black;
    font-weight: bold;
}
.rightdiv{
    width: 69%;
    height: 100%;
}
.swiperdiv{
    width: 100%;
    height: 100%;
}
.box{
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
}
.one{
    background-color: skyblue;
}
.two{
    background-color: rebeccapurple;
}
.three{
    background-color: darksalmon;
}
.four{
    background-color: salmon;
}
.five{
    background-color: darkolivegreen;
}
.six{
    background-color: aquamarine;
}
.seven{
    background-color: chartreuse;
}
.eight{
    background-color: hotpink;
}
.nine{
    background-color: seagreen;
}
.boxtopdiv{
    width: 100%;
    height: 8%;
    border-bottom: 1px solid snow;
    display: flex;
    justify-content: center;
    align-items: center;
}
.boxtext{
    font-size: 50px;
    font-weight: bold;
}
.boxcontentdiv{
    width: 100%;
    height: 92%;
    display: flex;
    justify-content: center;
    align-items: center;
}

业务逻辑层:

import prompt from '@system.prompt';
export default {
    data: {
        leftmenus:["one","two","three","four","five","six","seven","eight","nine"],
        currentIndex:0
    },

    //选中左侧导航栏触发
    onclick(index){
        this.currentIndex=index;
        this.$element("swiperid").swipeTo({index:index});
    },

    //滚动右侧内容区触发
    changeswiper(data){
        prompt.showToast({
            message:"滚动项的下标为:"+data.index
        });

        this.currentIndex = data.index;
    }

}

效果图:

 

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
标签
4
收藏
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐