uniapp定義new plus.nativeObj.View實現(xiàn)APP端全局彈窗功能
為什么要用new plus.nativeObj.View在APP端實現(xiàn)彈窗?因為uni.showModal在APP端太難看了。
AppPopupView彈窗函數(shù)參數(shù)定義
參數(shù)一:彈窗信息(所有屬性可不填,會有默認值)
1.title:"", //標題
2.content:"", //內容
3.confirmBoxColor:"", //確認按鈕背景色
4.cancelText:"", //取消按鈕文字
5.confirmText:" //確認按鈕文字"
6.showCancel: false, // 是否顯示取消按鈕 是:true(默認) 否:false
7.maskClick: true // 是否允許點擊遮罩層關閉彈窗 是:true 否:false(默認)
參數(shù)二(cd1):點擊確認按鈕執(zhí)行邏輯,
參數(shù)三(cd2):點擊取消按鈕執(zhí)行邏輯。
/utils/AppPopupView.js 定義彈窗文件
export default AppPopupView function AppPopupView({ title = '提示', content = '', confirmBoxColor = '#41a863', cancelText = "取消", confirmText = "確認", showCancel = true, maskClick = false, } = {}, cd1, cd2) { let screenWidth = plus.screen.resolutionWidth let screenHeight = plus.screen.resolutionHeight const popupViewWidth = screenWidth * 0.7 const popupViewHeight = 80 + 20 + 20 + 90 + 10 const viewContentPadding = 20 const viewContentWidth = parseInt(popupViewWidth - (viewContentPadding * 2)) let maskLayer = new plus.nativeObj.View('maskLayer', { top: '0px', left: '0px', height: '100%', width: '100%', backgroundColor: 'rgba(0,0,0,0.2)' }) let popupViewContentList = [{ tag: 'font', id: 'title', text: title, textStyles: { size: '18px', color: "#333", weight: "bold", whiteSpace: "normal" }, position: { top: '55px', left: viewContentPadding + "px", width: viewContentWidth + "px", height: "30px", } }] popupViewContentList.push({ tag: 'font', id: 'content', text: content || '確認要操作嗎?', textStyles: { size: '14px', color: "#666", lineSpacing: "50%", align: "left" }, position: { top: "100px", left: viewContentPadding + "px", width: viewContentWidth + "px", height: "18px", } }); if (showCancel === true) { // 添加取消按鈕 popupViewContentList.push({ tag: 'rect', id: 'cancelBox', rectStyles: { radius: "3px", borderColor: "#f1f1f1", borderWidth: "1px", }, position: { bottom: viewContentPadding + 'px', left: viewContentPadding + "px", width: (viewContentWidth - viewContentPadding) / 2 + "px", height: "30px", } }) popupViewContentList.push({ tag: 'font', id: 'cancelText', text: cancelText, textStyles: { size: '14px', color: "#666", lineSpacing: "0%", whiteSpace: "normal" }, position: { bottom: viewContentPadding + 'px', left: viewContentPadding + "px", width: (viewContentWidth - viewContentPadding) / 2 + "px", height: "30px", } }) } popupViewContentList.push({ tag: 'rect', id: 'confirmBox', rectStyles: { radius: "3px", color: confirmBoxColor, }, position: { bottom: viewContentPadding + 'px', left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px", width: (viewContentWidth - viewContentPadding) / 2 + "px", height: "30px", } }) popupViewContentList.push({ tag: 'font', id: 'confirmText', text: confirmText || '確認', textStyles: { size: '14px', color: "#FFF", lineSpacing: "0%", whiteSpace: "normal" }, position: { bottom: viewContentPadding + 'px', left: ((viewContentWidth - viewContentPadding) / 2 + viewContentPadding * 2) + "px", width: (viewContentWidth - viewContentPadding) / 2 + "px", height: "30px", } }) if (showCancel === false) { // 如果只顯示確認按鈕需要重新設置按鈕的width和left for (let i = 0; i < popupViewContentList.length; i++) { let item = popupViewContentList[i] if (item.id === 'confirmBox' || item.id === 'confirmText') { item.position.left = viewContentPadding + "px" item.position.width = viewContentWidth + "px" } } } let popupView = new plus.nativeObj.View("popupView", { //創(chuàng)建底部圖標菜單 tag: "rect", top: (screenHeight - popupViewHeight) / 2 + "px", left: '15%', height: popupViewHeight + "px", width: "70%" }) popupView.drawRect({ color: "#FFFFFF", radius: "20px" }, { top: "40px", height: popupViewHeight - 40 + "px", }) popupView.addEventListener("click", e => { let maxTop = popupViewHeight - viewContentPadding let maxLeft = popupViewWidth - viewContentPadding let buttonWidth = (viewContentWidth - viewContentPadding) / 2 if (e.clientY > maxTop - 30 && e.clientY < maxTop) { let maxTop = popupViewHeight - viewContentPadding; let maxLeft = popupViewWidth - viewContentPadding; let buttonWidth = (viewContentWidth - viewContentPadding) / 2; if (showCancel) { if (e.clientY > maxTop - 30 && e.clientY < maxTop) { if (e.clientX > viewContentPadding && e.clientX < maxLeft - buttonWidth - viewContentPadding) { // console.log("取消"); maskLayer.hide() popupView.hide() cd2 && cd2() } if (e.clientX > maxLeft - buttonWidth && e.clientX < maxLeft) { // console.log("確認"); maskLayer.hide() popupView.hide() cd1 && cd1() } } } else { maskLayer.hide() popupView.hide() cd1 && cd1() } } }) popupView.draw(popupViewContentList) // 點擊遮罩層 maskLayer.addEventListener("click", function() { //處理遮罩層點擊 if (maskClick) { maskLayer.hide(); popupView.hide(); } }) // 顯示彈窗 maskLayer.show() popupView.show() }
在main.js掛載到全局
// #ifdef APP-PLUS import AppPopupView from '@/utils/AppPopupView.js'; Vue.prototype.AppPopupView = AppPopupView; // #endif
頁面調用全局彈窗
<script> export default { onLoad() { // #ifdef APP-PLUS // 參數(shù)一:彈窗信息(所有屬性可不填,會有默認值) let AppPopupInfo = { // title:"", //標題 // content:"", //內容 // confirmBoxColor:"", //確認按鈕背景色 // cancelText:"", //取消按鈕文字 // confirmText:" //確認按鈕文字", // showCancel: false, // 是否顯示取消按鈕 是:true(默認) 否:false // maskClick: true // 是否允許點擊遮罩層關閉彈窗 是:true 否:false(默認) } // 參數(shù)二:點擊確認按鈕執(zhí)行邏輯 const affirmBtn = () => { console.log("點擊了確認按鈕"); } // 參數(shù)三:點擊取消按鈕執(zhí)行邏輯 const cancelBtn = () => { console.log("點擊了取消按鈕"); } this.AppPopupView(AppPopupInfo, affirmBtn, cancelBtn) // #endif }, } </script>
效果圖
到此這篇關于uniapp定義new plus.nativeObj.View實現(xiàn)APP端全局彈窗的文章就介紹到這了,更多相關uniapp全局彈窗內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vxe-table?實現(xiàn)表格數(shù)據(jù)分組功能(按指定字段數(shù)據(jù)分組)
文章介紹了如何使用樹結構實現(xiàn)表格數(shù)據(jù)分組,并提供了官方文檔的鏈接,本文結合實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-11-11electron-vite工具打包后如何通過內置配置文件動態(tài)修改接口地址
使用electron-vite?工具開發(fā)項目打包完后每次要改接口地址都要重新打包,對于多環(huán)境切換或者頻繁變更接口地址就顯得麻煩,這篇文章主要介紹了electron-vite工具打包后通過內置配置文件動態(tài)修改接口地址實現(xiàn)方法,需要的朋友可以參考下2024-05-05Vue+elementUI實現(xiàn)多圖片上傳與回顯功能(含回顯后繼續(xù)上傳或刪除)
這篇文章主要介紹了Vue+elementUI實現(xiàn)多圖片上傳與回顯功能(含回顯后繼續(xù)上傳或刪除),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03vue后臺系統(tǒng)管理項目之角色權限分配管理功能(示例詳解)
這篇文章主要介紹了vue后臺系統(tǒng)管理項目-角色權限分配管理功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09vue前端測試開發(fā)watch監(jiān)聽data的數(shù)據(jù)變化
這篇文章主要為大家介紹了vue測試開發(fā)watch監(jiān)聽data的數(shù)據(jù)變化,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05