当前位置: 首页 > backend >正文

vue布局

给于2个div块状元素的布局

方案1:横向并排(Flex Row)

<template><div class="container"><div class="background">背景</div><div class="panel">内容</div></div>
</template><script>
export default { name: 'FlexRowLayout' };
</script><style scoped>
.container {display: flex;height: 100vh;
}
.background {flex: 1;background: #74ebd5;
}
.panel {flex: 1;background: #fff;display: flex;justify-content: center;align-items: center;
}
</style>

方案2:纵向堆叠(Flex Column)

<template><div class="container"><div class="background">背景</div><div class="panel">内容</div></div>
</template><script>
export default { name: 'FlexColumnLayout' };
</script><style scoped>
.container {display: flex;flex-direction: column;height: 100vh;
}
.background {flex: 1;background: #9face6;
}
.panel {flex: 1;background: #fff;display: flex;justify-content: center;align-items: center;
}
</style>

方案3:层叠覆盖(绝对定位)

<template><div class="container"><div class="background"></div><div class="panel">内容</div></div>
</template><script>
export default { name: 'OverlayLayout' };
</script><style scoped>
.container {position: relative;width: 100vw;height: 100vh;
}
.background {position: absolute;width: 100%;height: 100%;background: #74ebd5;z-index: 0;
}
.panel {position: absolute;width: 400px;height: 300px;background: white;top: 50%;left: 50%;transform: translate(-50%, -50%);z-index: 1;box-shadow: 0 0 20px rgba(0,0,0,0.2);
}
</style>

方案4:左右对齐(float)

<template><div class="container"><div class="background">背景</div><div class="panel">内容</div></div>
</template><script>
export default { name: 'FloatLayout' };
</script><style scoped>
.container {height: 100vh;
}
.background {float: left;width: 50%;height: 100%;background: #74ebd5;
}
.panel {float: right;width: 50%;height: 100%;background: #fff;display: flex;justify-content: center;align-items: center;
}
</style>

方案5:Grid 网格布局

<template><div class="container"><div class="background">背景</div><div class="panel">内容</div></div>
</template><script>
export default { name: 'GridLayout' };
</script><style scoped>
.container {display: grid;grid-template-columns: 1fr 1fr;height: 100vh;
}
.background {background: #74ebd5;
}
.panel {background: #fff;display: flex;justify-content: center;align-items: center;
}
</style>

方案6:固定浮动按钮 + 居中内容

<template><div class="container"><div class="background"></div><div class="panel">内容</div><button class="float-btn" @click="toggle">切换</button></div>
</template><script>
export default {name: 'FloatingButtonLayout',data() {return { center: true };},methods: {toggle() {this.center = !this.center;}}
};
</script><style scoped>
.container {position: relative;width: 100vw;height: 100vh;
}
.background {position: absolute;width: 100%;height: 100%;background: #9face6;z-index: 0;
}
.panel {position: absolute;width: 400px;height: 300px;background: white;z-index: 1;transition: all 0.3s ease;top: 50%;left: 50%;transform: translate(-50%, -50%);
}
.container:not(.center) .panel {left: 60px;top: 50%;transform: translateY(-50%);
}
.float-btn {position: fixed;top: 20px;right: 20px;z-index: 999;
}
</style>

方案 7:卡片悬浮 + 背景渐变

效果:背景是渐变色,内容块居中悬浮,带阴影和圆角。

<template><div class="container"><div class="background"></div><div class="card">欢迎回来!</div></div>
</template><script>
export default { name: 'CardFloatLayout' };
</script><style scoped>
.container {position: relative;width: 100vw;height: 100vh;
}
.background {position: absolute;width: 100%;height: 100%;background: linear-gradient(135deg, #667eea, #764ba2);
}
.card {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);width: 360px;padding: 40px;background: white;border-radius: 12px;box-shadow: 0 20px 40px rgba(0,0,0,0.3);
}
</style>

方案 8:背景视频 + 登录框

效果:背景是自动播放的视频,登录框居中覆盖。

<template><div class="container"><video class="background" autoplay muted loop><source src="your-video.mp4" type="video/mp4" /></video><div class="panel">登录</div></div>
</template><script>
export default { name: 'VideoBackgroundLayout' };
</script><style scoped>
.container {position: relative;width: 100vw;height: 100vh;overflow: hidden;
}
.background {position: absolute;width: 100%;height: 100%;object-fit: cover;
}
.panel {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);background: rgba(255,255,255,0.9);padding: 30px;border-radius: 10px;box-shadow: 0 0 20px rgba(0,0,0,0.3);
}
</style>

