使用不透明度无效的显示无动画div

时间:2017-03-14 02:59:50

标签: jquery html css

问题

我有一个登录屏幕,当用户点击登录按钮时,该屏幕将覆盖屏幕。我最初将登录屏幕的不透明度设置为0并显示为无。

.login-screen {

    display: none;
    opacity: 0;
    z-index: 1;
    height: 100vh;
    width: 100vw;
    background: #3472FF;
    color: white;
    font-family: 'Roboto', 'sans-serif';
}

在我的Javascript中,我首先显示div,然后将不透明度设置为1:

 $(".login").on("click", function () {
        $(".login-screen").show(300, function () {
            $(".login-screen").css("opacity", "1");
        });
    });

然而,它并没有像我想的那样顺利过渡。它做了这个奇怪的事情(see here),它在登录屏幕淡入时调整其大小。但是,它会干净地淡出。

我想要的是在淡出淡出时保持同样干净的淡入淡出。我不希望登录屏幕中的div移动,我只是希望它们在点击时淡入。

login-screen以来,我还需要从display: none z-index: 1开始,以便我可以实际点击登录按钮。这就是fadeOut()hide()显示后的原因。

如果有人能帮我解决这个问题,我将不胜感激!

更新

我已尝试在Javascript中执行此操作,但由于某些原因,当我这样做时,登录屏幕在点击登录按钮时根本不会显示。

$(".login").on("click", function () {
    $(".login-screen").show();
    $(".login-screen").fadeIn(300);
});

.login-screen{
    display: none;
    opacity: 0;
}

代码

HTML

<div class="login-screen">
        <button class="close"><span class="icon-x"></span></button>
        <form action="form.php" method="post">
            <div class="login-wrapper">
                <input type="text" name="username" placeholder="Username">
                <input type="text" name="password" placeholder="Password">
                <button type="submit" class="field submit" name="submit" value="submit">Login</button>
            </div>
        </form>
    </div>

CSS

/*Remove auto styling*/

button:focus,
input:focus {
    outline: 0;
}

.login-screen {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    display: none;
    opacity: 0;
    transition: opacity 0.5s ease;
    z-index: 1;
    height: 100vh;
    width: 100vw;
    background: #3472FF;
    color: white;
    font-family: 'Roboto', 'sans-serif';
}

.close {
    position: absolute;
    top: 20px;
    right: 20px;
    font-size: 2em;
    color: white;
}

.login-wrapper {
    margin-top: 15%;
    margin-bottom: 15%;
}

input[type="text"],
.submit {
    display: block;
    margin: 0 auto;
}

input[type="text"] {
    cursor: text;
    border: none;
    font-family: 'Roboto', sans-serif;
    font-weight: 300;
    color: #3472FF;
    font-size: 2em;
    padding: 20px 20px 20px 20px;
    margin-bottom: 40px;
    width: 60%;
}

input[type="text"]::-webkit-input-placeholder {
    font-family: 'Roboto', sans-serif;
    font-weight: 300;
    color: #3472FF;
    font-size: 1em;
}

input[type="text"]:-ms-input-placeholder {
    font-family: 'Roboto', sans-serif;
    font-weight: 400;
    color: #3472FF;
    font-size: 1em;
}

input[type="text"]:-moz-placeholder {
    font-family: 'Roboto', sans-serif;
    font-weight: 400;
    color: #3472FF;
    font-size: 1em;
}

input[type="text"]::-moz-placeholder {
    font-family: 'Roboto', sans-serif;
    font-weight: 400;
    color: #3472FF;
    font-size: 1em;
}

.submit {
    background-color: white;
    color: #3472FF;
    font-size: 2em;
    text-transform: uppercase;
    padding: 20px 20px 20px 20px;
    border-radius: 7px;
    border: none;
}

的Javascript

$(document).ready(function () {
    $(".grid").on("mouseenter", ".grid-square", function () {
        $(this).find(".delete").fadeIn(100);
        $(this).find(".edit").fadeIn(100);
    });
    $(".grid").on("mouseleave", ".grid-square", function () {
        $(this).find(".delete").fadeOut(100);
        $(this).find(".edit").fadeOut(100);
    });
    $(".closeNavBtn").on("click", function () {
        $(".sidenav").css("width", "0vw");
    });
    $(".menu").on("click", function () {
        $(".sidenav").css("width", "250px");
    });
    $(".login").on("click", function () {
        $(".login-screen").show(300, function () {
            $(".login-screen").css("opacity", "1");
        });
    });
    $(".close").on("click", function () {
        $(".login-screen").fadeOut(300, function () {
            $(".login-screen").hide();
        })
    });
});

