CSS 幻灯片不会滑动,而只是显示

CSS slide does not slide, but just appears

本文关键字:显示 幻灯片 CSS      更新时间:2023-09-26

我试图让div#project-wrapper在单击a.post-link时向下滑动,此时div.post-container通过JS添加fadeOutDown类并从顶部淡入。我遇到了几个问题:

1) div#project-wrapper ,它获取通过 JS 添加的activated类,在单击a.post-link时不会向下滑动。相反,它只是出现

2)fadeOutDown类被添加到.post-container但div不执行我编写的任何CSS动画。

有人可以帮我解决这个问题吗?

小提琴:http://jsfiddle.net/eLooLb4c/2/

.HTML

<div id="project-wrapper">
    <img id="loading-animation" src="http://i.imgur.com/5RMfW8P.gif">
    <div id="project-container">
        <div class="post-container fadeOutDown">
            <div id="project-left-content">
                <h1 class="entry-title">Test 1</h1>
                <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
            </div>
            <div id="project-right-content"></div>
        </div><!-- #post-## -->
    </div>
</div>
<a class="post-link" href="#">post link</a>

.CSS

#project-wrapper {
    background: #000;
    color: #fff;
    display: none;
    margin: 0 1%;
    position: relative;
    transform: translateY(-100%);
    -webkit-transition: all 0.5s cubic-bezier(0, 1, 0.5, 1);
    transition: all 0.5s cubic-bezier(0, 1, 0.5, 1);
}
#project-wrapper.activated {
    display: block;
    transform: translateY(0);
}
#project-wrapper #loading-animation {
    display: none;
    left: 0;
    position: absolute;
    top: 0;
}
#project-wrapper #project-container {
    overflow: hidden;
    padding: 40px;
}
@-webkit-keyframes fadeOutDown {
  0% {
    opacity: 0;
    -webkit-transform: translate(0, -10px);
            transform: translate(0, -10px);
  }
  100% {
    opacity: 1;
    -webkit-transform: translate(0, 0);
            transform: translate(0, 0);
  }
}
@keyframes fadeOutDown {
  0% {
    opacity: 0;
    margin-bottom: -10px;
    -webkit-transform: translate(0, -10px);
            transform: translate(0, -10px);
  }
  100% {
    opacity: 1;
    -webkit-transform: translate(0, 0);
            transform: translate(0, 0);
  }
}
.fadeOutDown {
  -webkit-animation-name: fadeOutDown;
          animation-name: fadeOutDown;
}
.fadeOutDown {
    -webkit-animation-name: fadeOutDown;
    animation-name: fadeOutDown;
}
#project-wrapper #project-container .post-container #project-left-content {
    float: left;
    margin-right: 4%;
    width: 40%;
}
#project-wrapper #project-container .post-container h1 {
    font-family: Helvetica,sans-serif;
    text-transform: uppercase;
    font-size: 40px;
    font-size: 3em;
    font-weight: bold;
    line-height: 1.1;
    margin-bottom: 0.8em;
}
#project-wrapper #project-container .post-container #project-right-content {
    background: #222;
    float: left;
    height: 350px;
    margin-top: 10px;
    width: 56%;
}

.JS

$('.post-link').click(function(e) {
    e.preventDefault();
    function projectShow() {
        $('#project-wrapper').addClass('activated');
        $('.post-container').addClass('fadeOutDown');
    }
    if ($(window).scrollTop() != 0) {
        $('html, body').animate({
            scrollTop : 0
        },500, projectShow);
    } else {
        projectShow();
    }
});

这并不能解决第一个问题,但第二个问题是因为你没有给动画时间。看到编辑我也想出了第一部分小提琴

