Vue class與style繫結
阿新 • • 發佈:2020-08-27
Vue class與style繫結
剛開始學習vue,下面是一些小結
關於class與style繫結的官網地址:https://cn.vuejs.org/v2/guide/class-and-style.html
一、物件的寫法繫結class
第一種物件寫法:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script type="text/javascript" src="vue.js" charset="UTF-8"></script> </head> <body> <div id="test"> <div v-bind:class="classObject">fasd</div> </div> <script> var watchExampleVM = new Vue({ el:"#test", data: { classObject: { active: true, 'text-danger': true } } }) </script> <style type="text/css"> .active { font-size: 1.875rem; color: red; } </style> </body> </html>
該寫法較第二種簡便,故此推薦!
第二種物件寫法:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script type="text/javascript" src="vue.js" charset="UTF-8"></script> </head> <body> <div id="test"> <div v-bind:class="{ entity: classObject.textdanger , haha: classObject.active}">fasd</div> </div> <script> var watchExampleVM = new Vue({ el:"#test", data: { classObject: { active: true, textdanger: true } } }) </script> <style type="text/css"> .haha{ color: green; text-align: center; } .entity { font-size: 100px; } </style> </body> </html>
二、陣列的寫法繫結class
第一種陣列寫法:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script type="text/javascript" src="vue.js" charset="UTF-8"></script> </head> <body> <div id="test"> <div v-bind:class="[active, textdanger]">fasd</div> </div> <script> var watchExampleVM = new Vue({ el:"#test", data: { active: 'haha', textdanger: 'entity' } }) </script> <style type="text/css"> .haha{ color: green; text-align: center; } .entity { font-size: 100px; } </style> </body> </html>
第二種三元運算子寫法:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="vue.js" charset="UTF-8"></script>
</head>
<body>
<div id="test">
<div v-bind:class="[active ? 'haha' : '', textdanger]">fasd</div>
</div>
<script>
var watchExampleVM = new Vue({
el:"#test",
data: {
active: true,
textdanger: 'entity'
}
})
</script>
<style type="text/css">
/* .active {
font-size: 1.875rem;
color: red;
} */
.haha{
color: green;
text-align: center;
}
.entity {
font-size: 100px;
}
</style>
</body>
</html>
三、物件的寫法繫結style
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="vue.js" charset="UTF-8"></script>
</head>
<body>
<div id="test">
<div v-bind:style="styleObject">test</div>
</div>
<script>
var watchExampleVM = new Vue({
el:"#test",
data: {
styleObject: {
color: 'red',
fontSize: '20px'
}
}
})
</script>
<style type="text/css">
</style>
</body>
</html>
四、陣列的寫法繫結style
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="vue.js" charset="UTF-8"></script>
</head>
<body>
<div id="test">
<div v-bind:style="[styleObject , styleObject2]">test</div>
</div>
<script>
var watchExampleVM = new Vue({
el:"#test",
data: {
styleObject: {
color: 'red',
},
styleObject2: {
fontSize: '20px'
}
}
})
</script>
<style type="text/css">
</style>
</body>
</html>