方案 9:左右滑动切换布局(登录/注册)

效果:两个块分别是登录和注册界面,通过按钮滑动切换。

<template><div class="container"><div class="slider" :class="{ active: isRegister }"><div class="panel login">登录</div><div class="panel register">注册</div></div><button class="toggle-btn" @click="isRegister = !isRegister">{{ isRegister ? '去登录' : '去注册' }}</button></div>
</template><script>
export default {name: 'SlideSwitchLayout',data() {return { isRegister: false };}
};
</script><style scoped>
.container {position: relative;width: 100vw;height: 100vh;overflow: hidden;
}
.slider {display: flex;width: 200%;height: 100%;transition: transform 0.5s ease;
}
.slider.active {transform: translateX(-50%);
}
.panel {width: 50%;display: flex;justify-content: center;align-items: center;font-size: 24px;background: #f0f0f0;
}
.login {background: #74ebd5;
}
.register {background: #9face6;
}
.toggle-btn {position: fixed;bottom: 30px;right: 30px;padding: 10px 20px;
}
</style>

点击右下按钮会转到去注册

方案 10:响应式折叠布局(移动端优化)

效果:大屏幕左右并排,小屏幕上下堆叠。

<template><div class="container"><div class="background">背景</div><div class="panel">内容</div></div>
</template><script>
export default { name: 'ResponsiveLayout' };
</script><style scoped>
.container {display: flex;flex-direction: row;height: 100vh;
}
.background, .panel {flex: 1;display: flex;justify-content: center;align-items: center;
}
.background {background: #74ebd5;
}
.panel {background: #fff;
}
@media (max-width: 768px) {.container {flex-direction: column;}
}
</style>

方案 11:分屏滑动布局(左右滑入)

效果:两个块初始隐藏,页面加载时从左右滑入,制造动态分屏感。

<template><div class="container"><div class="left" :class="{ active: show }">左侧内容</div><div class="right" :class="{ active: show }">右侧内容</div></div>
</template><script>
export default {name: 'SplitSlideLayout',data() {return { show: false };},mounted() {setTimeout(() => (this.show = true), 300);}
};
</script><style scoped>
.container {position: relative;width: 100vw;height: 100vh;overflow: hidden;
}
.left, .right {position: absolute;width: 50%;height: 100%;top: 0;transition: transform 0.6s ease;display: flex;justify-content: center;align-items: center;font-size: 24px;color: white;
}
.left {left: 0;background: #667eea;transform: translateX(-100%);
}
.right {right: 0;background: #764ba2;transform: translateX(100%);
}
.left.active {transform: translateX(0);
}
.right.active {transform: translateX(0);
}
</style>

方案 12:登录框拖拽布局

效果:用户可以拖动登录框在页面任意位置,增强交互性。

<template><div class="container"><div class="background"></div><divclass="panel":style="{ top: y + 'px', left: x + 'px' }"@mousedown="startDrag">拖我试试</div></div>
</template><script>
export default {name: 'DraggableLogin',data() {return { x: 200, y: 200, dragging: false, offsetX: 0, offsetY: 0 };},methods: {startDrag(e) {this.dragging = true;this.offsetX = e.clientX - this.x;this.offsetY = e.clientY - this.y;document.addEventListener('mousemove', this.onDrag);document.addEventListener('mouseup', this.stopDrag);},onDrag(e) {if (this.dragging) {this.x = e.clientX - this.offsetX;this.y = e.clientY - this.offsetY;}},stopDrag() {this.dragging = false;document.removeEventListener('mousemove', this.onDrag);document.removeEventListener('mouseup', this.stopDrag);}}
};
</script><style scoped>
.container {position: relative;width: 100vw;height: 100vh;
}
.background {position: absolute;width: 100%;height: 100%;background: #9face6;
}
.panel {position: absolute;width: 300px;height: 200px;background: white;border-radius: 8px;box-shadow: 0 0 20px rgba(0,0,0,0.3);cursor: move;display: flex;justify-content: center;align-items: center;
}
</style>

方案 13:背景渐变切换 + 登录框淡入

效果:背景颜色渐变切换,登录框淡入,适合欢迎页或品牌展示。

