Angularjs實(shí)現(xiàn)mvvm式的選項(xiàng)卡示例代碼
在實(shí)現(xiàn)Angularjs實(shí)現(xiàn)mvvm式的選項(xiàng)卡之前,先搬出我們常用的jquery實(shí)現(xiàn)。
1、jquery實(shí)現(xiàn)簡單粗暴的選項(xiàng)卡效果
var nav = $(".tabs");//tab切換
var box = $(".box");//容器
nav.on("click", function(){ //點(diǎn)擊事件
var this_index=$(this).index();
$(this).addClass("active").siblings().removeClass("active");
box.eq(this_index).show().siblings().hide();
});
在這里只給出js核心部分,html和css不做贅述。
以上代碼,簡單粗暴的實(shí)現(xiàn)了選項(xiàng)卡效果,用點(diǎn)擊事件獲得elem.index() , 把索引和容器串起來控制顯示隱藏。
2、angularjs實(shí)現(xiàn)一個(gè)簡單選項(xiàng)卡效果
Html部分
<section ng-app="myApp">
<div class="tabs tabs-style" ng-controller="TabController as tab">
<nav>
<ul>
<li ng-class="{'current':tab.isSet(1)}">
<a href="#" ng-click="tab.setCurrent(1)"><span>Home</span></a></li>
<li ng-class="{'current':tab.isSet(2)}">
<a href="#" ng-click="tab.setCurrent(2)"><span>Work</span></a></li>
<li ng-class="{'current':tab.isSet(3)}">
<a href="#" ng-click="tab.setCurrent(3)"><span>Blog</span></a></li>
<li ng-class="{'current':tab.isSet(4)}">
<a href="#" ng-click="tab.setCurrent(4)"><span>About</span></a></li>
<li ng-class="{'current':tab.isSet(5)}">
<a href="#" ng-click="tab.setCurrent(5)"><span>Contact</span></a></li>
</ul>
</nav>
<div class="content">
<section id="section-1" ng-show="tab.isSet(1)">
<p>1</p>
</section>
<section id="section-2" ng-show="tab.isSet(2)">
<p>2</p>
</section>
<section id="section-3" ng-show="tab.isSet(3)">
<p>3</p>
</section>
<section id="section-4" ng-show="tab.isSet(4)">
<p>4</p>
</section>
<section id="section-5" ng-show="tab.isSet(5)">
<p>5</p>
</section>
</div>
</div>
</section>
css 部分(這里為了方便我們使用Less語法,童鞋們可以自定義css實(shí)現(xiàn)個(gè)性效果)
* {
margin: 0;
padding: 0;
}
body {
background: #e7ecea;
font-weight: 600;
font-family: 'Raleway', Arial, sans-serif;
text-align: center;
}
a {
color: #2CC185;
text-decoration: none;
outline: none;
&:hover {
color: #74777b;
}
}
.tabs {
position: relative;
width: 100%;
margin: 30px auto 0 auto;
nav {
ul {
position: relative;
display: flex;
max-width: 1200px;
margin: 0 auto;
list-style: none;
flex-flow: row wrap;
justify-content: center;
li {
flex: 1;
&.current a {
color: #74777b;
}
}
}
}
a {
display: block;
position: relative;
overflow : hidden;
line-height: 2.5;
span {
vertical-align: middle;
font-size: 1.5em;
}
}
}
.content {
position: relative;
section {
/* display: none; */
margin: 0 auto;
max-width: 1200px;
&.content-current {
/* display: block; */
}
p {
color: rgba(40,44,42, 0.4);
margin: 0;
padding: 1.75em 0;
font-weight: 900;
font-size: 5em;
line-height: 1;
}
}
}
.tabs-style {
nav {
/* background: rgba(255,255,255,0.4); */
ul li {
a {
overflow: visible;
border-bottom: 1px solid rgba(0,0,0,0.2);
-webkit-transition: color 0.2s;
transition: color 0.2s;
}
}
ul li.current a{
&:after, &:before {
content: '';
position: absolute;
top: 100%;
left: 50%;
width: 0;
height: 0;
border: solid transparent;
}
&:after {
margin-left: -10px;
border-width: 10px;
border-top-color: #e7ecea;
}
&:before {
margin-left: -11px;
border-width: 11px;
border-top-color: rgba(0,0,0,0.2);
}
}
}
}
js部分
angular.module('myApp', [])
.controller('TabController', function() {
this.current = 1;
this.setCurrent = function (tab) {
this.current = tab;
};
this.isSet = function(tab) {
return this.current == tab;
};
});
最后效果如下圖所示:

