PC端模态弹框

20200530-8.png

    

    弹框在页面开发中很是常见,通过弹框,可以在不中断用户使用流程的前提下,告知其应该如何进行下一步操作,极大提升用户体验。下面在代码层面介绍一个弹框样例。

    最终效果:


123.png


HTML代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
  <title>模态框</title>
  <link rel="stylesheet" href="./index.css">
</head>
<body>
    <!-- 触发按钮 -->
    <button id="triggerBtn">模态框</button>
    <!-- 模态框 -->
    <div id="myModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h2>头部</h2>
                <span id="closeBtn" class="close">×</span>
            </div>
            <div class="modal-body">
                <p>这是一个模态框!</p>
                <p>喜欢就点个赞吧!</p>
            </div>
            <div class="modal-footer">
                <h3>尾部</h3>
            </div>
        </div>
    </div>
<script type="text/javascript" src="./index.js"></script>
</body>


CSS代码:

/*模态框*/
#triggerBtn{
  margin: 0 auto;
  display: block;
  margin-top: 50px;
  background: crimson;
  color: #fff;
  width: 100px;
  height: 50px;
  border: none;
  outline: none;
  border-radius: 6px;
  font-size: 20px;
  cursor: pointer;
}
#triggerBtn:hover{
  opacity: 0.7;
}
.modal {
    display: none; /* 默认隐藏 */
    position: fixed; /* 根据浏览器定位 */
    z-index: 1; /* 放在顶部 */
    left: 0;
    top: 0;
    width: 100%; /* 全宽 */
    height: 100%; /* 全高 */
    overflow: auto; /* 允许滚动 */
    background-color: rgba(0,0,0,0.4); /* 背景色 */
}
/*模态框内容*/
.modal-content {
    display: flex; /*采用flexbox布局*/
    flex-direction: column; /*垂直排列*/
    position: relative;
    background-color: #fefefe;
    margin: 15% auto; /*距顶部15% 水平居中*/
    padding: 20px;
    border: 1px solid #888;
    width: 80%;
    animation: topDown 0.4s; /*自定义动画,从模态框内容上到下出现*/
}
@keyframes topDown {
    from {top: -300px; opacity: 0}
    to {top: 0; opacity: 1}
}
/*模态框头部*/
.modal-header {
    display: flex; /*采用flexbox布局*/
    flex-direction: row; /*水平布局*/
    align-items: center; /*内容垂直居中*/
    justify-content: space-between; 
}
/*关闭X 样式*/
.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}
.close:hover {
    color: black;
    text-decoration: none;
    cursor: pointer;
}


JS代码:

(function() {
	/*建立模态框对象*/
	var modalBox = {};
	/*获取模态框*/
	modalBox.modal = document.getElementById("myModal");
    /*获得trigger按钮*/
	modalBox.triggerBtn = document.getElementById("triggerBtn");
    /*获得关闭按钮*/
	modalBox.closeBtn = document.getElementById("closeBtn");
	/*模态框显示*/
	modalBox.show = function() {
		console.log(this.modal);
		this.modal.style.display = "block";
	}
	/*模态框关闭*/
	modalBox.close = function() {
		this.modal.style.display = "none";
	}
	/*当用户点击模态框内容之外的区域,模态框也会关闭*/
	modalBox.outsideClick = function() {
		var modal = this.modal;
		window.onclick = function(event) {
            if(event.target == modal) {
            	modal.style.display = "none";
            }
		}
	}
    /*模态框初始化*/
	modalBox.init = function() {
		var that = this;
		this.triggerBtn.onclick = function() {
            that.show();
		}
		this.closeBtn.onclick = function() {
			that.close();
		}
		this.outsideClick();
	}
	modalBox.init();
 
})();


CSS代码中已经添加了动画效果,下面再介绍一些其他的动画效果:

/*弹层动画(从上往下)*/

.fadeIn {
    -webkit-animation: fadeInDown .3s;
    animation: fadeInDown .3s;
}

@keyframes fadeInDown {
    0% {
        -webkit-transform: translate3d(0, -20%, 0);
        -webkit-transform: translate3d(0, -20%, 0);
        transform: translate3d(0, -20%, 0);
        transform: translate3d(0, -20%, 0);
        opacity: 0;
    }
    100% {
        -webkit-transform: none;
        transform: none;
        opacity: 1;
    }
}

@-webkit-keyframes fadeInDown {
    0% {
        -webkit-transform: translate3d(0, -20%, 0);
        opacity: 0;
    }
    100% {
        -webkit-transform: none;
        opacity: 1;
    }
}


/*弹层动画(从下往上)*/

.fadelogIn {
    -webkit-animation: fadelogIn .4s;
    animation: fadelogIn .4s;
}

@keyframes fadelogIn {
    0% {
        -webkit-transform: translate3d(0, 100%, 0);
        -webkit-transform: translate3d(0, 100%, 0);
        transform: translate3d(0, 100%, 0);
        transform: translate3d(0, 100%, 0);
    }
    100% {
        -webkit-transform: none;
        transform: none;
    }
}

@-webkit-keyframes fadelogIn {
    0% {
        -webkit-transform: translate3d(0, 100%, 0);
    }
    100% {
        -webkit-transform: none;
    }
}


/*弹层动画(从右往左)*/

.fadeleftIn {
    -webkit-animation: fadeleftIn .4s;
    animation: fadeleftIn .4s;
}

@keyframes fadeleftIn {
    0% {
        -webkit-transform: translate3d(100%, 0, 0);
        -webkit-transform: translate3d(100%, 0, 0);
        transform: translate3d(100%, 0, 0);
        transform: translate3d(100%, 0, 0);
    }
    100% {
        -webkit-transform: none;
        transform: none;
    }
}

@-webkit-keyframes fadeleftIn {
    0% {
        -webkit-transform: translate3d(100%, 0, 0);
    }
    100% {
        -webkit-transform: none;
    }
}


/*弹层动画(放大)*/

.popIn {
    -webkit-animation: fadeleftIn .4s;
    animation: fadeleftIn .4s;
    -webkit-animation-name: popIn;
    animation-name: popIn;
}

@-webkit-keyframes popIn {
    0% {
        -webkit-transform: scale3d(0, 0, 0);
        transform: scale3d(0.5, 0.5, 0.5);
        opacity: 0;
    }
    50% {
        -webkit-animation-timing-function: cubic-bezier(0.47, 0, 0.745, 0.715);
        animation-timing-function: cubic-bezier(0.47, 0, 0.745, 0.715);
    }
    100% {
        -webkit-transform: scale3d(1, 1, 1);
        transform: scale3d(1, 1, 1);
        -webkit-animation-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94);
        animation-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94);
        opacity: 1;
    }
}

@keyframes popIn {
    0% {
        -webkit-transform: scale3d(0, 0, 0);
        transform: scale3d(0.5, 0.5, 0.5);
        opacity: 0;
    }
    50% {
        -webkit-animation-timing-function: cubic-bezier(0.47, 0, 0.745, 0.715);
        animation-timing-function: cubic-bezier(0.47, 0, 0.745, 0.715);
    }
    100% {
        -webkit-transform: scale3d(1, 1, 1);
        transform: scale3d(1, 1, 1);
        -webkit-animation-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94);
        animation-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94);
        opacity: 1;
    }
}

快去尝试一下吧~

对我有帮助
28人认为有帮助

相关帮助