Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
105 lines (94 sloc) 2.59 KB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Scoped CSS Variables and JS</title>
<script src="https://unpkg.com/vue"></script>
</head>
<body>
<div id="app">
<h2>Update CSS Variables with <span class='hl'>JS</span></h2>
<div class="controls">
<label for="spacing">Spacing:</label>
<input id="spacing" type="range" min="10" max="200" v-model="spacing">
<label for="blur">Blur:</label>
<input id="blur" type="range" min="0" max="25" v-model="blur">
<label for="base">Base Color</label>
<input id="base" type="color" v-model="base">
</div>
<img src="https://source.unsplash.com/7bwQXzbF6KE/800x500">
</div>
<script>
var app = new Vue({
el: '#app',
data: {
spacing: 10,
blur: 10,
base: "#ffc600"
},
mounted: function() {
// Update the CSS Variables when mounted
this.updateCSSSpacing()
this.updateCSSBlur()
this.updateCSSBase()
},
methods: {
// Update the style of the root DOM element that the Vue instance is managing
updateCSSVariable: function(name, value) {
this.$el.style.setProperty(name, value)
},
updateCSSSpacing: function () {
this.updateCSSVariable('--spacing', `${this.spacing}px`)
},
updateCSSBlur: function () {
this.updateCSSVariable('--blur', `${this.blur}px`)
},
updateCSSBase: function () {
this.updateCSSVariable('--base', this.base)
}
},
watch: {
// When the value of the spacing attribute changes, update its CSS variable
spacing: function(newValue, oldValue) {
this.updateCSSSpacing()
},
// When the value of the blur attribute changes, update its CSS variable
blur: function(newValue, oldValue) {
this.updateCSSBlur()
},
// When the value of the base attribute changes, update its CSS variable
base: function(newValue, oldValue) {
this.updateCSSBase()
}
}
})
</script>
<style>
img {
padding: var(--spacing);
background: var(--base);
filter: blur(var(--blur));
}
.hl {
color: var(--base);
}
/*
misc styles, nothing to do with CSS variables
*/
body {
text-align: center;
background: #193549;
color: white;
font-family: 'helvetica neue', sans-serif;
font-weight: 100;
font-size: 50px;
}
.controls {
margin-bottom: 50px;
}
input {
width:100px;
}
</style>
</body>
</html>