AngularJs被用來(lái)開(kāi)發(fā)單頁(yè)面應(yīng)用程序(SPA),利用AJAX調(diào)用配合頁(yè)面的局部刷新,可以減少頁(yè)面跳轉(zhuǎn),從而獲得更好的用戶(hù)體驗(yàn)。Angular的ngView及其對(duì)應(yīng)的強(qiáng)大路由機(jī)制,是實(shí)現(xiàn)SPA應(yīng)用的核心模塊。本文所說(shuō)的頁(yè)面切換指的就是這個(gè)路由機(jī)制,即根據(jù)不同的url展示不同的視圖。
前端開(kāi)發(fā)中,為了對(duì)列表項(xiàng)進(jìn)行快捷操作,有時(shí)就添個(gè)按鈕來(lái)簡(jiǎn)單實(shí)現(xiàn)。但是,有時(shí)會(huì)發(fā)現(xiàn)按鈕影響美觀,甚至影響列表行的布局。稍在網(wǎng)上搜索無(wú)果,而寫(xiě)此仿蘋(píng)果滑屏刪除控件。
依賴(lài)項(xiàng):angularJS、jQuery
測(cè)試瀏覽器:Chrome、IE11、手機(jī)瀏覽器
原先列表項(xiàng)代碼:
<div class="row-class" ng-repeat="item in list">這是整行顯示的內(nèi)容</div>
開(kāi)發(fā)目標(biāo):
<div class="row-class" ng-repeat="item in list" slide-delete text="刪除" ondelete="ondelete(item)">這是整行顯示的內(nèi)容</div>
首先,要寫(xiě)個(gè)angular指令:
myapp.directive('slideDelete', function() {return {restrict: 'AE',scope: {text: "@",ondelete: "&"},link: function (scope, element, attrs) {var w = $(element).outerWidth ();//應(yīng)顯示的寬度var h = $(element).outerHeight();//應(yīng)顯示的高度//按鈕寬度var btn_w = 60;//設(shè)計(jì)按鈕:scope.btn = $('<div style="position:absolute;z-index:5998;right:0;top:0;width:'+btn_w+'px;height:'+h+'px;color:#fff;background-color:#900;text-align:center;padding-top:10px">'+(scope.text||'刪除')+'</div>');//改造行,用一個(gè)絕對(duì)定位div將內(nèi)容包裹起來(lái)$(element).contents().wrapAll('<div new_box style="position:absolute;z-index:5999;left:0;top:0;width:'+w+'px;height:'+h+'px;background-color:#fff;"></div>');//添加按鈕:$(element).css({overflow:"hidden", position:"relative", "z-index":5999}).append(scope.btn)//滑屏功能.slideable({getLeft: function(self){return self.children(":first-child").position().left;},setLeft: function(self, x){ self.children(":first-child").css({left: x<-btn_w && -btn_w || x<0 && x || 0});},onslide: function(self, x){scope.open = x < -btn_w / 2;self.css("z-index", scope.open && 6001 || 5999);//背景,點(diǎn)擊收起var bk = $.fixedBackground(6000, scope.open);scope.open && bk.data("self", self).click(function(){var self = bk.data("self");$.fixedBackground(6000, false);self && self.css("z-index", 5999).children(":first-child").animate({left: 0},100);});self.children(":first-child").animate({left: scope.open ? -btn_w : 0},100);}})//按鈕事件scope.btn.click(function(){scope.ondelete && scope.ondelete();$.fixedBackground(6000, 1).click();//關(guān)閉背景});}};});再寫(xiě)個(gè)滑屏事件類(lèi),當(dāng)然要兼容鼠標(biāo)
(function($){$.fn.slideable = function(options){var self = this;self.options = options;self.left = 0;self.down = 0;self.pressed = false;self.bindobj = options.bindobj || self;self.bindobj.bind("mousedown",function(event){ onmousedown(self, event); })self.bindobj.bind("mousemove",function(event){ onmousemove(self, event); })self.bindobj.bind("mouseup" ,function(event){ onmouseup (self, event); })self.bindobj[0].addEventListener('touchstart', function(event) { onmousedown(self, {screenX: event.changedTouches[0].pageX}); })self.bindobj[0].addEventListener('touchmove' , function(event) { onmousemove(self, {screenX: event.changedTouches[0].pageX}); })self.bindobj[0].addEventListener('touchend' , function(event) { onmouseup (self, {screenX: event.changedTouches[0].pageX}); })return this;}function onmousedown(self, event){self.down = event.screenX;self.options.onmousedown && self.options.onmousedown(self);self.left = self.options.getLeft && self.options.getLeft(self) || 0;self.pressed = true;}function onmousemove(self, event){self.pressed && self.options.setLeft && self.options.setLeft(self, self.left + event.screenX - self.down);}function onmouseup(self, event){self.pressed = false;self.left += event.screenX - self.down;self.options.onslide && self.options.onslide(self, self.left);}//背景功能$.fixedBackground = function(z_index, b_show){var bk = $('#fixed-background-'+z_index+'');if(!b_show)return bk && bk.remove();if(!(bk && bk.length>0)){bk = $('<div id="fixed-background-'+z_index+'" style="position:fixed;z-index:'+z_index+';left:0;top:0;right:0;bottom:0;background-color:rgba(0,0,0,0)">');$("body").append(bk);}return bk;}})(jQuery);關(guān)于上述代碼給大家介紹的AngularJS仿蘋(píng)果滑屏刪除控件的相關(guān)代碼,都是小編測(cè)試過(guò)的,可以放心安全使用。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注