angular1自定义指令模拟ng-material的输入框效果

效果图:

 

 

完整代码:

<!DOCTYPE html> 
<html lang="zh" ng-app="myApp"> 
<head> 
 <meta charset="UTF-8"> 
	<link rel="shortcut icon" href="favicon.ico" /> 
 <title>AngularJS入门学习</title> 
 <script type="text/javascript" src="js/angular.min.js"></script> 
</head> 
<body> 
<div ng-controller="myController"> 
	<div class="inputitem">
		<md-input-container class="mdic">
			<label>the label 1</label>
			<input type="text" name="1" ng-model="value">
		</md-input-container>
	</div>
	<div class="inputitem">
		<md-input-container class="mdic">
			<label>the label 2</label>
			<input type="text" name="1" ng-model="value">
		</md-input-container>
	</div>
</div>
<style>
	.inputitem{margin-top: 30px;height: 60px;}
    .mdic{position: relative;}
    .mdic input,.mdic label{position: absolute;font-size:28px;top:10px;left: 10px;}
    .mdic label{z-index: 1;white-space: nowrap;}
    .md-input-focused{position: relative;height: 100px;transition: all 0.3s ease-in-out;}
	.md-input-focused label{top: -10px;font-size:14px;transition: all 0.3s ease-in-out;}
	.md-input-focused input{background: blue;border: 1px solid red;position: absolute;top: 10px;left: 10px;transition: all 0.3s ease-in-out;}
</style>
</body> 
<script type="text/javascript"> 
var app = angular.module('myApp', []);
 app.controller("myController", function ($scope) {
	 $scope.name = "hello world"; 
 });
 app.directive("mdInputContainer", function(){
 	return {
  		scope: {}, 
 	    restrict: 'E',
		controller: function($scope, $element){
			this.setFocused = function(isFocused) {
			  $element.toggleClass('md-input-focused', !!isFocused);
			};
			this.setHasValue = function(hasValue) {
			  $element.toggleClass('md-input-has-value', !!hasValue);
			};
		}
 	};
 });
 app.directive("input",function(){
 	return {
 		scope: {},
 	    restrict: 'E',
 	    require: '^mdInputContainer',
 	    link: function(scope, elem, attrs, controllerInstance){
		 		var el = angular.element(elem)
		 		var self = this;
		 		el
		 		    .on('focus', function(ev) {
		 		      controllerInstance.setFocused(true);
		 		    })
		 		    .on('blur', function(ev) {
		 		      controllerInstance.setFocused(false);
		 		    });
		 	}
 	};
 });
</script> 
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值