一、概覽
實(shí)現(xiàn)效果如下:
二、項(xiàng)目環(huán)境
1、nodejs版本
node -v
v16.16.0
2、npm版本
npm -v
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
8.15.0
3、vue腳手架版本
vue -V
@vue/cli 5.0.8
三、創(chuàng)建vue項(xiàng)目
1、創(chuàng)建名為vuetest的項(xiàng)目
vue create vuetest
選擇Default([Vue2] babel,eslint)?
?
2、切換到項(xiàng)目目錄,啟動(dòng)項(xiàng)目
cd vuetest
npm run serve
?
3、使用瀏覽器預(yù)覽?
http://localhost:8080/
四、使用Visual Studio Code打開(kāi)項(xiàng)目
1、查看源碼
2、安裝element-ui
官網(wǎng)https://element.eleme.cn/?
npm安裝
npm i element-ui -S
3、在main.js中引用安裝好的element-ui
4、查看element-ui官網(wǎng),使用Container 布局容器方便快速搭建頁(yè)面的基本結(jié)構(gòu)
?
選擇常見(jiàn)頁(yè)面結(jié)構(gòu)樣式 :
<el-container>
<el-header>Header</el-header>
<el-container>
<el-aside width="200px">Aside</el-aside>
<el-container>
<el-main>Main</el-main>
<el-footer>Footer</el-footer>
</el-container>
</el-container>
</el-container>
修改App.vue文件:
<template>
<div id="app">
<el-container>
<el-header>Header</el-header>
<el-container>
<el-aside width="200px">Aside</el-aside>
<el-container>
<el-main>Main</el-main>
<el-footer>Footer</el-footer>
</el-container>
</el-container>
</el-container>
</div>
</template>
<script>
// import HelloWorld from './components/HelloWorld.vue'
export default {
name: 'App',
components: {
// HelloWorld
}
}
</script>
<style>
.el-header, .el-footer {
background-color: #B3C0D1;
color: #333;
text-align: center;
line-height: 60px;
}
.el-aside {
background-color: #D3DCE6;
color: #333;
text-align: center;
line-height: 200px;
}
.el-main {
background-color: #E9EEF3;
color: #333;
text-align: center;
line-height: 160px;
}
</style>
啟動(dòng)項(xiàng)目,預(yù)覽效果:
npm run serve
訪問(wèn)http://localhost:8080/此時(shí)發(fā)現(xiàn)結(jié)構(gòu)并沒(méi)有撐滿,在assets中新建一個(gè)css文件夾,新建global.css文件,并在main.js中引用
html,
body,
#app{
margin: 0;
padding: 0;
height: 100%;
width: 100%;
}
在App.vue文件中外層el-container中添加class="container"并設(shè)置高度為100%?
此時(shí)頁(yè)面顯示正常了!
五、實(shí)現(xiàn)el-aside側(cè)邊欄收縮與展開(kāi)效果
1、修改App.vue文件
<template>
<div id="app">
<el-container class="container">
<el-header>Header</el-header>
<el-container>
<el-aside class="aside_main" :class="{aside_main_show:!asideStatus}">Aside</el-aside>
<el-container>
<el-main class="main_cont">
<!-- aside側(cè)邊欄按鈕 -->
<div class="aside_open_close" @click="asidechange">
<i class="el-icon-arrow-left" v-if="aside_open_close"></i>
<i class="el-icon-arrow-right" v-else></i>
</div>
</el-main>
<el-footer>Footer</el-footer>
</el-container>
</el-container>
</el-container>
</div>
</template>
2、添加按鈕點(diǎn)擊事件?
export default {
name: 'App',
components: {
// HelloWorld
},
data(){
return{
asideStatus:false,
aside_open_close:false,
}
},
methods:{
// 側(cè)邊欄收縮與展開(kāi)
asidechange(){
this.asideStatus = !this.asideStatus
if(this.asideStatus){
setTimeout(()=>{
this.aside_open_close =true
},500)
}else{
setTimeout(()=>{
this.aside_open_close =false
},500)
}
}
}
}
3、修改樣式?
<style>
.container{
height: 100%;
}
.el-header, .el-footer {
background-color: #B3C0D1;
color: #333;
text-align: center;
line-height: 60px;
}
/* .el-aside {
background-color: #D3DCE6;
color: #333;
text-align: center;
line-height: 200px;
} */
/* 側(cè)邊欄樣式 */
.aside_main{
width: 200px !important;
transition: width 0.5s;
}
.aside_main_show{
width: 0px !important;
}
/* .el-main {
background-color: #E9EEF3;
color: #333;
text-align: center;
line-height: 160px;
} */
.main_cont{
position: relative;
margin: 0;
padding: 0;
background-color: #E9EEF3;
}
/* 側(cè)邊欄按鈕樣式 */
.aside_open_close{
position: absolute;
left: 0;
top: 50%;
width: 16px;
height: 60px;
line-height: 60px;
color: #fff;
background-color: #2A333A;
border-radius: 0 6px 6px 0;
font-size: 20px;
z-index: 1000;
cursor: pointer;
}
.aside_open_close:hover{
background-color: #FF8E2B;
color: #fff;
}
</style>
4、預(yù)覽效果
完結(jié)?。?!文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-421608.html
源碼下載:https://download.csdn.net/download/im_api/87457462文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-421608.html
到了這里,關(guān)于vue2+element-ui,el-aside側(cè)邊欄容器收縮與展開(kāi)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!