<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
:root {
--mi-titulo:#000;
--mi-color:#808080;
--mi-color-link:blue;
--mi-background:#fff;
}
body {
background-color:var(--mi-background);
color: var(--mi-color);
}
a:link, a:visited, a:hover {color:var(--mi-color-link);}
h1 {
color:var(--mi-titulo);
}
header {
position:relative;
}
header>div {
position:absolute;
top:14px;
right:10px;
}
header>div>div {
float:left;
margin-left:5px;
}
</style>
</head>
<body>
<header>
<h1>Como cambiar el diseño Light o Dark</h1>
<div>
<div id="light"><a>Light</a></div><div>|</div><div id="dark"><a href="#">Dark</a></div>
</div>
</header>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</body>
</html>
<script>
// creamos el evento click para el cambio de color
const links=document.querySelectorAll("header a");
for(link of links) {
console.log(link);
link.addEventListener("click",cambioColor);
}
function cambioColor(e) {
if(this.hasAttribute("href")==false) {
return;
}
// variables CSS para el estilo dark
const dark=":root { \
--mi-titulo:#fff; \
--mi-color:#b9b9b9; \
--mi-color-link:#baffff; \
--mi-background:#303337; \
}";
// variables CSS para el estilo light
const light=":root { \
--mi-titulo:#000; \
--mi-color:#808080; \
--mi-color-link:blue; \
--mi-background:#fff; \
}";
const head = document.head || document.getElementsByTagName('head')[0];
const style = document.createElement('style');
let css="";
if(this.parentElement.id=="dark") {
css = document.createTextNode(dark);
this.removeAttribute("href");
document.querySelector("#light a").setAttribute("href","#");
}else{
css = document.createTextNode(light);
this.removeAttribute("href");
document.querySelector("#dark a").setAttribute("href","#");
}
style.type = 'text/css';
style.appendChild(css);
// Añadimos al final de la cabecera un nuevo estilo con la definición de las variables
// de esta manera, el documento cambiara con los nuevos estilos
head.appendChild(style);
}
</script>
Comentarios sobre la versión: 1 (2)