的jsfiddle

$(document).ready(function () {
    $(".grid").on("mouseenter", ".grid-square", function () {
        $(this).find(".delete").fadeIn(100);
        $(this).find(".edit").fadeIn(100);
    });
    $(".grid").on("mouseleave", ".grid-square", function () {
        $(this).find(".delete").fadeOut(100);
        $(this).find(".edit").fadeOut(100);
    });
    $(".closeNavBtn").on("click", function () {
        $(".sidenav").css("width", "0vw");
    });
    $(".menu").on("click", function () {
        $(".sidenav").css("width", "250px");
    });
    $(".login").on("click", function () {
        $(".login-screen").show(300, function () {
            $(".login-screen").css("opacity", "1");
        });
    });
    $(".close").on("click", function () {
        $(".login-screen").fadeOut(300, function () {
            $(".login-screen").hide();
        })
    });
});
@import url('https://fonts.googleapis.com/css?family=Roboto:300,400,500');

/*
font-family: 'Roboto', sans-serif;
font-family: 'Raleway', sans-serif;
*/


/*Color palette (for reference)*/

.color-lightest {
    color: #87ABFE;
    color: rgba(135, 171, 254, 1);
}

.color-light {
    color: #5C8EFF;
    color: rgba( 92, 142, 255, 1);
}

.color-main {
    color: #3472FF;
    color: rgba( 52, 114, 255, 1);
}

.color-dark {
    color: #135AFF;
    color: rgba( 19, 90, 255, 1);
}

.color-darkest {
    color: #0147E9;
    color: rgba( 1, 71, 233, 1);
}


/*General styling & remove auto styling*/

body {
    margin: 0;
}

.sidenav {
    background-color: #f4f4f4;
}

button {
    border: none;
    font-family: 'Roboto', sans-serif;
    cursor: pointer;
    background-color: rgba( 15, 72, 204, 0);
}


/*Side nav styling*/

.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 70px;
    box-shadow: 1px 0px 10px 5px rgba(122, 122, 122, 0.5);
    -webkit-box-shadow: 1px 0px 10px 5px rgba(122, 122, 122, 0.5);
    -moz-box-shadow: 2px 5px 5px 5px rgba(122, 122, 122, 0.5);
}

.sidenav a,
.sidenav .closeNavBtn {
    text-decoration: none;
    font-family: 'Roboto', sans-serif;
    color: #7a7a7a;
    text-transform: capitalize;
}

.sidenav a {
    padding-top: 20px;
    padding-bottom: 20px;
    padding-left: 20px;
    font-size: 1.8em;
    display: block;
    transition: background 0.3s ease-in;
}

.sidenav a:hover,
.offcanvas a:focus {
    background: rgba(255, 255, 255, 0.5);
    /* For browsers that do not support gradients */
    background: -webkit-linear-gradient(rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.8));
    /* For Safari 5.1 to 6.0 */
    background: -o-linear-gradient(rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.8));
    /* For Opera 11.1 to 12.0 */
    background: -moz-linear-gradient(rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.8));
    /* For Firefox 3.6 to 15 */
    background: linear-gradient(rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.8));
    /* Standard syntax */
}

.sidenav .closeNavBtn {
    position: absolute;
    top: 20px;
    right: 20px;
    font-size: 2em;
}


/*Header styling*/

header {
    font-family: 'Roboto', sans-serif;
    font-weight: 400;
    font-size: 2em;
    width: 100vw;
    text-transform: capitalize;
    text-align: center;
    padding-top: 20px;
    padding-bottom: 20px;
}

