【angularjs】使用angular搭建PC端项目,开关按钮
方法一(使用指令)
1.指令(angular-ui-switch.js)
angular.module('uiSwitch', []) app.directive('switch', function(){
return {
restrict: 'AE'
, replace: true
, transclude: true
, template: function(element, attrs) {
var html = '';
html += '<span';
html += ' class="switch' + (attrs.class ? ' ' + attrs.class : '') + '"';
html += attrs.ngModel ? ' ng-click="' + attrs.disabled + ' ? ' + attrs.ngModel + ' : ' + attrs.ngModel + '=!' + attrs.ngModel + (attrs.ngChange ? '; ' + attrs.ngChange + '()"' : '"') : '';
html += ' ng-class="{ checked:' + attrs.ngModel + ', disabled:' + attrs.disabled + ' }"';
html += '>';
html += '<small></small>';
html += '<input type="checkbox"';
html += attrs.id ? ' id="' + attrs.id + '"' : '';
html += attrs.name ? ' name="' + attrs.name + '"' : '';
html += attrs.ngModel ? ' ng-model="' + attrs.ngModel + '"' : '';
html += ' style="display:none" />';
html += '<span class="switch-text">'; /*adding new container for switch text*/
html += attrs.on ? '<span class="on">'+attrs.on+'</span>' : ''; /*switch text on value set by user in directive html markup*/
html += attrs.off ? '<span class="off">'+attrs.off + '</span>' : ' '; /*switch text off value set by user in directive html markup*/
html += '</span>';
return html;
}
}
});
2.css(angular-ui-switch.css)
.switch {
background: #fff;
border: 1px solid #dfdfdf;
position: relative;
display: inline-block;
box-sizing: content-box;
overflow: visible;
width: 52px;
height: 30px;
padding: 0px;
margin: 0px;
border-radius: 20px;
cursor: pointer;
box-shadow: rgb(223, 223, 223) 0px 0px 0px 0px inset;
transition: 0.3s ease-out all;
-webkit-transition: 0.3s ease-out all;
top: -1px;
}
/*adding a wide width for larger switch text*/
.switch.wide {
width:80px;
}
.switch small {
background: #fff;
border-radius: 100%;
box-shadow: 0 1px 3px rgba(0,0,0,0.4);
width: 30px;
height: 30px;
position: absolute;
top: 0px;
left: 0px;
transition: 0.3s ease-out all;
-webkit-transition: 0.3s ease-out all;
}
.switch.checked {
background: rgb(100, 189, 99);
border-color: rgb(100, 189, 99);
}
.switch.checked small {
left: 22px;
}
/*wider switch text moves small further to the right*/
.switch.wide.checked small {
left:52px;
}
/*styles for switch-text*/
.switch .switch-text {
font-family:Arial, Helvetica, sans-serif;
font-size:13px;
} .switch .off {
display:block;
position: absolute;
right: 10%;
top: 25%;
z-index: 0;
color:#A9A9A9;
} .switch .on {
display:none;
z-index: 0;
color:#fff;
position: absolute;
top: 25%;
left: 9%;
} .switch.checked .off {
display:none;
} .switch.checked .on {
display:block; } .switch.disabled {
opacity: .50;
cursor: not-allowed;
}
3.在apps.js中注入
var app = angular.module('app', ['uiSwitch'])
4.index.html引入相关文件
<link rel="stylesheet" href="../angular-ui-switch.css"/>
<script src="../angular.min.js"></script>
<script src="../angular-ui-switch.js"></script>
<script src="app.js"></script>
5.应用:
<form data-ng-controller="MyController">
<switch name="enabled" ng-model="enabled" ng-change="changeCallback"></switch>
<p>
<button ng-click="enabled=!enabled">Toggle</button>
</p>
<p>
Enabled: {{ enabled }}
</p>
<!--Examples of using switch text on/off values. These values can be anything. First example shows basic on/off--> <switch name="onOff" ng-model="onOff" on="on" off="off"></switch>
<p>
<button ng-click="onOff=!onOff">Toggle</button>
</p>
<p>
Enabled: {{ onOff }}
</p> <!--Examples of using the nonsense words lorem/ipsum in the on/off values. Because these are wider, added an option "wide" class to allow for more room-->
<switch name="yesNo" ng-model="yesNo" on="lorem" off="ipsum" class="wide"></switch>
<p>
<button ng-click="onOff=!onOff">Toggle</button>
</p>
<p>
Enabled: {{ yesNo }}
</p> <!--Examples of using switch disabled states. -->
<switch name="disabled" ng-model="disabled" disabled="true"></switch>
<p>
<button ng-click="disabled=!disabled">Toggle</button>
</p>
<p>
Enabled: {{ disabled }}
</p>
</form>
6.附录---目录结构
7.效果图:
方法二(使用背景图和ng-class)
html
<span class="wrap" ng-class="{true:'on',false:'off'}[isOn]" ng-click="isOn = !isOn"></span>
css
.wrap{
display:block;
width:68px;
height:40px;
background-size: cover;
cursor: pointer;
}
.on{
background:url(../on.png) no-repeat; }
.off{
background:url(../off.png) no-repeat;
}
js
$scope.isOn = false;//默认为关闭状态
效果:
相关链接:
个人资料:
作者:smile.轉角
QQ:493177502
【angularjs】使用angular搭建PC端项目,开关按钮的更多相关文章
- 【angularjs】使用ionic+angular 搭建移动端项目,字体适配
解析: 首先,rem是以html为基准. 一般的,各大主流浏览器的font-size默认值为16px,此时1rem=16px.如果此时将rem与px进行换算很麻烦,比如0.75rem=12px. 为了 ...
- 曾经的pc端项目踩到的一些兼容性的坑及其解决方案
曾经公司pc端项目一直最低兼容到IE7,要求和chrome下浏览效果一致,真心坑坏了我和另外一个小伙伴(另一个小伙伴以前也没处理过兼容问题).不过还好,在这里真心感谢鑫哥博客的详解,从底层原理讲到了具 ...
- react 开发 PC 端项目(一)项目环境搭建 及 处理 IE8 兼容问题
步骤一:项目环境搭建 首先,你不应该使用 React v15 或更高版本.使用仍然支持 IE8 的 React v0.14 即可. 技术选型: 1.react@0.14 2.bootstrap3 3. ...
- vue脚手架搭建移动端项目--flexible.js
通过命令行 node -v 查看是否安装node环境 在 nodejs 和 webpack已安装的前提下,随便一个文件夹下,输入命令行 npm install vue-cli -g 安装完成后,通过 ...
- pc端的企业网站(IT修真院test9)详解一个响应式完成的pc端项目
一:引入bootstrap框架 昨天一直被bootstrap栅格系统折磨. why? 我本来想一边码字,一边学习栅格布局的.but不成功.这时我头脑已经昏了. 下午,我查看了bootstrap的官网, ...
- vue PC端项目中解决userinfo问题
在vue2 中用脚手架建立的项目,后端提供接口获取数据.在公司做第一个项目的时候不清楚公司里的对接流程,结果后续代码被一个接口整的乱七八糟,这个接口是获取用户信息的接口——'usre/info'. 如 ...
- angular --- s3core移动端项目(三)
angular.module('myApp') .directive('listActive',functon(){ return { restrict:'A', scope:{ listActive ...
- angular --- s3core移动端项目(二)
product-ctrl.js angular.modules('myApp').controller('ProductCtrl',['$scope','$rootScope','$timeout', ...
- angular --- s3core移动端项目
因为记性不好的原因做个草稿笔记 app.js中 var myApp = angular.module('myApp',['ui.router','oc.lazyLoad','ngAnimate','数 ...
随机推荐
- springboot之scheduled任务调度
springboot整合Scheduled可以方便的进行任务调度,话不多说,直接上代码 package com.rookie.bigdata; import org.springframework.b ...
- log4j介绍
log4j.properties文件的三种加载方式 1.默认自动加载 满足以下条件时: 1).配置文件名为log4j.properties 2).在classpath根目录下(即resources根目 ...
- JS中substring与substr的用法
substring方法用于提取字符串中介于两个指定下标之间的字符 substring(start,end) 开始和结束的位置,从零开始的索引javascript 参数 描述 start 必需.一个非负 ...
- CSS3布局之box-flex的使用
语法: box-flex:<number> 其中number取值:使用浮点数指定对象所分配其父元素剩余空间的比例.设置或检索伸缩盒对象的子元素如何分配其剩余空间.(伸缩盒最老版本) htm ...
- P3275 [SCOI2011]糖果
题目描述 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配 ...
- clipboard.js -- js实现将文本复制到剪贴板的方法
资源 推荐使用:clipboard.js 官方教程地址:https://clipboardjs.com/#example-text 官方github地址:https://github.com/zeno ...
- 从换脸到换姿势,AI 在图像处理的道路上越走越魔幻
从换脸到换姿势,AI 在图像处理的道路上越走越魔幻 https://mp.weixin.qq.com/s/VFq3BWLpzyKZ3sqVWf1HKA [导读]加州大学伯克利分校的研究人员,近日在著名 ...
- 如何用ABP框架快速完成项目(8) - 用ABP一个人快速完成项目(4) - 能自动化就不要手动 - 使用自动化测试(BDD/TDD)
做为一个程序员, 深深知道计算机自动化的速度是比人手动的速度快的, 所以”快速”完成项目的一个重要武器就是: 能自动化就不要手动. BDD/TDD有很多优势, 其中之一就是自动化, 我们这节文章先 ...
- Linux 安装 Mysql 5.7.23
切换目录 cd /usr 创建目录 mkdir mysql cd mysql 下载 Mysql Yum wget http://repo.mysql.com/mysql57-community-rel ...
- Android广播机制的基本使用
一提到广播我们第一感觉就会联想到小时候村里面的广播,安卓的广播机制也是类似于大喇叭.有发送广播的地方,也有接收广播的地方.但是具体怎么操作呢,我们来一步一步的看下去~ 安卓的广播种类 系统发送的广播: ...