当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > AngularJS实现网站换肤实例

AngularJS实现网站换肤实例

2018年05月03日  | 移动技术网IT编程  | 我要评论

网站不应该只注重功能,还应该注重用户体验;成熟的大型网站大都具备让用户自行选择主题及颜色的功能,angularjs也可以做到这点。

效果图:

原理是使用ng-href来动态为网页更换样式:

<link rel="stylesheet" type="text/css" ng-href="{{theme.value}}.css">

代码:

<!doctype html>
<html ng-app="app" ng-controller="mainctrl">
<head >
 <meta charset="utf-8">
 <title></title>
 //动态更新css样式
 <link rel="stylesheet" type="text/css" ng-href="{{theme.value}}.css">
 <script src="../angular.js"></script>
 <style type="text/css">
  h1{
   font-style:italic;
  }
 </style>
 <script type="text/javascript">
  angular.module("app", [])
   .controller("mainctrl", function($scope){
    $scope.options = [
     {
      name:"蓝色",
      value:"blue"
     },
     {
      name:"红色",
      value:"red"
     }
    ];
    //默认选择第一个样式
    $scope.theme = $scope.options[0];
   })
 </script>
</head>
<body>
 <select ng-model="theme" ng-options="c.name for c in options">
 </select>
 <h1>welcome</h1>
 <ul>
  <li>home</li>
  <li>about</li>
  <li>contact</li>
 </ul>
</body>
</html>

blue.css

h1{
 color:blue;
}
ul li{
 display:inline-block;
}

red.css

h1{
 color:red;
}

此文档的作者:
github pages:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网