网页侧边弹出栏的简单实现(在当前窗口而不是固定位置)

1.首先我们创建主页面和侧边弹出框

<div>
    <button @click="showSidebar">Open Sidebar</button>
    <div class="main-content">
      <!-- 主页面 -->
    </div>
    <div class="sidebar" :class="{ 'sidebar-open': sidebarVisible }">
      <!-- 侧边栏 -->
      <button @click="hideSidebar">Close Sidebar</button>
    </div>
  </div>

2.设置css样式

第一步:为了使侧边栏在当前窗口内弹出我们将侧边栏的position设置为fixed。

第二步:使用css的transform translateX()属性将侧边栏放在窗口外。

.sidebar {
  position: fixed;
  top: 0;
  left: -100%; /* 将侧边栏放在窗口外 */
  width: 300px;
  height: 100%;
  background-color: #ccc;
  transition: transform 0.3s ease-in-out; /* 添加过渡效果 */
}
.sidebar-open {
  transform: translateX(100%); /* 将侧边栏移回窗口内 */
}

3.侧边栏的弹出和关闭

通过给侧边栏动态添加class来控制,在方法中我们改变sidebarVisible的值,为true时显示,为false则隐藏

data() {
    return {
      sidebarVisible: false,
    };
  },
  methods: {
    showSidebar() {
      this.sidebarVisible = true;
    },
    hideSidebar() {
      this.sidebarVisible = false;
    },
  },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值