標籤:
再探 butterfly.js - 拋磚引玉篇(notification.js) 事情經過
今天又來研究butterfly.js的源碼(打醬油),發現了一個notification.js。顧名思義notification就是公告欄嘛,那麼這個notification.js到底有什麼用途呢?題外話:還有十幾天就過年… 我的新年願望是:準時出糧!
正片
先貼:
顯然,這真的是一個顯示狀態的公告欄。萬萬沒有想到butterfly.js會內建了這類外掛程式。那個這個notification.js到底要怎麼用呢?下面只貼出關鍵代碼,詳情請自行查閱notification.js。
defaults: { duration: 2000, autoDismiss: true, type: ‘info‘, showSpin: false},
這是notification.js的預設參數,又來顧名思義,duration是時間長度,autoDismiss是自動消失,type是類型,showSpin是顯示spin(菊花圖)。
initialize: function(options) { _.extend(this, this.defaults, options); this.el.classList.add(this.type); this.el.innerHTML = options.message; document.body.appendChild(this.el); if (this.showSpin) this.spinner = new Spinner({ left: ‘20px‘, lines: 7, speed: 1.3, length: 3, radius: 3, color: ‘#666‘ }).spin(this.el);},
initialize是用來初始化的function。_.extend()這是個承繼的function,new Spinner()是用來定義菊花圖。總的來說,這個initialize就是用來處理預設參數和自訂參數。原始碼就介紹到這裡,具體還是要自行查閱,原理和實現的方法都比較簡單。
用法
建立一個butterfly.js項目:
<input type="button" value="click" class="btn">
```javascriptevents:{ ‘click .btn‘:‘cl‘},cl:function(){ new Notification({ message: ‘我是info‘, type: ‘info‘ }).show();}
點擊效果如下:
這個notification的樣式由butterfly.css內控制:
.notification { position: fixed; bottom: -40px; -webkit-transition: bottom 1.25s; -moz-transition: bottom 1.25s; transition: bottom 1.25s;}.notification.active { bottom: 0px;}
初始位置為bottom: -40px,添加.active後buttom: 0px。由於transition: bottom 1.25s;,會出現從下至上的出現效果。預設duration為2s,而且autoDismiss: true,所以在2s後公告欄會消失,移除.active。公告欄的位置由當前的0px,變為-40px。同理,因為有transition,會有一個從上至下的消失效果。
目前,notification.js提供4種type : well、info、warning、error。具體的操作,還請大家親自動手摺騰一下。
再探 butterfly.js - 拋磚引玉篇(notification.js)