HTML 按钮和 div 出现在导航栏上,但使用 z-index 会破坏动画

标签 html css

body {
    background-color: rgb(30, 21, 120);
    margin: 0;
    padding: 0;
}

p {
  color: white;
  font-size: 20px;
  display: inline;
  padding: 20px;
}

div.package {
    position: relative;
    border: 4px solid white;
    border-radius: 70px;
    margin: 60px 0 60px 0;
    background-color: rgb(37, 110, 194);
    padding: 40px;
    transition-duration: 0.4s;
}
p.package {
    color: white;
    padding-left: 3vw;
    font-size: 2vw;
    text-align: center;
    }
div.package:hover {
    box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 35px 60px 0 
    rgba(0,0,0,0.19);
}
.purchase {
    float: right;
    width: 20vw;
    height: 75px;
    background-color: rgb(10,27,64);
    color: white;
    border-color: rgb(24,146,40);
    position: relative;
    bottom: 17px;
    border-radius: 30px;
    border-style: solid;
    border-width: 5px;
    transition-duration: 0.4s;
    font-size: 2vw;
}
.purchase:hover {
    z-index: 2;
    background-color: rgb(116,164,242);
    color: black;
    width: 24vw;
}
.purchase_b {
    display: none;
}
.navbar {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: rgb(10,10,10);
    top: 0;
}

.navcont {
    margin: 0;
    position: relative;
    float: left;
}

.navcont a {
    margin: 0;
    position: relative;
    display: block;
    color: white;
    font-size: 20px;
    text-align: center;
    padding: 5vh 7vw;
    text-decoration: none;
    border-right: 1px solid rgb(50,50,50);
    border-left: 1px solid rgb(50,50,50);
}
.navcont a:hover {
    transition-duration: 0.3s;
    background-color: rgb(30,30,30);
}
.navcont a:active {
    background-color: rgb(9,194,36);
}
#navbarIMG {
    margin: 0;
}
.sticky {
    position: sticky;
    position: -webkit-sticky;
    top: 0;
}
@media screen and (max-width: 600px) {
    .purchase_b {
        position: absolute;
        text-align: center;
        display: block;
    }
    button.purchase {
        width: 100%;
        bottom: 10px;
    }
    p.package {
        bottom: 100px;
        font-size: 2.65vw;
    }
    .package {
        height: 70px;
    }
    .purchase:hover {
        background-color: rgb(116,164,242);
        color: black;
        width: 100%;
    }
    p {
        font-size: 10px;
        padding: 0;
    }
    .navcont a {
        padding: 3vh 3vw;
    }
}
    <html>
        <head>
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>My site</title>
            <link rel="stylesheet" type="text/css" href="style.css">
        </head>
        <body>
            <div class="sticky">
                <ul class="navbar">
                    <li class="navcont"><a href="index.htm">Home</a></li>
                    <li class="navcont"><a href="#about">About</a></li>
                    <li class="navcont"><a href="#purchase">Purchase</a></li>
                    <li class="navcont" style="float: right;"><a href="#contact">Contact</a></li>
                </ul>
            </div>
            <br>
            <br>
            <br>
            <br class="purchase_b">
            <br class="purchase_b">
            <div class="package">
                <p class="package">Recommended Package: Standard Package | Only $20 per month!</p>
                <br class="purchase_b" />
                <br class="purchase_b" />
                <button value="Purchase" class="purchase" style="cursor: pointer;">Purchase</button>
            </div>
            <div class="package">
                <p class="package">Popular Choice: Premium Package | Only $35 per month!</p>
                <br class="purchase_b" />
                <br class="purchase_b" />
                <button value="Purchase" class="purchase" style="cursor: pointer;">Purchase</button>
            </div>
        </body>
    </html>

所以我目前在导航栏顶部出现购买框和购买按钮时遇到问题。我尝试使用 Z-index,但是,购买框和按钮的动画都停止工作了。有什么办法可以让购买框和按钮出现在导航栏后面?谢谢!

我向下滚动了一下,购买框越过导航栏。

最佳答案

我已经将 z-index:3 添加到 .sticky 类中,请看下面的代码片段。

body {
    background-color: rgb(30, 21, 120);
    margin: 0;
    padding: 0;
}

