写了个简单的弹窗插件Prompt.js, 效果图如下

clipboard.png

功能: 仿iOS弹出效果, 背景模糊, 可传入的确定和取消回调函数等.

用法:

不带参数调用:
Prompt()

会按照默认参数和回调函数调用

带参数调用:

clipboard.png

目前有四个参数 - 标题(str), 内容(str), 确定回调(function), 取消回调(function). 其中取消回调和标题是可选的, 而内容和确定回调必填.

javascript
Prompt({content: '<span></span>确定取消关注达人?', okCallback:function(){void(0)}, cancelCallback:function(){void(0)}})
按参数自动重载

当没有传入取消回调函数时, 只会显示'确定'按钮.

代码

module.exports = window.Prompt = function(options){
    var cancelFlag = false; 
    var titleFlag = false; 
    if(options){
        options.content||(options.content = 'default content'); 
        options.okCallback||(options.okCallback = function(){
            console.log('ok chosen!'); 
        }); 
        if(options.cancelCallback){
            cancelFlag = true; 
        }; 
        if(options.title){
            titleFlag = true; 
        }; 
    }else{
        var options={
            title:'this is default title', 
            content: 'this is default content', 
            okCallback: function(){
                console.log('ok clicked!'); 
            },
            cancelCallback: function(){
                console.log('cancel clicked!'); 
            }
        }
        cancelFlag = true; 
        titleFlag = true; 
    }; 
    // debugger; 
    var shade = document.createElement('div'); 
    shade.setAttribute('style', 'background-color:rgba(0,0,0,0.6); width:100%; height: 100%; position:absolute; left:0; top:0; z-index:1024;')
    shade.classList.add('shade'); 
    var prompt = document.createElement('div'); 
    prompt.setAttribute('style', 'position:absolute; margin:auto; left:0; right:0; top:0; bottom:0; width:6rem; height:4rem; background-color:#fff; z-index:1025; transition: all .3s ease; box-sizing: content-box; padding: 0.2rem; border-radius: 10px; opacity: 0; '); 
    var innerPrompt = document.createElement('div'); 
    innerPrompt.setAttribute('style', 'height:100%; width:100%; text-align: center; font-size: 0.3rem; '); 
    innerPrompt.classList.add('innerPrompt'); 
    
    if(titleFlag){
        var title = document.createElement('p'); 
        title.innerHTML = options.title; 
        innerPrompt.appendChild(title); 
    }; 
    
    var content = document.createElement('p'); 
    content.innerHTML = options.content; 
    innerPrompt.appendChild(content); 
    
    function modualRemoval(){
        document.body.removeChild(shade); 
        var others = document.querySelectorAll('body > *'); 
        [].forEach.call(others, function(elem){
            if(elem.nodeName != 'SCRIPT'){
                elem.style.webkitFilter = ''; 
            }
        }); 
    }; 

    var buttonWrapper = document.createElement('div'); 
    
    var okBtn = document.createElement('button'); 
    okBtn.innerHTML = '确定'; 
    okBtn.addEventListener('click', function(event){
        options.okCallback(); 
        modualRemoval(); 
        event.stopPropagation(); 
    }); 
    buttonWrapper.appendChild(okBtn); 
    
    if(cancelFlag){
        var cancelBtn = document.createElement('button'); 
        cancelBtn.innerHTML = '取消'; 
        cancelBtn.addEventListener('click', function(event){
            options.cancelCallback();
            modualRemoval(); 
            event.stopPropagation(); 
        }); 
        buttonWrapper.appendChild(cancelBtn); 
    }
    innerPrompt.appendChild(buttonWrapper); 
    
    prompt.appendChild(innerPrompt); 
    shade.appendChild(prompt); 
    document.body.appendChild(shade); 
    setTimeout(function(){
        var others = document.querySelectorAll('body > *:not(.shade)'); 
        [].forEach.call(others, function(elem){
            if(elem.nodeName != 'SCRIPT'){
                elem.style.webkitFilter = 'blur(6px)'; 
            }
        })
        prompt.style.opacity = 1; 
        prompt.style.padding = 0; 
    }, 50); 
    prompt.addEventListener('click', function(event){
        event.stopPropagation(); 
    }); 
    shade.addEventListener('click', function(event){
        modualRemoval(); 
    }); 
}
div.innerPrompt{
    padding: 0.2rem; 
}
div.innerPrompt > p:first-of-type{
    margin-top: 20%; 
}
div.innerPrompt > div{
    position: absolute;
    left:0; 
    bottom: 0; 
    display: -webkit-box; 
    width: 100%; 
    border-bottom-right-radius: 10px; 

}
div.innerPrompt > div > button{
    display: block; 
    -webkit-box-flex: 1; 
    background-color: #fff; 
    border-top: 1px solid #bbb; 
    height: 0.8rem; 
}
div.innerPrompt > div > button:last-child{
    border-bottom-right-radius: 10px; 
    color: #777; 
}
div.innerPrompt > div > button:first-child{
    border-bottom-left-radius: 10px; 
    border-right: 1px solid #bbb; 
    color: red; 
}

你也可以根据你的喜好定制CSS

-EOF-


已注销
609 声望9 粉丝