技術(shù)頻道導(dǎo)航
HTML/CSS
.NET技術(shù)
IIS技術(shù)
PHP技術(shù)
Js/JQuery
Photoshop
Fireworks
服務(wù)器技術(shù)
操作系統(tǒng)
網(wǎng)站運營

贊助商

分類目錄

贊助商

最新文章

搜索

純CSS3實現(xiàn)模態(tài)彈出窗口

作者:admin    時間:2021-7-26 8:29:48    瀏覽:

本文介紹如何創(chuàng)建很酷的 CSS3 模態(tài)彈出窗口(或框)。為了實現(xiàn)彈窗這樣的效果,我們可以使用jQuery或Javascript。但是,事實證明,CSS3 也擁有制作模態(tài)窗口所需的所有工具。在我們的演示中,我準備了帶有兩個彈出元素的單頁:注冊表單和登錄表單。

CSS3模態(tài)彈出窗口
CSS3模態(tài)彈出窗口

下面我們看看如何實現(xiàn)的。

步驟 1. HTML

首先,我們創(chuàng)建主要的 HTML 標記。從代碼可以看到,結(jié)構(gòu)非常簡單。這是一個帶有按鈕和兩個彈出窗口的面板。它們中的每一個都包含自己的覆蓋 DIV 元素和彈出 DIV 元素,其中包含一些內(nèi)容和“關(guān)閉”按鈕。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>CSS3 Modal Popups</title>
        <link href="css/layout.css" rel="stylesheet" type="text/css" />
        <link href="css/modal.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
       <header>
            <h2>CSS3模態(tài)窗口</h2>
        </header>

        <!-- panel with buttons -->
        <div class="main">
            <div class="panel">
                <a href="#login_form" id="login_pop">登錄</a>
                <a href="#join_form" id="join_pop">注冊</a>
            </div>

        </div>

        <!-- popup form #1 -->
        <a href="#x" class="overlay" id="login_form"></a>
        <div class="popup">
            <h2>歡迎光臨</h2>
            <p>請輸入你的登錄賬戶和密碼</p>
            <div>
                <label for="login">賬號</label>
                <input type="text" id="login" value="" />
            </div>
            <div>
                <label for="password">密碼</label>
                <input type="password" id="password" value="" />
            </div>
            <input type="button" value="登錄" />

            <a class="close" href="#close"></a>
        </div>

        <!-- popup form #2 -->
        <a href="#x" class="overlay" id="join_form"></a>
        <div class="popup">
            <h2>注冊</h2>
            <p>請輸入你的E-mail</p>
            <div>
                <label for="email">E-mail</label>
                <input type="text" id="email" value="" />
            </div>
            <div>
                <label for="pass">密碼</label>
                <input type="password" id="pass" value="" />
            </div>
            <div>
                <label for="name">姓名</label>
                <input type="text" id="name" value="" />
            </div>

            <input type="button" value="注冊" />   或   <a href="#login_form" id="login_pop">登錄</a>

            <a class="close" href="#close"></a>
        </div>
    </body>
</html>

第 2 步。 CSS

layout.css 文件主要是頁面布局樣式,文件在下載壓縮包里,這里就不貼出代碼了,這里僅貼模態(tài)彈出窗口(modal.css)的樣式:

.main {
    background: #aaa url(../images/bg.jpg) no-repeat;
    width: 800px;
    height: 600px;
    margin: 50px auto;
}
.panel {
    background-color: #444;
    height: 34px;
    padding: 10px;
}
.panel a#login_pop, .panel a#join_pop {
    border: 2px solid #aaa;
    color: #fff;
    display: block;
    float: right;
    margin-right: 10px;
    padding: 5px 10px;
    text-decoration: none;
    text-shadow: 1px 1px #000;

    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    -ms-border-radius: 10px;
    -o-border-radius: 10px;
    border-radius: 10px;
}
a#login_pop:hover, a#join_pop:hover {
    border-color: #eee;
}
.overlay {
    background-color: rgba(0, 0, 0, 0.6);
    bottom: 0;
    cursor: default;
    left: 0;
    opacity: 0;
    position: fixed;
    right: 0;
    top: 0;
    visibility: hidden;
    z-index: 1;

    -webkit-transition: opacity .5s;
    -moz-transition: opacity .5s;
    -ms-transition: opacity .5s;
    -o-transition: opacity .5s;
    transition: opacity .5s;
}
.overlay:target {
    visibility: visible;
    opacity: 1;
}
.popup {
    background-color: #fff;
    border: 3px solid #fff;
    display: inline-block;
    left: 50%;
    opacity: 0;
    padding: 15px;
    position: fixed;
    text-align: justify;
    top: 40%;
    visibility: hidden;
    z-index: 10;

    -webkit-transform: translate(-50%, -50%);
    -moz-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    -o-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);

    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    -ms-border-radius: 10px;
    -o-border-radius: 10px;
    border-radius: 10px;

    -webkit-box-shadow: 0 1px 1px 2px rgba(0, 0, 0, 0.4) inset;
    -moz-box-shadow: 0 1px 1px 2px rgba(0, 0, 0, 0.4) inset;
    -ms-box-shadow: 0 1px 1px 2px rgba(0, 0, 0, 0.4) inset;
    -o-box-shadow: 0 1px 1px 2px rgba(0, 0, 0, 0.4) inset;
    box-shadow: 0 1px 1px 2px rgba(0, 0, 0, 0.4) inset;

    -webkit-transition: opacity .5s, top .5s;
    -moz-transition: opacity .5s, top .5s;
    -ms-transition: opacity .5s, top .5s;
    -o-transition: opacity .5s, top .5s;
    transition: opacity .5s, top .5s;
}
.overlay:target+.popup {
    top: 50%;
    opacity: 1;
    visibility: visible;
}
.close {
    background-color: rgba(0, 0, 0, 0.8);
    height: 30px;
    line-height: 30px;
    position: absolute;
    right: 0;
    text-align: center;
    text-decoration: none;
    top: -15px;
    width: 30px;

    -webkit-border-radius: 15px;
    -moz-border-radius: 15px;
    -ms-border-radius: 15px;
    -o-border-radius: 15px;
    border-radius: 15px;
}
.close:before {
    color: rgba(255, 255, 255, 0.9);
    content: "X";
    font-size: 24px;
    text-shadow: 0 -1px rgba(0, 0, 0, 0.9);
}
.close:hover {
    background-color: rgba(64, 128, 128, 0.8);
}
.popup p, .popup div {
    margin-bottom: 10px;
}
.popup label {
    display: inline-block;
    text-align: left;
    width: 120px;
}
.popup input[type="text"], .popup input[type="password"] {
    border: 1px solid;
    border-color: #999 #ccc #ccc;
    margin: 0;
    padding: 2px;

    -webkit-border-radius: 2px;
    -moz-border-radius: 2px;
    -ms-border-radius: 2px;
    -o-border-radius: 2px;
    border-radius: 2px;
}
.popup input[type="text"]:hover, .popup input[type="password"]:hover {
    border-color: #555 #888 #888;
}

execcodegetcode

結(jié)論

本文介紹了一個純 CSS3 實現(xiàn)的模態(tài)彈出窗口。

其實,在之前也介紹過模式窗口,制作得十分精美,CSS實現(xiàn)模式窗口(modal window)覆蓋層的3種方法,你也許也感興趣。

x
  • 站長推薦
/* 左側(cè)顯示文章內(nèi)容目錄 */