{"id":1439,"date":"2024-08-02T06:28:35","date_gmt":"2024-08-02T04:28:35","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=1439"},"modified":"2024-08-02T06:28:35","modified_gmt":"2024-08-02T04:28:35","slug":"19-cambiar-variables-css-con-javascript","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/css3\/css3-avanzado\/19-cambiar-variables-css-con-javascript\/","title":{"rendered":"19. Cambiar variables CSS con Javascript"},"content":{"rendered":"\n<p>Las <strong>variables CSS<\/strong> tienen acceso al <strong>DOM<\/strong>, lo que significa que puedes cambiarlas con <strong>Javascript<\/strong>. Este es un ejemplo de c\u00f3mo puede crear un <strong>script<\/strong> para mostrar y cambiar la variable <em>&#8211;blue<\/em> del ejemplo utilizado en las p\u00e1ginas anteriores.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html><br>&lt;html><br>  &lt;head><br>    &lt;style><br>      :root {<br>        --blue: #1e90ff;<br>        --white: #ffffff;<br>      }<br><br>      body {<br>        background-color: var(--blue);<br>      }<br><br>      h2 {<br>        border-bottom: 2px solid var(--blue);<br>      }<br><br>      .container {<br>        color: var(--blue);<br>        background-color: var(--white);<br>        padding: 15px;<br>      }<br><br>      .container button {<br>        background-color: var(--white);<br>        color: var(--blue);<br>        border: 1px solid var(--blue);<br>        padding: 5px;<br>      }<br>    &lt;\/style><br><br>    &lt;script><br>      \/\/ Get the root element<br>      var r = document.querySelector(\":root\");<br><br>      \/\/ Create a function for getting a variable value<br>      function myFunction_get() {<br>        \/\/ Get the styles (properties and values) for the root<br>        var rs = getComputedStyle(r);<br>        \/\/ Alert the value of the --blue variable<br>        alert(\"The value of --blue is: \" + rs.getPropertyValue(\"--blue\"));<br>      }<br><br>      \/\/ Create a function for setting a variable value<br>      function myFunction_set() {<br>        \/\/ Set the value of variable --blue to another value (in this case \"lightblue\")<br>        r.style.setProperty(\"--blue\", \"lightblue\");<br>      }<br>    &lt;\/script><br>  &lt;\/head><br>  &lt;body><br>    &lt;h1>Get and Change CSS Variable With JavaScript&lt;\/h1><br><br>    &lt;div class=\"container\"><br>      &lt;h2>Lorem Ipsum&lt;\/h2><br>      &lt;p><br>        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper<br>        diam at erat pulvinar, at pulvinar felis blandit.<br>      &lt;\/p><br>      &lt;p><br>        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper<br>        diam at erat pulvinar, at pulvinar felis blandit.<br>      &lt;\/p><br>      &lt;p><br>        &lt;button>Yes&lt;\/button><br>        &lt;button>No&lt;\/button><br>      &lt;\/p><br>    &lt;\/div><br>    &lt;br \/><br><br>    &lt;button type=\"button\" onclick=\"myFunction_get()\"><br>      Get CSS Variable with JavaScript<br>    &lt;\/button><br>    &lt;button type=\"button\" onclick=\"myFunction_set()\"><br>      Change CSS Variable with JavaScript<br>    &lt;\/button><br>  &lt;\/body><br>&lt;\/html><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Las variables CSS tienen acceso al DOM, lo que significa que puedes cambiarlas con Javascript. Este es un ejemplo de [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1367,"menu_order":18,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-1439","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"Las variables CSS tienen acceso al DOM, lo que significa que puedes cambiarlas con Javascript. Este es un ejemplo de [&hellip;]","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=1439"}],"version-history":[{"count":3,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1439\/revisions"}],"predecessor-version":[{"id":1442,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1439\/revisions\/1442"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1367"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=1439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}