<template><div class="container" :class="bgClass"><div class="panel">欢迎回来</div></div>
</template><script>
export default {name: 'GradientSwitchLayout',data() {return { bgClass: 'bg1' };},mounted() {setInterval(() => {this.bgClass = this.bgClass === 'bg1' ? 'bg2' : 'bg1';}, 3000);}
};
</script><style scoped>
.container {position: relative;width: 100vw;height: 100vh;transition: background 1s ease;
}
.bg1 {background: linear-gradient(to right, #ff9a9e, #fad0c4);
}
.bg2 {background: linear-gradient(to right, #a1c4fd, #c2e9fb);
}
.panel {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);background: white;padding: 40px;border-radius: 10px;box-shadow: 0 0 30px rgba(0,0,0,0.2);animation: fadeIn 1s ease;
}
@keyframes fadeIn {from { opacity: 0; transform: translate(-50%, -60%); }to { opacity: 1; transform: translate(-50%, -50%); }
}
</style>

方案 14:左右互换布局方案(Float 或 Flex)

✅ 效果说明

  • 两个块并排显示:左边是 A,右边是 B。

  • 点击按钮后,A 和 B 互换位置。

  • 可用于登录页、信息展示页、对比页等场景。

💡 Vue 2 示例代码

<template><div class="container"><div :class="leftClass">左侧内容</div><div :class="rightClass">右侧内容</div><button class="switch-btn" @click="swap">切换位置</button></div>
</template><script>
export default {name: 'SwapLayout',data() {return {swapped: false};},computed: {leftClass() {return this.swapped ? 'block right' : 'block left';},rightClass() {return this.swapped ? 'block left' : 'block right';}},methods: {swap() {this.swapped = !this.swapped;}}
};
</script><style scoped>
.container {position: relative;width: 100vw;height: 100vh;
}
.block {position: absolute;width: 50%;height: 100%;display: flex;justify-content: center;align-items: center;font-size: 24px;transition: all 0.4s ease;
}
.left {left: 0;background: #74ebd5;
}
.right {right: 0;background: #9face6;
}
.switch-btn {position: fixed;bottom: 30px;right: 30px;padding: 10px 20px;z-index: 999;
}
</style>

🧠 技术要点

  • 使用 position: absolute + left/right 控制位置。

  • computed 动态切换类名实现互换。

  • 加上 transition 实现平滑过渡。

🚀 可扩展方向

  • 加入动画:滑动、淡入淡出、翻转。

  • 内容块支持 slot 或组件嵌套。

  • 响应式适配:小屏幕下上下堆叠。

  • 多种布局模式切换:并排、覆盖、居中。

http://www.xdnf.cn/news/18942.html

相关文章:

  • 架构设计——云原生与分布式系统架构
  • Android中设置RecyclerView滑动到指定条目位置
  • 搜维尔科技核心产品矩阵涵盖从硬件感知到软件渲染的全产品供应链
  • 万博智云联合华为云共建高度自动化的云容灾基线解决方案
  • 【Python开源环境】Anaconda/Miniconda
  • 【数据结构与算法】(LeetCode)141.环形链表 142.环形链表Ⅱ
  • 重置 Windows Server 2019 管理员账户密码
  • 深入理解QLabel:Qt中的文本与图像显示控件
  • 国产的服务器
  • 机器学习回顾(一)
  • Day16_【机器学习—KNN算法】
  • 小白入门:支持深度学习的视觉数据库管理系统
  • 解构与重构:“真人不露相,露相非真人” 的存在论新解 —— 论 “真在” 的行为表达本质
  • c++ 观察者模式 订阅发布架构
  • Visual Scope (Serial_Digital_Scope V2) “串口 + 虚拟示波器” 工具使用记录
  • JavaScript中的BOM,DOM和事件
  • Spring Boot 实战:接入 DeepSeek API 实现问卷文本优化
  • 底层音频编程的基本术语 PCM 和 Mixer
  • 数据分析学习笔记4:加州房价预测
  • 腕上智慧健康管家:华为WATCH 5与小艺的智美生活新范式
  • 音频转PCM
  • curl、python-requests、postman和jmeter的对应关系
  • AR培训系统:油气行业的安全与效率革新
  • frp 一个高性能的反向代理服务
  • PAT 1086 Tree Traversals Again
  • SpringBoot项目使用Liquibase 数据库版本管理
  • C#编译错误:CS1056 意外字符
  • vsgCs显示谷歌全球倾斜模型-节点
  • 第八章:《性能优化技巧》——深入讲解预分配容量、移动语义、避免频繁拼接等优化策略,以及C++17的`string_view`如何减少拷贝开
  • vxetable数据导出