通過以上代碼,我們可以發(fā)現(xiàn)實(shí)現(xiàn)的核心是angularjs內(nèi)置的ng-class和ng-click,ng-show指令。
通俗來講:controller里定義了current 這條數(shù)據(jù)默認(rèn)值為1,ng-click給點(diǎn)擊事件自定義函數(shù),改變current數(shù)據(jù),ng-class通過獲得current的值綁定條件,給當(dāng)前選中的索引添加current樣式,容器同樣獲得controller里的current數(shù)據(jù)通過ng-show控制顯示隱藏。
3、angularjs實(shí)現(xiàn)一個(gè)稍微復(fù)雜的移動端選項(xiàng)卡效果
html部分
<script src="http://cdn.bootcss.com/angular.js/1.3.8/angular.min.js"></script>
<script src="http://cdn.bootcss.com/angular.js/1.3.8/angular-touch.min.js"></script>
//angularjs的一個(gè)移動端touch事件庫
<div ng-app='myApp' ng-controller="myController">
<div class="type-tabs">
<div ng-repeat="item in [1,2,3,4]" ng-click="changeIndex($index)">Tab{{item}}</div>
</div>
<div class="guid-bar">
<div class="guid-bar-content" style="left:{{ 25*activeIndex}}%"></div>
</div>
<div class="tab-content" ng-swipe-right="swipeRight()" ng-swipe-left="swipeLeft()">
<div class="tab-content-inner" style="left:{{ -100*activeIndex}}%">
<div class="tab-content-item" ng-repeat="item in [1,2,3,4]" >
<br /><br /><br /><br />
<h1>Tab{{item}}
</h1></div>
</div>
</div>
</div>
css部分
*{
padding:0;
margin:0;
font-family:'Arial';
}
.type-tabs{
width: 100%;
height: 40px;
}
.type-tabs div{
float: left;
width: 25%;
line-height: 40px;
text-align: center;
cursor:pointer;
user-select:none;
-webkit-user-select:none;
}
.guid-bar{
position: relative;
margin-top: -3px;
}
.guid-bar-content{
width: 25%;
height: 3px;
background-color: #345;
position: absolute;
left: 50%;
transition:all 400ms ease;
}
.tab-content{
width: 100%;
height: 500px;
background-color: #ccc;
overflow: hidden;
}
.tab-content-inner{
width: 400%;
position: relative;
transition: all 400ms;
}
.tab-content-item{
width: 25%;
float: left;
text-align:center;
}
js部分
var myApp=angular.module('myApp',['ngTouch']);
myApp.controller('myController',function($scope){
$scope.activeIndex=0;
$scope.changeIndex=function(index){
$scope.activeIndex=index;
};
$scope.swipeLeft=function(){
$scope.activeIndex=++$scope.activeIndex;
$scope.check();
};
$scope.swipeRight=function(){
$scope.activeIndex=--$scope.activeIndex;
$scope.check();
};
$scope.check=function(){
if($scope.activeIndex>3)
$scope.activeIndex=0;
if($scope.activeIndex<0)
$scope.activeIndex=3;
}
})
效果如下:

