遇到一個需要自定義彈出框的項目,找到了一個感覺比較好的js代碼。收藏一下
//限制頁面是否可滾動(彈窗出現時)var doc = document.documentElement;var preventDefault = function (e) {e.preventDefault();}function stop() {doc.style.overflow = 'hidden';doc.addEventListener('touchmove',preventDefault,false);}function scroll() {doc.style.overflow = 'auto';doc.removeEventListener('touchmove',preventDefault,false);}function dialog(obj){var $body = $('body');obj.width = obj.width||'380px';//var html = ''+? ? ? ? ? ? ? ? '
' +? ? ? ? ? ? ? ? ? ? '
提示' +? ? ? ? ? ? 'X
'+? ? ? ? ? ? ? ? ? ? '
'+obj.text+'
';if(obj.button){console.log('a')html = html +? '
取消
'? ? ? ? }html = html + '
';$(html).appendTo($body)? ? ? ? stop();}function closeDialog() {$("#commonDialog").remove();scroll();}? ? dialog({? ? ? ? text: '確定刪除?
確定刪除?
',? ? ? ? button: true,? ? ? ? event: '_delete'? ? })? ? function _delete(){? ? closeDialog();? ? ? ? dialog({? ? ? ? ? ? text: '已刪除',? ? ? ? ? ? button: false? ? ? ? })? ? }