微信小程序 左侧滑动导航栏

博客围绕微信小程序展开,介绍了左侧滑动导航栏相关内容,涉及wxml、wxss和js方面。

左侧滑动导航栏如图

wxml

<!-- 左侧滚动栏 -->
<view class='under_line'></view>
<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="lists" wx:for="{{lists}}">
        <view bindtap='jumpIndex' data-menuindex='{{index}}'>
          <view class='text-style'>
            <text class="{{indexId==index?'active1':''}}">{{item}}</text>
            <text class="{{indexId==index?'active':''}}"></text>
          </view>
        </view>
      </block>
    </view>
  </scroll-view>
</view>

wxss

.under_line{
  width: 100%;
  border-top: 1rpx solid #efefef;
}
.scrollY {
  width: 200rpx;
  position: fixed;
  left: 0;
  top: 0;
  border-right: 1rpx solid #efefef;
}

.left {
  border-top: 1rpx solid #efefef;
  border-right: 1rpx solid #efefef;
}

.text-style {
  width: 200rpx;
  height: 140rpx;
  line-height: 140rpx;
  text-align: center;
  font-size: 34rpx;
  font-family: PingFangSC-Semibold;
  color: rgba(51, 51, 51, 1);
}

.active1 {
  color: #85d1fd;
}

.active {
  display: block;
  width: 50rpx;
  height: 6rpx;
  background: #85d1fd;
  position: relative;
  left: 75rpx;
  bottom: 30rpx;
}