好了,今天我們就給出這兩個(gè)例子,對angularjs了解過的童鞋,直接看代碼應(yīng)該可以很快看懂。沒了解過的童鞋,也能通過這兩個(gè)例子,了解到mvvm的黑魔法,以及它的代碼組織結(jié)構(gòu)。
4、angularjs的mvvm模式比jquery的dom操作好在哪里?
1、從宏觀上來說,一種是操作數(shù)據(jù)處理數(shù)據(jù),一種是操作dom和ui交互。
一般網(wǎng)頁項(xiàng)目的流程可以總結(jié)為三個(gè)流程:1) 你要獲取界面上的數(shù)據(jù) 2)后臺交換數(shù)據(jù)3)獲取數(shù)據(jù)后,對界面重新進(jìn)行渲染。這個(gè)過程中,你和后臺數(shù)據(jù)交換怎么實(shí)現(xiàn)?jquery的ajax吧,如果數(shù)據(jù)交換的API假設(shè)20多個(gè),那么$.get或者$.ajax你要寫多少個(gè)才能全部包含進(jìn)去?而且所有API鏈接都不在一個(gè)地方,管理起來相當(dāng)麻煩。而angularjs只要配置一下route就行了。
獲取了數(shù)據(jù)后,你又如何管理這些數(shù)據(jù),如何把數(shù)據(jù)渲染到界面上去?
如何管理各種事件?jquery本身特性,也就是事件觸發(fā),很多時(shí)候,就是你在編寫 觸發(fā)事件->處理數(shù)據(jù) 的流程。很顯然,功能一多,代碼就會和面條一樣,交織在一起了。身邊不乏兩三年的傳統(tǒng)jquery前端,事件委托、dom操作、瀏覽器渲染過程、插件組件封裝等等都沒有去深入研究,可想而知代碼質(zhì)量有多爛。事實(shí)上jquery是一個(gè)類庫,并不是一個(gè)開發(fā)框架,jq是對js原生api的進(jìn)一步封裝,讓你更加愉快的進(jìn)行dom操作、動畫操作以及ajax,正是因?yàn)樗^靈活,所以更容易寫出難以維護(hù)的代碼。
2、性能方面:DOM操作慢,DOM對象本身也是一個(gè)js對象,所以嚴(yán)格來說,并不是操作這個(gè)對象慢,而是說操作了這個(gè)對象后,會觸發(fā)一些瀏覽器行為,比如布局(layout)和繪制(paint)。
總結(jié)
隨著web產(chǎn)品越來越復(fù)雜,分層架構(gòu)是必不可少的,因此雙向綁定作為解藥,結(jié)合很早就流行的MVC框架,衍生出MVVM這神器。博主堅(jiān)信,mvvm會是前端的終極解決方案。由DOM操作到數(shù)據(jù)操作需要一個(gè)過程去適應(yīng),但只要結(jié)果是好的,這些付出就都值得。在這個(gè)過程中,也是對你專業(yè)能力的一種提升。加油小伙伴們?。?!
- angularJs中datatable實(shí)現(xiàn)代碼
- AngularJS tab欄實(shí)現(xiàn)和mvc小案例實(shí)例詳解
- Angularjs+bootstrap+table多選(全選)支持單擊行選中實(shí)現(xiàn)編輯、刪除功能
- 對比分析Django的Q查詢及AngularJS的Datatables分頁插件
- AngularJS ng-table插件設(shè)置排序
- 用AngularJS的指令實(shí)現(xiàn)tabs切換效果
- angularjs表格ng-table使用備忘錄
- AngularJS入門教程之學(xué)習(xí)環(huán)境搭建
- AngularJS中的模塊詳解
- AngularJS實(shí)現(xiàn)表單驗(yàn)證
- AngularJS中的指令全面解析(必看)
- AngularJS實(shí)現(xiàn)tab選項(xiàng)卡的方法詳解
相關(guān)文章
anime.js 實(shí)現(xiàn)帶有描邊動畫效果的復(fù)選框(推薦)
anime.js是一個(gè)靈活的輕型JavaScript動畫庫。這篇文章主要介紹了anime.js 實(shí)現(xiàn)帶有描邊動畫效果的復(fù)選框 ,需要的朋友可以參考下2017-12-12
Angularjs實(shí)現(xiàn)多個(gè)頁面共享數(shù)據(jù)的方式
本文給大家介紹使用Angularjs實(shí)現(xiàn)多個(gè)頁面共享數(shù)據(jù)的方式,通過定義一個(gè)共享服務(wù)service來實(shí)現(xiàn)此功能,對angularjs共享數(shù)據(jù)相關(guān)知識感興趣的朋友一起學(xué)習(xí)2016-03-03
詳解AngularJS中$filter過濾器使用(自定義過濾器)
這篇文章主要介紹了詳解AngularJS中$filter過濾器使用(自定義過濾器)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
AngularJS基礎(chǔ) ng-mouseenter 指令示例代碼
本文主要介紹AngularJS ng-mouseenter 指令,這里對ng-mouseenter 指令基礎(chǔ)資料做了詳細(xì)整理,并附代碼實(shí)例,有需要的小伙伴可以參考下2016-08-08
詳解Angular的雙向數(shù)據(jù)綁定(MV-VM)
本文主要對Angular的雙向數(shù)據(jù)綁定(MV-VM)進(jìn)行實(shí)例分析,具有一定的參考價(jià)值,下面跟著小編一起來看下吧2016-12-12

