VueDay07样式(绑定HTML class)内联样式和菜单栏的一个简单实现
一:内联样式:
1.内联样式变量拼接
2.内联样式放置对象
3.CSS数组方式拼接
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/vue.js" type="text/javascript"></script>
</head>
<body>
<div id="app">
<div style="width :100px; height: 100px;background: skyblue;" >
</div>
<!-- 内联样式变量拼接 -->
<div style="width :100px; height: 100px;background: skyblue;"
:style="{border:borderWidth+'px solid red',padding:paddingWidth+'px'}">
</div>
<!-- 内联样式放置对象 -->
<div :style="styleObj"></div>
<!-- CSS数组方式拼接 -->
<div :style="styleArr"></div>
</div>
<script type="text/javascript">
let app = new Vue({
el:"#app",
data:{
borderWidth:50,
paddingWidth:30,
styleObj:{
width:"200px",
height:"300px",
padding:"50px",
backgroundColor:'skyblue'
//'background-color':'skyblue'
},
styleArr:[
{
width:"200px",
height:"300px",
padding:"50px",
backgroundColor:'skyblue'
//'background-color':'skyblue'
},{
border:"30px solid yellow"
}
]
}
})
</script>
</body>
</html>
二:一个简单的菜单栏代码如下所示:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/vue.js" type="text/javascript"></script>
<style type="text/css">
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.page{
width: 100vw;
height: 100vh;
background-color: #efefef;
position: fixed;
left: 0;
top: 0;
}
.rMenu{
width: 50vw;
height: 100vh;
position: fixed;
left: 0;
top: 0;
transform: translateX(100vw);
background-color: skyblue;
transition: transform 2s;
}
.active{
transform: translateX(50vw);
}
</style>
</head>
<body>
<div id="app">
<div class="page">
首页
<button @click="toggleMenu" type="button">切换侧边栏</button>
</div>
<div class="rMenu" :class="{active:isShow}">
侧边栏
</div>
</div>
<script type="text/javascript">
let app = new Vue({
el:"#app",
data:{
isShow:false
},
methods:{
toggleMenu:function(){
this.isShow = !this.isShow;
}
}
})
</script>
</body>
</html>
三:用style的形式来拼接一个菜单栏
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/vue.js" type="text/javascript"></script>
<style type="text/css">
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.page{
width: 100vw;
height: 100vh;
background-color: #efefef;
position: fixed;
left: 0;
top: 0;
}
.rMenu{
width: 50vw;
height: 100vh;
position: fixed;
left: 0;
top: 0;
transform: translateX(100vw);
background-color: skyblue;
transition: transform 2s;
}
.active{
transform: translateX(50vw);
}
</style>
</head>
<body>
<div id="app">
<div class="page">
首页
<button @click="toggleMenu" type="button">切换侧边栏</button>
</div>
<div class="rMenu" :style="{transform: 'translateX('+menuWidth+'vw)'}">
侧边栏
</div>
</div>
<script type="text/javascript">
let app = new Vue({
el:"#app",
data:{
// isShow:false
menuWidth:100
},
methods:{
// toggleMenu:function(){
// this.isShow = !this.isShow;
// },
toggleMenu:function(){
if(this.menuWidth==100){
this.menuWidth = 30
}else{
this.menuWidth = 100
}
}
}
})
</script>
</body>
</html>