uniapp 仿企微左边公司切换页

示例代码:

html 复制代码
<template>
  <view class="container">
    <!-- 遮罩层 -->
    <view class="mask" v-if="showSidebar" @click="closeSidebar"></view>
    
    <!-- 侧边栏 -->
    <view class="sidebar" :class="{ active: showSidebar }">
      <!-- 用户信息区域 -->
      <view class="user-info">
        <image class="avatar" src="/static/default-avatar.png"></image>
        <view class="user-details">
          <text class="username">用户名</text>
          <text class="company">公司名称</text>
        </view>
      </view>
      
      <!-- 菜单列表 -->
      <view class="sidebar-content">
        <view class="sidebar-item">
          <text class="iconfont icon-message"></text>
          <text class="item-text">消息</text>
        </view>
        <view class="sidebar-item">
          <text class="iconfont icon-contacts"></text>
          <text class="item-text">通讯录</text>
        </view>
        <view class="sidebar-item">
          <text class="iconfont icon-workbench"></text>
          <text class="item-text">工作台</text>
        </view>
        <view class="sidebar-item">
          <text class="iconfont icon-profile"></text>
          <text class="item-text">我的</text>
        </view>
      </view>
    </view>
    
    <!-- 主内容区域包装器 -->
    <view class="main-content" :class="{ 'content-shifted': showSidebar }">
      <!-- 主页面内容 -->
      <button @click="openSidebar" class="menu-button">
        <u-icon name="list" size="28"></u-icon>
      </button>
      
      <!-- 这里可以放置其他主页面内容 -->
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      showSidebar: false
    }
  },
  methods: {
    openSidebar() {
      this.showSidebar = true
    },
    closeSidebar() {
      this.showSidebar = false
    }
  }
}
</script>

<style>
.container {
  position: relative;
  width: 100%;
  height: 100vh;
}

.mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.3);
  z-index: 998;
}

/* 主内容区域样式 */
.main-content {
  position: relative;
  min-height: 100vh;
  background-color: #fff;
  transition: transform 0.3s ease-in-out; /* 确保与sidebar使用相同的过渡时间 */
  z-index: 997;
}

.content-shifted {
  transform: translateX(70%);
}

/* 修改sidebar的样式 */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: 70%;
  height: 100vh;
  background-color: #2f2f2f;
  z-index: 999;
  transition: transform 0.3s ease-in-out; /* 确保与main-content使用相同的过渡时间 */
  color: #fff;
  transform: translateX(-100%);
}

.sidebar.active {
  transform: translateX(0);
}

.user-info {
  padding: 20px;
  background-color: #393939;
  display: flex;
  align-items: center;
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 6px;
  margin-right: 15px;
}

.user-details {
  flex: 1;
}

.username {
  font-size: 18px;
  font-weight: 500;
  margin-bottom: 5px;
  display: block;
}

.company {
  font-size: 14px;
  color: #999;
  display: block;
}

.sidebar-content {
  padding: 10px 0;
}

.sidebar-item {
  padding: 16px 20px;
  display: flex;
  align-items: center;
}

.sidebar-item:active {
  background-color: #393939;
}

.iconfont {
  font-size: 24px;
  margin-right: 12px;
}

.item-text {
  font-size: 16px;
}

.menu-button {
  position: absolute;
  top: 15px;
  left: 15px;
  background: none;
  border: none;
  padding: 10px;
  z-index: 997;
}

.menu-button::after {
  border: none;
}

/* 注意:需要引入iconfont字体文件 */
@font-face {
  font-family: 'iconfont';
  src: url('/static/iconfont.ttf') format('truetype');
}

.iconfont {
  font-family: 'iconfont';
}
</style>

效果展示:

相关推荐
程序员黑豆3 小时前
Java类型推断完全指南:从var到菱形运算符,掌握使用限制与最佳实践
java·前端·ai编程
To_OC3 小时前
踩了个 TS 的坑之后,我终于把 type 和 interface 掰明白了
前端·react.js·typescript
GreenTea4 小时前
深度解读 Anthropic 多智能体报告:更强的模型 ≠ 更好的协调
前端·后端·算法
浮生望4 小时前
前端API工程化:用 Mock 数据与 Axios 配置实现独立于后端的并行开发
前端
万少4 小时前
给 DeepSeek Harness 装个"应用商店":一条命令,595 个插件随你逛
前端·javascript·后端
波波0075 小时前
C# 15 重磅新特性: 带标签 break 与 continue:重新定义嵌套循环控制流
服务器·前端·c#
Brown.alexis5 小时前
es6知识点1-自备使用
前端·ecmascript·es6
梦想的旅途26 小时前
企业微信 API 二次开发:AI 智能体与外部群业务流闭环
人工智能·microsoft·企业微信
小爬的老粉丝6 小时前
JavaScript 纯前端预览 WPS:先识别容器,再路由解析器
前端·javascript·wps
计算机魔术师7 小时前
新兴多智能体系统的模式与问题
前端