header {
    background: #3472FF;
    color: white;
    /* For browsers that do not support gradients */
    background: -webkit-linear-gradient(#3472FF, #5C8EFF);
    /* For Safari 5.1 to 6.0 */
    background: -o-linear-gradient(#3472FF, #5C8EFF);
    /* For Opera 11.1 to 12.0 */
    background: -moz-linear-gradient(#3472FF, #5C8EFF);
    /* For Firefox 3.6 to 15 */
    background: linear-gradient(#3472FF, #5C8EFF);
    /* Standard syntax */
}

header .menu,
header .login {
    /*drag to top */
    position: absolute;
    top: 0;
    /*center menu icon on bar*/
    margin-top: 20px;
    /*formatting*/
    color: white;
    background-color: rgba( 52, 114, 255, 0);
    font-size: 1em;
}

header .menu {
    /*drag to left corner*/
    left: 0;
    margin-left: 20px;
}

header .login {
    /*drag to right corner*/
    right: 0;
    margin-right: 20px;
    text-transform: uppercase;
}

@import url('https://fonts.googleapis.com/css?family=Roboto:300i,400,400i');

/*General styling*/

body {
    font-family: 'Roboto', sans-serif;
    background-color: #efefef;
    color: #333333;
    margin: 10px 3% 0 3%;
    font-size: 100%;
    font-weight: 400;
    display: block;
}

.row {
    display: block;
}

.col {
    display: inline-block;
}

.search,
.task {
    background-color: #fafafb;
}

div.element:first-child {
    margin-top: 10px;
}

.element {
    margin-bottom: 10px;
}


/*Remove auto styling*/

button {
    border: none;
    background-color: #fafafb;
    color: #333333;
    padding: 0;
}

button:focus,
input:focus,
select:focus {
    outline: 0;
}

h1,
h2,
h3,
p {
    margin: 0;
}


/*General form  styling*/

input[type="text"],
select[name="course"],
input[name="due-date"] {
    background-color: #fafafb;
    font-family: 'Roboto', sans-serif;
    font-weight: 400;
    color: #333333;
    border: none;
}

input[type="submit"],
button {
    cursor: pointer;
}

input[type="text"]::-webkit-input-placeholder {
    font-family: 'Roboto', sans-serif;
    font-weight: 400;
    font-style: italic;
    color: #949496;
}

input[type="text"]:-ms-input-placeholder {
    font-family: 'Roboto', sans-serif;
    font-weight: 400;
    font-style: italic;
    color: #949496;
}

input[type="text"]:-moz-placeholder {
    font-family: 'Roboto', sans-serif;
    font-weight: 400;
    font-style: italic;
    color: #949496;
}

input[type="text"]::-moz-placeholder {
    font-family: 'Roboto', sans-serif;
    font-weight: 400;
    font-style: italic;
    color: #949496;
}

input[type="text"][name="note"]::-webkit-input-placeholder {
    font-weight: 300;
}

input[type="text"][name="note"]:-ms-input-placeholder {
    font-weight: 300;
}

input[type="text"][name="note"]:-moz-placeholder {
    font-weight: 300;
}

input[type="text"][name="note"]::-moz-placeholder {
    font-weight: 300;
}

input[type="text"][name="due-date"]::-webkit-input-placeholder {
    font-weight: 300;
}

input[type="text"][name="due-date"]:-ms-input-placeholder {
    font-weight: 300;
}

input[type="text"][name="due-date"]:-moz-placeholder {
    font-weight: 300;
}

input[type="text"][name="due-date"]::-moz-placeholder {
    font-weight: 300;
}

button[type="submit"] {
    font-size: 1.7em;
}


/*Datepicker styling*/


/*Set datepicker’s width, position it to the center and add drop shadow*/

.ui-datepicker {
    width: auto;
    height: auto;
    margin: 5px auto 0;
    font: .7em 'Roboto', sans-serif;
    -webkit-box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, .10);
    -moz-box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, .10);
    box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, .10);
    background-color: #fafafb;
}


/*Remove the default underline decoration from every anchor tag*/

.ui-datepicker a {
    text-decoration: none;
}


/*add 100% width for the table, so it will have the same maximum width as the wrapper above (i.e. widith: auto)*/

.ui-datepicker table {
    width: 100%;
}


/*style header*/

.ui-datepicker-header {
    text-transform: uppercase;
    background-color: #fafafb;
    font-size: 1.2em;
    color: #555555;
    font-weight: bold;
    line-height: 30px;
}


/*center the Month position.*/

.ui-datepicker-title {
    text-align: center;
}


/*Set up arrows*/

.ui-datepicker-prev,
.ui-datepicker-next {
    display: inline-block;
    width: 30px;
    height: 30px;
    text-align: center;
    cursor: pointer;
    background-repeat: no-repeat;
    line-height: 600%;
    overflow: hidden;
}


/*Replace the Next and Prev text with the sprite arrow images and adjust arrow position*/

.ui-datepicker-prev {
    float: left;
    background-position: center 10px;
    background-image: url('../images/arrow_prev.png');
    /*Images created by me (Monica Ong)*/
}

.ui-datepicker-next {
    float: right;
    background-position: center 10px;
    background-image: url('../images/arrow_next.png')
    /*Images created by me (Monica Ong)*/
}


/*Set colors of day names*/

.ui-datepicker thead {
    background-color: #efefef;
}

.ui-datepicker th {
    text-transform: uppercase;
    font-size: .7em;
    padding: 5px 0;
    color: #666666;
}


/*Style dates*/

.ui-datepicker tbody td {
    padding: 0;
}

.ui-datepicker tbody td {
    padding: 0;
}


/*Style default,  hover, and active state*/

.ui-datepicker td span,
.ui-datepicker td a {
    display: inline-block;
    font-weight: bold;
    text-align: center;
    width: 30px;
    height: 30px;
    line-height: 30px;
    color: #666666;
}

.ui-datepicker-calendar .ui-state-default {
    background: #ededed;
}

.ui-datepicker-unselectable .ui-state-default {
    background: #f4f4f4;
    color: #949496;
}

.ui-datepicker-calendar .ui-state-hover {
    background: #f7f7f7;
}

.ui-datepicker-calendar .ui-state-active {
    background: #666666;
    color: #efefef;
    position: relative;
    margin: -1px;
}

.ui-datepicker-calendar td:first-child .ui-state-active {
    width: 29px;
    margin-left: 0;
}

.ui-datepicker-calendar td:last-child .ui-state-active {
    width: 29px;
    margin-right: 0;
}

.ui-datepicker-calendar tr:last-child .ui-state-active {
    height: 29px;
    margin-bottom: 0;
}


/*Search bar styling*/

.search {
    padding: 15px 15px 15px 15px;
}

.search-header {
    padding-bottom: 10px;
}

#search,
#show-all {
    margin-right: 10px;
}

