效果预览图
第一步 新建项目
先创建一个新项目 web-phone,然后安装 vue-router,路由是一定要安装的,因为我们要通过路由来实现应用切换。
第二步 配置路由
首先我们要新建一个路由组件 PhoneContainer 然后在这个组件的页面,有个 Box 组件,这个 Box 组件里面呢则装着一个 Phone 组件。根据上面的描述,我们需要创建3个 .vue 文件,然后路由组件放在 pages 目录,非路由组件放在 components 目录,
然后,我们在 router 目录下的 index.js 文件中进行路由的配置
import { createRouter, createWebHistory } from 'vue-router'
const routes = [
{ path: "/", redirect: "/phone-container" },
{
path: "/phone-container",
name: "phoneContainer",
component: () => import("../pages/PhoneContainer.vue"),
redirect:'/phone-container/frontPage',
children: [
{
path: 'frontPage', // 首页 即手机桌面
name: 'FrontPage',
component:()=> import('../components/PhonePages/FrontPage.vue')
},
{
path: 'dds', // 打地鼠应用
name: 'DDS',
component:()=> import('../components/PhonePages/DDS.vue')
}
]
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
export default router
第三步 页面结构
先把 App.vue 的 template 标签中的内容全部删掉,写上一句
<template>
<router-view />
</template>
然后,去到 PhoneContainer 组件中写下
<template>
<div class="phone-container">
<!-- <Config /> -->
<Box />
</div>
</template>
<script>
import Box from '../components/Box.vue';
export default {
name: 'PhoneContainer', // 手机容器
components: {
Box
}
};
</script>
<style scoped>
.phone-container {
display: flex;
justify-content: center;
align-items: center;
height: calc(100% - 20px);
padding: 10px;
}
</style>
这里面,除了放个 Box 组件,我打算后面再放上个配置台,可以用来在页面上调节手机盒子的颜色,手机边框的颜色,手机壁纸,手机APP列表行列数,Home键样式等等。先预留位置。
再然后,去到 Box 组件中写下
<template>
<div class="box">
<Phone />
</div>
</template>
<script>
import Phone from './Phone.vue'
export default {
name: 'TheBox', // 手机盒
components: {
Phone
}
};
</script>
<style scoped>
.box {
width: 40%;
height: 95%;
background: red;
display: flex;
justify-content: center;
align-items: center;
padding: 10px;
margin: 0 10px;
border: 5px solid black;
}
</style>
建议大家从一开始就确定好平板的尺寸,包括装平板的盒子的尺寸,然后统一使用px做为单位,我这里都用了百分比做单位,后面样式变形真的好难受,我直接躺平不改了。
最后,在Phone 组件中写下
<template>
<div class="phone">
<!-- 屏幕 -->
<div class="screen">
<router-view v-slot="{ Component }">
<!-- 为后面的切换动画做准备 -->
<!-- <keep-alive> -->
<transition>
<component :is="Component" />
</transition>
<!-- </keep-alive> -->
</router-view>
</div>
<!-- home键 -->
<div class="home">
<div class="home-btn" @click="$router.push({ name: 'FrontPage' })"></div>
</div>
</div>
</template>
<script>
import FrontPage from './PhonePages/FrontPage.vue';
export default {
name: 'ThePhone', // 手机
components: {
FrontPage
}
};
</script>
<style scoped>
.phone {
height: 97%;
width: 450px;
border-radius: 5px;
padding: 5px;
background: whitesmoke;
cursor: pointer;
}
/* 屏幕样式 */
.screen {
height: 90%;
position: relative;
background: url(../assets/bg.jpg) no-repeat fixed center center;
}
/* home键样式 */
.home {
height: calc(10% - 5px);
margin-top: 5px;
display: flex;
justify-content: center;
align-items: center;
}
.home-btn {
background: white;
border-radius: 50%;
height: 40px;
width: 40px;
box-shadow: 0px 0px 2px black;
}
.home-btn:hover {
box-shadow: inset 0px 0px 4px 1px black;
}
</style>
我们把 Phone 组件分为两部分,一部分是 screen 屏幕部分,用来显示和切换APP的,一部分是Home键,因为切换应用实际上就是替换掉屏幕中的内容,所以我们把 router-view 写在 screen 中,又因为默认的路由都是直接切换的,没有动画效果显得有点生硬,但是我又不会,所以先留个切换动画的位置,后面学会了再补上。
.screen 的背景图就是平板的壁纸。
第四步 应用组件
上面的步骤已经把一台黑屏的平板搭建好了,接下来我们要做的就是给这个平台编写各种应用组件了,第一个肯定得是桌面组件,也就是首页。
在 components 目录下,新建一个 PhonePages 目录,里面专门用来存放应用组件,然后我们新建一个 FrontPage.vue(首页) 和 DDS.vue (打地鼠),这两个组件我们上面已经在路由里配置过了。然后开始编写首页的代码。
FrontPage.vue
<template>
<!-- 状态栏 -->
<StatusBar />
<!-- 应用列表 -->
<div class="app-list">
<template v-for="app in appList" :key="app.name">
<div class="app-item" @click="$router.push({ name: app.routeName })">
<div class="app-icon">
<el-image style="width: 40px; height: 40px" :src="app.icon" fit="contain" />
</div>
<div class="app-name">{{ app.name }}</div>
</div>
</template>
</div>
<!-- 底部导航栏 -->
<div class="tabbar">
<template v-for="app in tabbarAppList" :key="app.name">
<el-image style="width: 40px; height: 40px" :src="app.icon" fit="contain" />
</template>
</div>
</template>
<script setup>
import StatusBar from '../StatusBar.vue';
// 应用列表
const appList = [
{
name: '打地鼠',
icon: '../src/assets/appList/ds.webp',
routeName: 'DDS'
},
{
name: '网易云音乐',
icon: '../src/assets/appList/couldmusic.png'
},
{
name: '京东',
icon: '../src/assets/appList/jingdong.webp'
},
{
name: '掘金',
icon: '../src/assets/appList/juejin.webp'
},
{
name: 'Line',
icon: '../src/assets/appList/line.png'
},
{
name: 'QQ',
icon: '../src/assets/appList/qq.jpeg'
},
{
name: '淘宝',
icon: '../src/assets/appList/taobao.webp'
},
{
name: '企业微信',
icon: '../src/assets/appList/qywx.webp'
},
{
name: '微信',
icon: '../src/assets/appList/wechat.png'
},
{
name: '王者农药',
icon: '../src/assets/appList/wzry.png'
},
{
name: '小米运动',
icon: '../src/assets/appList/xiaomi.webp'
}
];
const tabbarAppList = [
{
name: 'QQ',
icon: '../src/assets/appList/qq.jpeg'
},
{
name: '微信',
icon: '../src/assets/appList/wechat.png'
},
{
name: '王者农药',
icon: '../src/assets/appList/wzry.png'
},
{
name: '小米运动',
icon: '../src/assets/appList/xiaomi.webp'
},
{
name: '掘金',
icon: '../src/assets/appList/juejin.webp'
}
];
</script>
<script>
export default {
name: 'FrontPage' // 首页
};
</script>
<style scoped>
/* 应用列表 */
.app-list {
color: white;
font-weight: bold;
display: flex;
flex-flow: row wrap;
justify-content: flex-start;
align-items: flex-start;
}
.app-list .app-item {
display: flex;
flex-flow: column wrap;
justify-content: center;
align-items: center;
padding: 10px;
width: calc(20% - 20px);
}
.app-name {
font-size: 12px;
}
.el-image:hover {
position: relative;
top: -5px;
}
/* 底部导航栏 */
.tabbar {
width: 92%;
height: 50px;
margin: 5px auto;
border-radius: 5px;
background: #fafafa;
position: absolute;
bottom: 5px;
left: 4%; /* 92+4+4=100 */
display: flex;
justify-content: space-around;
align-items: center;
}
.tabbar .el-image {
margin: 0 5px;
}
</style>
.app-list 要使用弹性布局,当然,更好的是栅格布局,我用弹性用的更顺手,所以没用栅格的。
const appList = [] 中的每个元素都应该有 name,icon,routeName 三个属性,分别是 应用名称,应用图标图片路径,应用组件的路由名称。
然后回到前面,给Home键加上一个点击就跳转到 FrontPage 组件的 click 事件,就实现Home键返回主页的功能了。
到这里,你已经拥有一台属于自己的平板啦,想要安装什么应用都可以把它抽离成应用组件安装进你的平板里面了。比如我的打地鼠,就是把我上一篇写的打地鼠给抽离了出来。
等一个有缘人让我能在平板上玩王者农药!!!