Código de JavaScript - Cambiar el diseño de tu pagina entre Dark y Light

1
estrellaestrellaestrellaestrellaestrella(2)

Publicado el 4 de Marzo del 2019gráfica de visualizaciones de la versión: 1
1.592 visualizaciones desde el 4 de Marzo del 2019
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!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)

Imágen de perfil
5 de Marzo del 2019
estrellaestrellaestrellaestrellaestrella
No ha dejado ningún comentario
Responder
Imágen de perfil
5 de Marzo del 2019
estrellaestrellaestrellaestrellaestrella
No ha dejado ningún comentario
Responder

Comentar la versión: 1

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s5180