JavaScript ile CSS Değişkenlerini Değiştir
JavaScript ile Değişkenleri Değiştirin
CSS değişkenlerinin DOM'a erişimi vardır, bu da onları JavaScript ile değiştirebileceğiniz anlamına gelir.
Önceki sayfalarda kullanılan örnekten --blue değişkenini görüntülemek ve değiştirmek için bir komut dosyasını nasıl oluşturabileceğinize ilişkin bir örnek aşağıda verilmiştir. Şimdilik JavaScript'e aşina değilseniz endişelenmeyin. JavaScript Eğitimimizde JavaScript hakkında daha fazla bilgi edinebilirsiniz :
Örnek
<script>
// Get the root element
var r = document.querySelector(':root');
// Create a function for getting a variable value
function
myFunction_get() {
// Get the styles (properties and values) for the
root
var rs = getComputedStyle(r);
// Alert the value of
the --blue variable
alert("The value of --blue is: " +
rs.getPropertyValue('--blue'));
}
// Create a function for setting a
variable value
function myFunction_set() {
// Set the value of
variable --blue to another value (in this case "lightblue")
r.style.setProperty('--blue', 'lightblue');
}
</script>
Tarayıcı Desteği
Tablodaki sayılar,
var()
işlevi tam olarak destekleyen ilk tarayıcı sürümünü belirtir.
Function | |||||
---|---|---|---|---|---|
var() | 49.0 | 15.0 | 31.0 | 9.1 | 36.0 |
CSS var() İşlevi
Property | Description |
---|---|
var() | Inserts the value of a CSS variable |