#search {
    font-size: 2.5em;
    vertical-align: middle;
}

#show-all {
    font-size: 2em;
}

input[name="search"] {
    width: 80%;
    font-size: 2em;
    height: 50px;
}

.search-header button[type="button"] {
    float: right;
}

.search-header #arrow {
    font-size: 3em;
}

.search-header .error {
    margin-left: 6%;
}

.search-details {
    margin-left: 4%;
}

.search-details h2 {
    display: inline;
}

.search-details .priority,
.search-details input[name="note"] {
    margin-left: 7px;
}

.search-details input[name="note"] {
    width: 85%;
}

.search-details .priority {
    padding-top: 0;
    padding-bottom: 0;
}

.search-details label {
    padding-bottom: 10px;
}


/*Add task styling*/

input[name="title"] {
    width: 70%;
    font-size: 2em;
}

select[name="course"],
input[name="due-date"] {
    font-size: 1.5em;
}

input[name="note"] {
    font-size: 1.15em;
    width: 100%;
}

input[name="due-date"] {
    width: 130px;
}

.error {
    color: #d11141;
}

.task-details h2 {
    margin: 0;
    display: inline;
}

.task-details label {
    margin-left: 20px;
}


/*Task styling*/

.task {
    padding-left: 3%;
    padding-right: 3%;
    margin-bottom: 10px;
}

.task:last-child {
    margin-bottom: 0px;
}

.task-title {
    padding-top: 2.5%;
}

.checkbox,
button[type="submit"] {
    font-size: 1.5em;
    padding-right: 1.5%;
}

.checkbox {
    background-color: #fafafb;
}

.task-info {
    float: right;
}

.task-info h2 {
    display: inline;
}

.task-info input[name="priority"] {
    vertical-align: top;
}

.task-info input[name="due-date"] {
    margin-bottom: 10px;
}

.due-date {
    font-weight: 300;
    font-style: italic;
}

.priority {
    font-size: 1.1em;
    padding-top: 5%;
    padding-bottom: 5%;
}

.priority-high {
    color: #d11141;
}

.priority-medium {
    color: #ffc425;
}

.priority-low {
    color: #00aedb;
}

.task-details {
    padding-top: 1%;
    padding-left: 4.5%;
    padding-bottom: 2.5%;
}

.task-date {
    padding-right: 10px;
}

.note {
    padding-top: 10px;
    font-weight: 300;
    font-style: italic;
}


/*Footer*/

footer a {
    text-decoration: none;
}

footer a:link {
    color: #333333;
}

footer a:visited {
    color: #949496;
}

footer a:hover {
    color: #949496;
}

footer a:active {
    color: #333333;
}

footer .icon-circle {
    font-size: .2em;
    color: #333333;
}

/*Remove auto styling*/

