Angularjs切換網站配色模式簡單樣本

來源:互聯網
上載者:User

標籤:angularjs   stylesheet   css   color   原始碼   

一個網站可以有多種色彩配置,例如正常模式,夜間模式等。

簡單樣本一個通過點擊按鈕,更換css檔案,達到切換配色模式的angularjs 小app。

主要檔案有三個:index.html(主檔案),white.css(白色背景色彩配置css檔案),black.css(黑色背景色彩配置css檔案)。

index.html檔案代碼如下:

<!DOCTYPE html><html><head><link rel='stylesheet' type='text/css' href='black.css' id="global-css"><scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script><script>var myApp = angular.module('App', []);myApp.factory('myFac', function() {return {changeTheme : function(themeFile) {//根據ID定位載入css的元素,將其href修改為特定css檔案document.getElementById('global-css').setAttribute('href',themeFile);}};});myApp.controller('AppController', function($scope, myFac) {//如果是white主題,使用css檔案white.css$scope.whiteTheme = function() {myFac.changeTheme('white.css');}//如果是black主題,使用css檔案black.css$scope.blackTheme = function() {myFac.changeTheme('black.css');}});</script></head><body ng-app='App' ng-controller='AppController'><button ng-click='blackTheme()'>black theme</button><button ng-click='whiteTheme()'>white theme</button><p class='p1'>test line 1</p><p class='p2'>test line 2</p></body></html>


black.css代碼如下:

body{background-color:black;}.p1{color:red;}.p2{color:blue;}

white.css代碼如下:

body{background-color:white;}


index.html預設使用黑色背景色彩配置。點擊“white theme”按鈕切換為白色背景色彩配置,點擊“black theme”按鈕,切換成黑色背景色彩配置。具體樣本如下:

在瀏覽器中運行index.html如下:


查看網頁原始碼,可以看到調用的css檔案是black.css:


點擊“white theme”按鈕,index.html改為白色背景色彩配置,網頁如下:


查看網頁原始碼,可以看到調用的css檔案是white.css:



Angularjs切換網站配色模式簡單樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.