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

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.dtcms.com/a/354422.html

相关文章:

  • LightGBM 在金融逾期天数预测任务中的经验总结
  • 2025年渗透测试面试题总结-36(题目+回答)
  • 2025年渗透测试面试题总结-37(题目+回答)
  • vue3 数据库 内的 字符 显示 换行符
  • LeetCode-238除自身以外数组的乘积
  • 基于单片机步进电机控制电机正反转加减速系统Proteus仿真(含全部资料)
  • codeforces(1045)(div2) E. Power Boxes
  • 2024年09月 Python(三级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • Kubernetes 的20 个核心命令分类详解
  • 深度学习11 Deep Reinforcement Learning
  • 基于视觉的网页浏览Langraph Agent
  • 【RAG知识库实践】向量数据库VectorDB
  • Linux应用软件编程---网络编程(TCP并发服务器构建:[ 多进程、多线程、select ])
  • Spring Start Here 读书笔记:第15 章 Testing your Spring app
  • 【PyTorch】基于YOLO的多目标检测项目(二)
  • vue2 watch 的使用
  • Xshell 自动化脚本大赛技术文章大纲
  • TypeScript:重载函数
  • 《Linux 网络编程四:TCP 并发服务器:构建模式、原理及关键技术(select )》
  • oceanbase-部署
  • yolo ultralytics之yolov8.yaml文件简介
  • 《信息检索与论文写作》实验报告三 中文期刊文献检索
  • Linux 云服务器内存不足如何优化
  • LinuxC系统多线程程序设计
  • C语言:数据在内存中的存储
  • nginx referer-policy 和 referer
  • redis集群分片策略
  • 【温室气体数据集】NOAA CCGG 飞机观测温室气体
  • 2025年06月 Python(三级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • spring-cloud项目中gateway配置解析