首页 > 代码库 > Angularjs切换网站配色模式简单示例2(切换body元素的class)

Angularjs切换网站配色模式简单示例2(切换body元素的class)

一个网站可以有多种配色方案,例如正常模式,夜间模式等。

简单示例一个通过点击toggle 按钮,切换body元素的class,达到切换配色模式的angularjs小app。

1. Live范例可以在以下Codepen网址查看: http://codepen.io/ChristyWang/pen/NPxNMx

2. Toggle button源代码由以下网站生成:https://proto.io/freebies/onoff/


3. 主要文件有两个:index.html和main.css。

index.html文件代码如下:

<!DOCTYPE html>
<html>

<head>
    <link rel='stylesheet' type='text/css' href=http://www.mamicode.com/'main.css'>>


main.css文件代码如下:

 /*for light swtich*/
 .off p {
	color: red;
}

body.on{
	background-color:grey;
}
 .on p {
	color: blue;
}

/*for the toggle button*/
.onoffswitch {
    position: relative; width: 67px;
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
    display: none;
}
.onoffswitch-label {
    display: block; overflow: hidden; cursor: pointer;
    border: 2px solid #999999; border-radius: 20px;
}
.onoffswitch-inner {
    display: block; width: 200%; margin-left: -100%;
    -moz-transition: margin 0.3s ease-in 0s; -webkit-transition: margin 0.3s ease-in 0s;
    -o-transition: margin 0.3s ease-in 0s; transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
    display: block; float: left; width: 50%; height: 22px; padding: 0; line-height: 22px;
    font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
    -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;
}
.onoffswitch-inner:before {
    content: "ON";
    padding-left: 10px;
    background-color: #0064D2; color: ;
}
.onoffswitch-inner:after {
    content: "OFF";
    padding-right: 10px;
    background-color: #F0F0F0; color: #999999;
    text-align: right;
}
.onoffswitch-switch {
    display: block; width: 18px; margin: 2px;
    background: #FFFFFF;
    border: 2px solid #999999; border-radius: 20px;
    position: absolute; top: 0; bottom: 0; right: 41px;
    -moz-transition: all 0.3s ease-in 0s; -webkit-transition: all 0.3s ease-in 0s;
    -o-transition: all 0.3s ease-in 0s; transition: all 0.3s ease-in 0s; 
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px; 
} 

4. 截图展示:
lightswich off:



lightswitch on:


Angularjs切换网站配色模式简单示例2(切换body元素的class)