怎么没有自定义html结构的模态对话框啊,还是我没找到?
asmallbird
- 发布:2016-12-08 00:51
- 更新:2016-12-08 10:35
- 阅读:2786
自定义html结构的模态对话框
分类:MUI
赵梦欢 - 专注前端,乐于分享!
用popover改造实现:
-
样式部分:
<style type="text/css"> .mui-popover{ display: none; position: fixed; width: 80%; height: auto; border-radius: 0; left: 50%; top: 50%; margin: 0 auto; z-index: 9999; background-color: #fff; -webkit-transform: translateX(-50%) translateY(-50%); -moz-transform: translateX(-50%) translateY(-50%); -ms-transform: translateX(-50%) translateY(-50%); transform: translateX(-50%) translateY(-50%); } .mui-popover-arrow{ display: none; } </style>
-
逻辑封装:
/** * 自定义弹出层 * @param {Object} opt */ var popover = function(opt) { var el; if(typeof opt === 'object'){ el = document.querySelector(opt.el); if(opt.style) { el.setAttribute('style', opt.style); } }else{ el = document.querySelector(opt); } var mask = mui.createMask(function() { el.classList.remove('mui-active'); }); this.open = function() { mask.show(); el.classList.add('mui-active'); } this.close = function() { mask.close(); el.classList.remove('mui-active'); } }
调用:
<div id="pop" class="mui-popover">
<ul class="mui-table-view">
<li class="mui-table-view-cell"><a href="#">Item1</a></li>
<li class="mui-table-view-cell"><a href="#">Item2</a></li>
<li class="mui-table-view-cell"><a href="#">Item3</a></li>
<li class="mui-table-view-cell"><a href="#">Item4</a></li>
<li class="mui-table-view-cell"><a href="#">Item5</a></li>
</ul>
</div>
<div class="mui-content mui-content-padded">
<button id="openPopover" type="button" class="mui-btn mui-btn-blue mui-btn-block">打开弹出菜单</button>
</div>
<script src="./js/mui.js"></script>
<script src="./js/popover.js"></script>
<script type="text/javascript">
var pop = new popover('#pop');
document.querySelector('#openPopover').addEventListener('tap',function () {
pop.open();
})
mui('.mui-table-view').on('tap','li a',function(){
mui.toast(this.innerHTML);
pop.close();
})
</script>
预览地址:demo
asmallbird (作者)
这么做会影响tocast等弹出框的样式
2016-12-08 11:41
赵梦欢
回复 asmallbird: 你也可以改一下类名,不用.mui-popover
2016-12-08 12:06