js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists: [
      "标题1", "标题二", "标题三", "标题四", "标题五", "标题六", "标题七", "标题八", "标题九", "标题十", "标题十一", "标题十二"
    ],
    indexId: 0,
  },
  // 左侧点击事件
  jumpIndex(e) {
    let index = e.currentTarget.dataset.menuindex
    let that = this
    that.setData({
      indexId: index
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          winHeight: res.windowHeight
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})

 

<think>根据用户需求,用户希望在微信小程序中实现左侧导航栏选中时更换图片的效果。结合提供的引用内容,我们可以通过以下步骤实现: 1. 使用`wx:for`循环渲染导航栏选项 2. 为每个选项绑定点击事件`bindtap` 3. 在数据中记录当前选中的选项索引 4. 根据当前选中状态动态切换图片路径 具体实现如下: **WXML文件代码示例:** ```html <view class="container"> <!-- 左侧导航栏 --> <view class="left-nav"> <block wx:for="{{navList}}" wx:key="index"> <view class="nav-item {{index === currentIndex ? 'active' : ''}}" bindtap="switchNav" data-index="{{index}}" > <!-- 根据选中状态切换图片 --> <image src="{{index === currentIndex ? item.selectedIcon : item.icon}}" /> <text>{{item.title}}</text> </view> </block> </view> <!-- 右侧内容区域 --> <view class="content"> <!-- 根据currentIndex切换显示内容 --> </view> </view> ``` **JS文件代码示例:** ```javascript Page({ data: { currentIndex: 0, // 当前选中索引 navList: [ { title: "首页", icon: "/images/home.png", // 默认图标 selectedIcon: "/images/home-active.png" // 选中图标 }, { title: "分类", icon: "/images/category.png", selectedIcon: "/images/category-active.png" }, // ...其他导航项 ] }, // 切换导航 switchNav(e) { const index = e.currentTarget.dataset.index; this.setData({ currentIndex: index }); } }); ``` **WXSS样式示例:** ```css .left-nav { width: 25%; background-color: #f9f9f9; } .nav-item { padding: 20rpx; text-align: center; border-bottom: 1rpx solid #eee; } .nav-item.active { background-color: #fff; /* 选中背景色 */ } .nav-item image { width: 40rpx; height: 40rpx; margin-bottom: 10rpx; } ``` ### 实现说明: 1. **数据驱动**:通过`navList`数组定义导航项,每个项包含`title`、默认图标`icon`和选中图标`selectedIcon` 2. **状态判断**:使用`index === currentIndex`判断当前项是否被选中 3. **动态切换图片**:在`<image>`组件中通过三元表达式切换图片路径 4. **点击事件**:点击时通过`switchNav`方法更新`currentIndex`,触发视图更新 ### 效果优化: - 可添加CSS过渡动画使切换更平滑 - 结合引用[2]中的滑动效果,可增加侧滑手势切换功能 - 使用`hover-class`增加点击态效果(如点击时短暂改变背景色) §§相关问题§§ 1. 如何在小程序中实现导航栏滑动切换效果? 2. 微信小程序中如何优化长列表导航栏的性能? 3. 除了更换图片,还有哪些导航栏选中状态的样式设计方法?</think>### 微信小程序实现左侧导航栏选中时更换图片的方案 要实现左侧导航栏选中时更换图片的效果,核心是通过数据绑定动态切换图片路径,结合点击事件更新选中状态。以下是完整实现方案: #### 1. WXML文件(结构层) ```html <view class="container"> <!-- 左侧导航栏 --> <scroll-view class="left-nav" scroll-y> <block wx:for="{{navList}}" wx:key="index"> <view class="nav-item {{index === currentIndex ? 'active' : ''}}" bindtap="switchNav" data-index="{{index}}" > <!-- 动态切换图片 --> <image src="{{index === currentIndex ? item.selectedIcon : item.icon}}" class="nav-icon" /> <text>{{item.title}}</text> </view> </block> </scroll-view> <!-- 右侧内容区 --> <view class="right-content"> <!-- 根据currentIndex显示对应内容 --> {{navList[currentIndex].content}} </view> </view> ``` #### 2. JS文件(逻辑层) ```javascript Page({ data: { currentIndex: 0, // 当前选中索引 navList: [ { title: "首页", icon: "/images/home.png", // 默认图标 selectedIcon: "/images/home-active.png", // 选中图标 content: "首页内容" }, { title: "分类", icon: "/images/category.png", selectedIcon: "/images/category-active.png", content: "分类内容" }, { title: "购物车", icon: "/images/cart.png", selectedIcon: "/images/cart-active.png", content: "购物车内容" } ] }, // 切换导航 switchNav(e) { const index = e.currentTarget.dataset.index this.setData({ currentIndex: index }) } }) ``` #### 3. WXSS文件(样式层) ```css .container { display: flex; height: 100vh; } .left-nav { width: 25%; background-color: #f9f9f9; } .nav-item { padding: 20rpx; text-align: center; border-bottom: 1rpx solid #eee; transition: all 0.3s; } .nav-item.active { background-color: #fff; border-left: 6rpx solid #07c160; } .nav-icon { width: 40rpx; height: 40rpx; margin-bottom: 10rpx; display: block; margin: 0 auto 10rpx; } .right-content { flex: 1; padding: 20rpx; } ``` ### 实现原理说明 1. **数据驱动视图**: - 在`navList`数组中为每个导航项设置两个图标路径:默认状态`icon`和选中状态`selectedIcon` - 通过`currentIndex`标识当前选中的导航项索引 2. **动态切换图片**: - 使用三元表达式判断显示哪个图标: ```html src="{{index === currentIndex ? item.selectedIcon : item.icon}}" ``` - 当项目索引与当前选中索引匹配时显示选中图标,否则显示默认图标 3. **点击交互**: - `bindtap`绑定`switchNav`事件处理函数 - 通过`data-index`传递点击项的索引值 - 点击后更新`currentIndex`触发视图重新渲染 4. **视觉反馈**: - 添加`.active`类实现背景色变化和左侧边框指示 - 使用CSS过渡动画增强交互体验 ### 优化建议 1. **添加动画效果**: ```css .nav-icon { transition: transform 0.3s; } .nav-item.active .nav-icon { transform: scale(1.1); } ``` 2. **使用字体图标替代图片**(更轻量): ```html <text class="iconfont {{index === currentIndex ? 'icon-home-fill' : 'icon-home'}}"></text> ``` 3. **结合滑动效果**(参考引用[2]): ```javascript // 在JS中添加滑动切换支持 onSwiper(e) { const direction = e.detail.current; this.setData({ currentIndex: direction }) } ```
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值