微信小程序:菜单栏

菜单栏的主要实现方式就是嵌套view,做好每一层的view的设计。

要注意的是在放图片的时候记得要把图片嵌套在view里面,然后把图片的wxss设置为width:100%,height:100%。

设计页面的时候可以先给每个区设置background-color和border,这样可以最直观的看到你设计的效果对不对,最后再把这两个样式删掉就可以了。

这里做的主要是中间:动漫、电影、综艺、直播的菜单栏导航。

是在js的data里面把每个菜单的图片、名字都设置成一个对象,然后存在一系列数组里面,最后循环遍历出来

效果图:

 

one.js

// pages/forview/forview.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
    
      topimg:"../images/logo.jpg",
      links:[{"text":"动漫","url":"a1","img":"../images/dm.png"},
      {"text":"电影","url":"a2","img":"../images/dy.png"},
      {"text":"综艺","url":"a3","img":"../images/zy.png"},
      {"text":"直播","url":"a4","img":"../images/zb.png"}]
  
    },
  
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
  
    },
  
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
  
    },
  
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
  
    },
  
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
  
    },
  
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
  
    }
  })

one.json

{
  "usingComponents": {}
}

one.wxml

<!--pages/forview/forview.wxml-->
<view  class="firstview">
   <view  class="topimg">
      <image  src="{{topimg}}"  class="cimg"></image>
   </view>
   <view  class="menuview">
      <block  wx:for="{{links}}">
         <view  class="boxview">
            <view class="imgview">
              <image src="{{item.img}}"  class="cimg"></image>
            </view>
            <view  class="txtview">
              <view>{{item.text}}</view>
            </view>
         </view>
      </block>
   </view>
   <view class="buttomview">
        <view class="box">
            <image src="../images/jr1.jpg" class="cimg"></image>
        </view>
        <view class="box">
            <image src="../images/jr2.jpg" class="cimg"></image>
        </view>
        <view class="box">
            <image src="../images/jr3.jpg" class="cimg"></image>
        </view>
        <view class="box">
            <image src="../images/jr4.jpg" class="cimg"></image>
        </view>
   </view>
</view>

one.wxss

/* pages/forview/forview.wxss */
.firstview{
    width: 100%;
    height: 100vh;
    background-color: 	#F5F5F5;
  }
  .topimg{
    width: 100%;
    height: 35%;
  }
  .cimg{
    width: 100%;
    height: 100%;
  }
  .menuview{
    width: 100%;
    height: 14%;
    border-bottom: 1px solid  paleturquoise;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .boxview{
    width: 20%;
    height: 80%;
    /**border: 1px  solid  black;**/
    margin: 4px;
    display: flex;
    flex-direction: column;
    align-items: center;
  
  }
  .imgview{
    width: 50%;
    height: 45%;
    /**border: 1px  solid  black;**/
  }
  .txtview{
    width: 90%;
    height: 45%;
   /** border: 1px  solid  black;**/
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 12px;
    font-weight: bold;
  }
  .buttomview{
      width:100%;
      height: 50%;
      background-color: skyblue;
      display: flex;
      flex-wrap: wrap;
      align-content: center;
      justify-items: center;
  }
  .box{
    width: 48%;
    height: 48%;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 2px;
  }
  .cimg{
      widows: 100%;
      height: 100%;
  }

  • 4
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现微信小程序中的左滑菜单列表,你可以使用 `<swiper>` 和 `<swiper-item>` 组件结合手势事件来实现。 以下是一个简单的示例代码,演示如何实现左滑菜单列表: ```html <view class="container"> <swiper class="swiper" bindchange="swiperChange" bindtouchstart="swiperTouchStart" bindtouchend="swiperTouchEnd"> <block wx:for="{{list}}" wx:key="index"> <swiper-item class="swiper-item"> <view class="content">{{item}}</view> <view class="menu">菜单</view> </swiper-item> </block> </swiper> </view> ``` ```css .container { height: 100vh; } .swiper { width: 100%; height: 100%; } .swiper-item { position: relative; height: 100%; } .content { width: 100%; height: 100%; background-color: #f5f5f5; } .menu { position: absolute; top: 0; right: -60px; width: 60px; height: 100%; background-color: #ff0000; color: #ffffff; } ``` ```javascript Page({ data: { list: ['第一项', '第二项', '第三项'] }, swiperChange(event) { console.log('当前项索引', event.detail.current); }, swiperTouchStart(event) { this.startX = event.changedTouches[0].clientX; }, swiperTouchEnd(event) { const endX = event.changedTouches[0].clientX; const deltaX = endX - this.startX; if (deltaX < -50) { console.log('向左滑动'); } else if (deltaX > 50) { console.log('向右滑动'); } } }) ``` 在上面的示例中,使用了 `<swiper>` 和 `<swiper-item>` 组件来展示一个列表项。每个列表项包含两个部分:内容区域和菜单区域。内容区域使用 `<view>` 组件,并设置了背景颜色。菜单区域也使用 `<view>` 组件,并设置了背景颜色和位置。通过设置菜单区域的 `right` 属性为负值,使其隐藏在内容区域的右侧。 通过绑定 `bindtouchstart` 和 `bindtouchend` 手势事件来监听用户的滑动操作。在 `bindtouchstart` 事件中记录起始触摸点的横坐标,而在 `bindtouchend` 事件中计算滑动的横向偏移量。根据偏移量的正负值,可以判断用户是向左滑动还是向右滑动。 你可以根据自己的需求进行样式和交互的调整,以适应实际场景。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值