示例
index.html
<p :class="pClasses">Style Me!</p>
<p :style="{backgroundColor: inputBC}">Style Me Inline!</p>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vue Basics</title>
<link
href="https://fonts.googleapis.com/css2?family=Jost:wght@400;700&display=swap"
rel="stylesheet"
/>
<link rel="stylesheet" href="styles.css" />
<script src="https://unpkg.com/vue@3/dist/vue.global.js" defer></script>
<script src="app.js" defer></script>
</head>
<body>
<header>
<h1>Vue Styling</h1>
</header>
<section id="styling">
<input type="text" v-model="inputClass">
<p :class="pClasses">Style Me!</p>
<button @click="togglePVisibility">Toggle Paragraph</button>
<input type="text" v-model="inputBC">
<p :style="{backgroundColor: inputBC}">Style Me Inline!</p>
</section>
</body>
</html>
app.js
const app = Vue.createApp({
data() {
return {
inputClass: '',
pIsVisible: true,
inputBC: ''
};
},
methods: {
togglePVisibility() {
this.pIsVisible = !this.pIsVisible;
}
},
computed: {
pClasses() {
return {
user1: this.inputClass === 'user1',
user2: this.inputClass === 'user2',
visible: this.pIsVisible,
hidden: !this.pIsVisible,
}
}
}
});
app.mount('#styling');
styles.css
* {
box-sizing: border-box;
}
html {
font-family: 'Jost', sans-serif;
}
body {
margin: 0;
}
header {
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.26);
margin: 3rem;
border-radius: 10px;
padding: 1rem;
background-color: #4fc08d;
color: white;
text-align: center;
}
#styling {
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.26);
margin: 3rem;
border-radius: 10px;
padding: 1rem;
text-align: center;
}
p {
font-size: 1.25rem;
font-weight: bold;
border: 1px solid #4fc08d;
background-color: #4fc08d;
color: white;
padding: 0.5rem;
border-radius: 25px;
}
button {
font: inherit;
cursor: pointer;
border: 1px solid #ff0077;
background-color: #ff0077;
color: white;
padding: 0.05rem 1rem;
box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.26);
border-radius: 20px;
margin: 0 1rem;
}
.user1 {
background-color: blue;
color: black;
}
.user2 {
background-color: purple;
color: black;
}
.hidden {
display: none;
}
.visible {
display: block;
}
.demo {
width: calc(100% - 32px);
height: 100px;
margin: 16px;
border: 2px dashed #ccc;
}
.active {
border-color: red;
background-color: salmon;
}