温馨提示×

温馨提示×

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

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

微信小程序如何实现滑动侧边栏

发布时间:2022-07-18 09:18:21 来源:亿速云 阅读:168 作者:iii 栏目:开发技术

这篇文章主要介绍“微信小程序如何实现滑动侧边栏”,在日常操作中,相信很多人在微信小程序如何实现滑动侧边栏问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”微信小程序如何实现滑动侧边栏”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

效果图:

微信小程序如何实现滑动侧边栏

手指向右滑动可以显示侧边栏,向左滑动隐藏侧边栏

代码附上:.wxml

<view class="title">
      <image  class='headPortrait' src='../../images/1.jpg'></image>  
             <text>赵国伟</text>  
            </view>
            <view class="nav_left_items {{click == 1 ? 'active' : ''}}" 
          bindtap="switchRightTab"  data-id="1" >
                <image  class='icon' src='../../images/get.png'></image>
                <view>收件箱
            </view>
            </view>
            <view class="nav_left_items {{click == 2 ? 'active' : ''}}" 
          bindtap="switchRightTab"  data-id="2">
                <image   class='icon' src='../../images/send.png'></image>
                <view>发件箱
            </view>
            </view>
            <view class="nav_left_items {{click == 3 ? 'active' : ''}}" 
          bindtap="switchRightTab"  data-id="3">
                <image   class='icon' src='../../images/write.jpg'></image>
                <view >写信件
            </view>
        </view>
</scroll-view>

.js文件

 tap_ch: function(e) {
    if (this.data.open) {
        this.setData({
            open: false
        });
    } else {
        this.setData({
            open: true
        });
    }
},
tap_start: function(e) {
    // touchstart事件
    // 把手指触摸屏幕的那一个点的 x 轴坐标赋值给 mark 和 newmark
    this.data.mark = this.data.newmark = e.touches[0].pageX;
},

tap_drag: function(e) {
    // touchmove事件
    this.data.newmark = e.touches[0].pageX;
   
    // 手指从左向右移动
    if (this.data.mark < this.data.newmark) {
        this.istoright = true;
    }
    
    // 手指从右向左移动
    if (this.data.mark > this.data.newmark) {
        this.istoright = false;
    }
    this.data.mark = this.data.newmark;
},

tap_end: function(e) {
    // touchend事件
    this.data.mark = 0;
    this.data.newmark = 0;
    // 通过改变 opne 的值,让主页加上滑动的样式
    if (this.istoright) {
        this.setData({
            open: true
        });
    } else {
        this.setData({
            open: false
        });
    }
},

.wxss文件

.nav_left{
    width:25%;
    height:100%;
    background:#F2F2F2;
    text-align:center;
    position:absolute;
    top:0;
    left:0;
  }
  .nav_left .nav_left_items{
      display: flex;
    height:40px;
    line-height:40px;
    font-size:28rpx;
  }
  .nav_left .nav_left_items.active{
      display: flex;
    background: #fff;  /* 背景色变成白色 */  
  color: #3385ff;    /* 字体编程蓝色 */
  border-left: 3px solid #3385ff;  /* 设置边框的宽度以及颜色 */
  }
.title{
    margin-top: 10px;
}
.icon{
    display: flex;
    justify-content: center;
    align-items: center;
    width:20px;
    height: 20px;
    margin: 0px 3px;
    border-radius: 5px ;
    margin-top:10px ; 
}
.headPortrait{
    width:28px;
    height: 28px;
    border-radius: 20px;
}
.page-slidebar {
  height: 100%;
  width: 750rpx;
  position: fixed;
  background-color:white;
  z-index: 0;
}

到此,关于“微信小程序如何实现滑动侧边栏”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

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

AI