vue一步步實(shí)現(xiàn)alert功能
原生alert的缺點(diǎn)
- 會阻塞一切操作,影響用戶體驗(yàn)
- 很多瀏覽器會默認(rèn)靜止alert,例如微信。
- 原生alert框樣式丑陋。
項(xiàng)目地址: web-style 項(xiàng)目里有css樣式和vue組件。目標(biāo)是快速構(gòu)建后臺系統(tǒng)。有一定自適應(yīng)的設(shè)計。
css
思路:最外層是一個黑色透明撐滿全屏幕的div并且是fixed的div.modal-mask
。
在mask內(nèi)部是一個垂直居中的div框大小可以固定。垂直居中方法有幾種可選。我選用的是flex。關(guān)鍵性的css代碼如下
.modal-mask{ position: fixed; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(55,55,55,.6); z-index: 100; display: flex; align-items: center; justify-content: center; } .modal-confirm{ width: 400px; box-sizing: border-box; padding: 30px 40px; background-color: #fff; border-radius: 6px; } @media only screen and (max-width: 640px) { .modal-confirm{ width: 100%; margin: 0 20px; padding: 10px 20px; } }
其中modal-confirm
是alert框,有固定的寬度400px 還有padding。 然后我們做了一個小小的自適應(yīng)。 在小屏上(屏幕寬度小于640px)取消了固定寬度。減少了padding的值,看起來更小巧。
開發(fā)vue組件
vue template
首先我希望這個組件功能能像原生的alert事件一樣隨時隨地的方便調(diào)用。 不希望每次都new Vue({})一個實(shí)例。 所以我做了一些不一樣的設(shè)計。
<div class="modal-mask" v-show="show"> <div class="modal-confirm"> <h2 class="confirm-header"> <i class="iconfont icon-questioncircle"></i> {{ title }} </h2> <div class="confirm-content"> {{ content }} </div> <div class="confirm-btns"> <button class="btn" @click="op(1)">取 消</button> <button class="btn btn-primary" @click="op(2)">確 定</button> </div> </div> </div>
v-show
是控制alert組件的顯示和隱藏的指令。 {{ }}
是vue默認(rèn)的模版標(biāo)記。
@click
是綁定click事件的指令
vue data
new Vue({ el: '#V-confirm', data: { show: false, onCancel: false, onOk: false, title: '', content: '' } })
- show 是控制顯示隱藏的標(biāo)記。
- onCancel onOk 是點(diǎn)擊取消或者確定時候觸發(fā)的回調(diào)。
- title content 是alert顯示的文本。
vue methods
methods: { op(type){ this.show = false if(type == '1'){ if(this.onCancel) this.onCancel() }else{ if(this.onOk) this.onOk() } this.onCancel = false this.onOk = false document.body.style.overflow = '' }, alert(setting){ this.title = setting.title || '標(biāo)題' this.content = setting.content || '內(nèi)容' this.onOk = setting.onOk || false this.onCancel = setting.onCancel || false this.show = true document.body.style.overflow = 'hidden' } } }
- alert(setting) 方法是控制顯示alert組件的方法。接受一個object的參數(shù)配置。
- op(type) 方法是點(diǎn)擊取消和確定按鈕的時候觸發(fā)的時候。
hack代碼
var element = document.createElement('div'); element.id = 'V-confirm' element.innerHTML = template document.body.appendChild(element)
這一段代碼作用是一開始就把vue實(shí)例插入到 body
底部,方便直接 alert
調(diào)用。
加入一些動畫效果
依賴的是vue指令 transition 具體的用法教程 大家去過渡-傳送門
.modal-enter, .modal-leave { opacity: 0; } .modal-transition{ transition: all .3s ease; } .modal-enter .modal-confirm, .modal-leave .modal-confirm { transform: scale(1.1); } .modal-transition{ transition: all .3s ease; }
用法
var setting = {} setting.title = '你確定刪除嗎?' setting.content = '刪除不可以恢復(fù)...' setting.onOk = function(){} setting.onCancel = function(){} $confirm.alert(setting)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue.js基礎(chǔ)指令實(shí)例講解(各種數(shù)據(jù)綁定、表單渲染大總結(jié))
這篇文章主要為大家詳細(xì)介紹了Vue.js基礎(chǔ)指令實(shí)例,各種數(shù)據(jù)綁定、表單渲染大總結(jié),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07vue中element 的upload組件發(fā)送請求給后端操作
這篇文章主要介紹了vue中element 的upload組件發(fā)送請求給后端操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09vue2.x中keep-alive源碼解析(實(shí)例代碼)
Keep-Alive模式避免頻繁創(chuàng)建、銷毀鏈接,允許多個請求和響應(yīng)使用同一個HTTP鏈接,這篇文章主要介紹了vue2.x中keep-alive源碼解析,需要的朋友可以參考下2023-02-02vue如何設(shè)置輸入框只能輸入數(shù)字且只能輸入小數(shù)點(diǎn)后兩位,并且不能輸入減號
這篇文章主要介紹了vue如何設(shè)置輸入框只能輸入數(shù)字且只能輸入小數(shù)點(diǎn)后兩位,并且不能輸入減號問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05vue實(shí)現(xiàn)echart餅圖legend顯示百分比的方法
本文主要介紹了vue實(shí)現(xiàn)echart餅圖legend顯示百分比的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09Vue路由跳轉(zhuǎn)方式區(qū)別匯總(push,replace,go)
vue項(xiàng)目中點(diǎn)擊router-link標(biāo)簽鏈接都屬于聲明式導(dǎo)航。vue項(xiàng)目中編程式導(dǎo)航有this.$router.push(),this.$router.replace(),this.$router.go()???????。這篇文章主要介紹了Vue路由跳轉(zhuǎn)方式區(qū)別匯總(push,replace,go)2022-12-12vue 詳情跳轉(zhuǎn)至列表頁實(shí)現(xiàn)列表頁緩存
這篇文章主要介紹了vue 詳情跳轉(zhuǎn)至列表頁實(shí)現(xiàn)列表頁緩存,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Vue 使用html、css實(shí)現(xiàn)魚骨組件圖
這篇文章主要介紹了Vue 使用html、css實(shí)現(xiàn)魚骨組件圖,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-07-07