$('.post-link').click(function(e) {
    e.preventDefault();
    
    function projectShow() {
        $('#project-wrapper').addClass('activated' );
        $('.post-container').addClass('fadeOutDown');
    }
    
    if ($(window).scrollTop() != 0) {
        $('html, body').animate({
            scrollTop : 0
        },500, projectShow);
    } else {
        projectShow();
    }
});
#project-wrapper {
    background: #000;
    color: #fff;
    display: none;
    margin: 0 1%;
    position: relative;
    transform: translateY(-100%);
    -webkit-transition: all 0.5s cubic-bezier(0, 1, 0.5, 1);
    transition: all 0.5s cubic-bezier(0, 1, 0.5, 1);
}
#project-wrapper.activated {
    
    display: block;
    transform: translateY(0);
}
#project-wrapper #loading-animation {
    display: none;
    left: 0;
    position: absolute;
    top: 0;
}
#project-wrapper #project-container {
    overflow: hidden;
    padding: 40px;
}
@-webkit-keyframes fadeOutDown {
  0% {
    opacity: 0;
    -webkit-transform: translate(0, -10px);
            transform: translate(0, -10px);
  }
  100% {
    opacity: 1;
    -webkit-transform: translate(0, 0);
            transform: translate(0, 0);
  }
}
@keyframes fadeOutDown {
  0% {
    opacity: 0;
    margin-bottom: -10px;
    -webkit-transform: translate(0, -10px);
            transform: translate(0, -10px);
  }
  100% {
    opacity: 1;
    -webkit-transform: translate(0, 0);
            transform: translate(0, 0);
  }
}
.fadeOutDown {
  -webkit-animation-name: fadeOutDown 5s;
          animation-name: fadeOutDown;
}
.fadeOutDown {
    -webkit-animation: fadeOutDown 5s;
    animation-name: fadeOutDown;
}
#project-wrapper #project-container .post-container #project-left-content {
    float: left;
    margin-right: 4%;
    width: 40%;
}
#project-wrapper #project-container .post-container h1 {
    font-family: Helvetica,sans-serif;
    text-transform: uppercase;
    font-size: 40px;
    font-size: 3em;
    font-weight: bold;
    line-height: 1.1;
    margin-bottom: 0.8em;
}
#project-wrapper #project-container .post-container #project-right-content {
    background: #222;
    float: left;
    height: 350px;
    margin-top: 10px;
    width: 56%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="project-wrapper">
    <img id="loading-animation" src="http://i.imgur.com/5RMfW8P.gif">
	<div id="project-container">
        <div class="post-container">
    	    <div id="project-left-content">
	            <h1 class="entry-title">Test 1</h1>
                <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
            </div>
            <div id="project-right-content"></div>
        </div><!-- #post-## -->
    </div>
</div>
<a class="post-link" href="#">post link</a>

编辑 小提琴

我从这里删除了display:block

#project-wrapper.activated {
transform: translateY(0);
}

我像这样编辑了jQuery。

 function projectShow() {
    $('#project-wrapper').show(500);
    $('#project-wrapper').addClass('activated');
    $('.post-container').addClass('fadeOutDown');
}

$('.post-link').click(function (e) {
    e.preventDefault();
    function projectShow() {
        $('#project-wrapper').show(500);
        $('#project-wrapper').addClass('activated');
        $('.post-container').addClass('fadeOutDown');
    }
    if ($(window).scrollTop() != 0) {
        $('html, body').animate({
            scrollTop: 0
        }, 500, projectShow);
    } else {
        projectShow();
    }
});
#project-wrapper {
    background: #000;
    color: #fff;
    display: none;
    margin: 0 1%;
    position: relative;
    transform: translateY(-100%);
    -webkit-transition: all 0.5s cubic-bezier(0, 1, 0.5, 1);
    transition: all 0.5s cubic-bezier(0, 1, 0.5, 1);
}
#project-wrapper.activated {
    
    transform: translateY(0);
}
#project-wrapper #loading-animation {
    display: none;
    left: 0;
    position: absolute;
    top: 0;
}
#project-wrapper #project-container {
    overflow: hidden;
    padding: 40px;
}
@-webkit-keyframes fadeOutDown {
    0% {
        opacity: 0;
        -webkit-transform: translate(0, -10px);
        transform: translate(0, -10px);
    }
    100% {
        opacity: 1;
        -webkit-transform: translate(0, 0);
        transform: translate(0, 0);
    }
}
@keyframes fadeOutDown {
    0% {
        opacity: 0;
        margin-bottom: -10px;
        -webkit-transform: translate(0, -10px);
        transform: translate(0, -10px);
    }
    100% {
        opacity: 1;
        -webkit-transform: translate(0, 0);
        transform: translate(0, 0);
    }
}
.fadeOutDown {
    -webkit-animation: fadeOutDown 5s;
    animation: fadeOutDown 5s;
}
#project-wrapper #project-container .post-container #project-left-content {
    float: left;
    margin-right: 4%;
    width: 40%;
}
#project-wrapper #project-container .post-container h1 {
    font-family: Helvetica, sans-serif;
    text-transform: uppercase;
    font-size: 40px;
    font-size: 3em;
    font-weight: bold;
    line-height: 1.1;
    margin-bottom: 0.8em;
}
#project-wrapper #project-container .post-container #project-right-content {
    background: #222;
    float: left;
    height: 350px;
    margin-top: 10px;
    width: 56%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="project-wrapper">
    <img id="loading-animation" src="http://i.imgur.com/5RMfW8P.gif">
    <div id="project-container">
        <div class="post-container">
            <div id="project-left-content">
                 <h1 class="entry-title">Test 1</h1>
                <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
            </div>
            <div id="project-right-content"></div>
        </div>
        <!-- #post-## -->
    </div>
</div>
<a class="post-link" href="#">post link</a>

由于您使用的是jquery,因此您可以省去一些麻烦和所有这些css,并使用如下所示的内容:

function projectShow() {
    $('#project-wrapper').slideDown();
    $('.post-container').hide().fadeIn();
}

小提琴:更新示例