JavaScript 自己手写实现一个弹窗

项目中有很多地方会使用到弹窗,现在咱们自己写一个有弹出效果的弹窗.
html 部分一个按钮 一个弹窗盒子 一个蒙板层代码:


<!--按钮 盒子的css-->
<style>
.btn{display: inline-block;padding: .2rem .4rem;border-radius: .1rem;border: 1px solid #dddddd;font-size: .26rem}
.confirmBox{width: 70%;margin-left: 15%;border-radius: .1rem;opacity: 0;font-size: .26rem;position: relative;z-index: 101;background-color: #FFFFFF}
.confirmBox>p{text-align: center}
.confirmBtn{width: 30%;line-height: .5rem;border: 1px solid #939393;border-radius: .1rem;margin-left: 13.3%;display: inline-block}
</stype>
<p class="btn" onclick="showBox()">弹窗1</p>
<div class="confirmBox">
    <p style="text-align: center">标题</p>
    <textarea name="" id="" cols="40" rows="3" style="border: none;width: 100%;padding: 0"> 内容部分…………</textarea>
    <p class="btnSure" onclick="sure(1)">确定</p>
    <p class="btnCancel" onclick="sure(0)">取消</p>
</div>

<!--蒙板层样式-->
<style>
    .maskModal {overflow: auto;position: fixed;top: 0;right: 0;bottom: 0;left: 0;z-index: 100;background-color: rgba(0,0,0,.5);}
</style>
<div class="maskModal" style="display: none;"></div>

js 部分当按钮被点击的时候这个弹窗的盒子出现,并且有个弹出的效果,蒙版层遮住主体部分,突出弹出部分。这个通过给弹窗盒子添加一个class来实现。弹出效果使用一个动画。

css:


.tipShow
{
    opacity:1;
    transition: opacity .5s;
    border: 1px solid #dddddd;
    animation:showPanel .5s ease
}
/**动画部分**/
@keyframes showPanel
{
    0%   {transform:scale(0.5);opacity:0.0;}
    50%  {transform:scale(1.05);opacity:1.0;}
    100% {transform:scale(1);opacity:1.0;}
}

js:


<script src="js/jquery-2.1.4.min.js"></script>
<script>
  var showBox=function () {
        $(".confirmBox").addClass('tipShow');
        $(".maskModal").show();
    }
    var sure=function (type) {
        $(".confirmBox").removeClass('tipShow');
        $(".maskModal").hide();
        if(type==1){
            console.log('点击确定要做的事')
        }else {
            console.log('点击取消')
        }
    }
    $(".maskModal").click(function () {
        $(".maskModal").hide();
        $(".confirmBox").removeClass('tipShow');
    })
</script>

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值