日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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='main.css'><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script><script>var myApp = angular.module('App', []);myApp.controller('AppController',function($scope) {<span style="white-space:pre"> </span>//默認lightswitch關閉$scope.lightswitch = 'off';$scope.switchTheme = function() {var checkProperty = document.getElementById('myonoffswitch').checked;//如果toggle button被點擊,body元素class設為“on”if (checkProperty) {$scope.lightswitch = 'on';}?//如果toggle button再次被點擊,body元素class設為“off”else {$scope.lightswitch = 'off';}}});</script> </head><body ng-app='App' ng-controller='AppController' ng-class="lightswitch"><div class="onoffswitch"><input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" ng-click="switchTheme()"><label class="onoffswitch-label" for="myonoffswitch"> <span class="onoffswitch-inner"></span><span class="onoffswitch-switch"></span></label></div><p class='p1'>test line 1</p><p class='p2'>test line 2</p> </body></html>


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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。