button:focus,
input:focus {
    outline: 0;
}

.login-screen {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    display: none;
    opacity: 0;
    transition: opacity 0.5s ease;
    z-index: 1;
    height: 100vh;
    width: 100vw;
    background: #3472FF;
    color: white;
    font-family: 'Roboto', 'sans-serif';
}

.close {
    position: absolute;
    top: 20px;
    right: 20px;
    font-size: 2em;
    color: white;
}

.login-wrapper {
    margin-top: 15%;
    margin-bottom: 15%;
}

input[type="text"],
.submit {
    display: block;
    margin: 0 auto;
}

input[type="text"] {
    cursor: text;
    border: none;
    font-family: 'Roboto', sans-serif;
    font-weight: 300;
    color: #3472FF;
    font-size: 2em;
    padding: 20px 20px 20px 20px;
    margin-bottom: 40px;
    width: 60%;
}

input[type="text"]::-webkit-input-placeholder {
    font-family: 'Roboto', sans-serif;
    font-weight: 300;
    color: #3472FF;
    font-size: 1em;
}

input[type="text"]:-ms-input-placeholder {
    font-family: 'Roboto', sans-serif;
    font-weight: 400;
    color: #3472FF;
    font-size: 1em;
}

input[type="text"]:-moz-placeholder {
    font-family: 'Roboto', sans-serif;
    font-weight: 400;
    color: #3472FF;
    font-size: 1em;
}

input[type="text"]::-moz-placeholder {
    font-family: 'Roboto', sans-serif;
    font-weight: 400;
    color: #3472FF;
    font-size: 1em;
}

.submit {
    background-color: white;
    color: #3472FF;
    font-size: 2em;
    text-transform: uppercase;
    padding: 20px 20px 20px 20px;
    border-radius: 7px;
    border: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<div class="sidenav">
        <button class="closeNavBtn"><span class="icon-x"></span></button> <a href="photos.html">Photos</a> <a href="index.html">Albums</a> </div>
    <header>
        <button class="menu"><span class="icon-bars"></span></button> Albums
        <button class="login">Login</button>
    </header>
    <div class="grid">
        <div class="grid-square">
            <div class="grid-info">
                <h1>Album Title</h1>
                <h2>Date</h2>
                <h2>Creator</h2></div>
            <button class="delete"><span class="icon-trash-can"></span></button>
            <button class="edit"><span class="icon-pencil"></span></button>
        </div>
        <div class="grid-square">
            <div class="grid-info">
                <h1>Album Title</h1>
                <h2>Date</h2>
                <h2>Creator</h2></div>
            <button class="delete"><span class="icon-trash-can"></span></button>
            <button class="edit"><span class="icon-pencil"></span></button>
        </div>
        <div class="grid-square">
            <div class="grid-info">
                <h1>Album Title</h1>
                <h2>Date</h2>
                <h2>Creator</h2></div>
            <button class="delete"><span class="icon-trash-can"></span></button>
            <button class="edit"><span class="icon-pencil"></span></button>
        </div>
        <div class="grid-square">
            <div class="grid-info">
                <h1>Album Title</h1>
                <h2>Date</h2>
                <h2>Creator</h2></div>
            <button class="delete"><span class="icon-trash-can"></span></button>
            <button class="edit"><span class="icon-pencil"></span></button>
        </div>
        <div class="grid-square">
            <div class="grid-info">
                <h1>Album Title</h1>
                <h2>Date</h2>
                <h2>Creator</h2></div>
            <button class="delete"><span class="icon-trash-can"></span></button>
            <button class="edit"><span class="icon-pencil"></span></button>
        </div>
    </div>
    <button class="add"><span class="icon-plus"></span></button>
    <footer></footer>
    <div class="login-screen">
        <button class="close"><span class="icon-x"></span></button>
        <form action="form.php" method="post">
            <div class="login-wrapper">
                <input type="text" name="username" placeholder="Username">
                <input type="text" name="password" placeholder="Password">
                <button type="submit" class="field submit" name="submit" value="submit">Login</button>
            </div>
        </form>
    </div>

1 个答案:

答案 0 :(得分:0)

所以,一个非常轻微的逻辑错误:

$(".login-screen").show(300, function () {
    $(".login-screen").css("opacity", "1");
});

您正在动画函数的回调中设置不透明度,这意味着动画已完成,然后您正在设置不透明度。此外,.show不依赖于不透明度。如果默认情况下您希望菜单不可见,请使用(并删除不透明度css):

display:none;