p {
  color: white;
  font-size: 20px;
  display: inline;
  padding: 20px;
}

div.package {
    position: relative;
    border: 4px solid white;
    border-radius: 70px;
    margin: 60px 0 60px 0;
    background-color: rgb(37, 110, 194);
    padding: 40px;
    transition-duration: 0.4s;
}
p.package {
    color: white;
    padding-left: 3vw;
    font-size: 2vw;
    text-align: center;
    }
div.package:hover {
    box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 35px 60px 0 
    rgba(0,0,0,0.19);
}
.purchase {
    float: right;
    width: 20vw;
    height: 75px;
    background-color: rgb(10,27,64);
    color: white;
    border-color: rgb(24,146,40);
    position: relative;
    bottom: 17px;
    border-radius: 30px;
    border-style: solid;
    border-width: 5px;
    transition-duration: 0.4s;
    font-size: 2vw;
}
.purchase:hover {
    z-index: 2;
    background-color: rgb(116,164,242);
    color: black;
    width: 24vw;
}
.purchase_b {
    display: none;
}
.navbar {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: rgb(10,10,10);
    top: 0;
}

.navcont {
    margin: 0;
    position: relative;
    float: left;
}

.navcont a {
    margin: 0;
    position: relative;
    display: block;
    color: white;
    font-size: 20px;
    text-align: center;
    padding: 5vh 7vw;
    text-decoration: none;
    border-right: 1px solid rgb(50,50,50);
    border-left: 1px solid rgb(50,50,50);
}
.navcont a:hover {
    transition-duration: 0.3s;
    background-color: rgb(30,30,30);
}
.navcont a:active {
    background-color: rgb(9,194,36);
}
#navbarIMG {
    margin: 0;
}
.sticky {
    position: sticky;
    position: -webkit-sticky;
    top: 0;
    z-index:3
}
@media screen and (max-width: 600px) {
    .purchase_b {
        position: absolute;
        text-align: center;
        display: block;
    }
    button.purchase {
        width: 100%;
        bottom: 10px;
    }
    p.package {
        bottom: 100px;
        font-size: 2.65vw;
    }
    .package {
        height: 70px;
    }
    .purchase:hover {
        background-color: rgb(116,164,242);
        color: black;
        width: 100%;
    }
    p {
        font-size: 10px;
        padding: 0;
    }
    .navcont a {
        padding: 3vh 3vw;
    }
}
<html>
        <head>
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>My site</title>
            <link rel="stylesheet" type="text/css" href="style.css">
        </head>
        <body>
            <div class="sticky">
                <ul class="navbar">
                    <li class="navcont"><a href="index.htm">Home</a></li>
                    <li class="navcont"><a href="#about">About</a></li>
                    <li class="navcont"><a href="#purchase">Purchase</a></li>
                    <li class="navcont" style="float: right;"><a href="#contact">Contact</a></li>
                </ul>
            </div>
            <br>
            <br>
            <br>
            <br class="purchase_b">
            <br class="purchase_b">
            <div class="package">
                <p class="package">Recommended Package: Standard Package | Only $20 per month!</p>
                <br class="purchase_b" />
                <br class="purchase_b" />
                <button value="Purchase" class="purchase" style="cursor: pointer;">Purchase</button>
            </div>
            <div class="package">
                <p class="package">Popular Choice: Premium Package | Only $35 per month!</p>
                <br class="purchase_b" />
                <br class="purchase_b" />
                <button value="Purchase" class="purchase" style="cursor: pointer;">Purchase</button>
            </div>
            
        </body>
    </html>

关于HTML 按钮和 div 出现在导航栏上,但使用 z-index 会破坏动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55160897/

相关文章:

asp.net - 使用链接打开默认邮件客户端

jquery - 如何创建具有 "drawer-looking"行为的导航栏

javascript - React - Bootstrap 下拉菜单

javascript - 如何在 HTML 表格上使用分页?

javascript - Foundation 4 和 backbone.js 的等高柱

css - 获取 flexbox 列换行以使用全宽并最小化高度

javascript - 如何将 jQuery 倒计时附加到代码并使其工作?

html - 如何使文本左对齐但格式化为居中

javascript - 单击每个表行中的复选框启用文本框

html - IE8不透明度错误