任何人都可以在重新出现的动画完成后第3次点击切换开关.project-list
时,让我深入了解元素为什么会消失(隐藏)。我的if / else语句中有缺陷吗?
这是动画类slideDown
(消失)和slideUp
的CSS。
http://www.justinaguilar.com/animations/css/animations.css
var main = function() {
var enabled = false;
//$('#project-box').hide();
$('.project-list').click(function() {
debugger;
if (enabled == false) {
$('#project-box').addClass("slideUp");
$('#project-box').removeClass("slideDown");
enabled = true;
$('#project-box').show();
} else {
$('#project-box').removeClass("slideUp");
$('#project-box').addClass("slideDown");
enabled = false;
$("#project-box").on('webkitAnimationEnd', function() {
$('#project-box').hide();
});
}
//$('#project-box').toggleClass("slideUp");
});
// $(window).scroll(function () {
// $('#project-box').each(function () {
// var imagePos = $(this).offset().top;
//
// var topOfWindow = $(window).scrollTop();
// if (imagePos < topOfWindow + 400) {
// $(this).addClass("slideUp");
// }
// });
// });
//$('#carousel').carousel('cycle');
}
$(document).ready(main);
/* Exit Animation
-------------------------------------------------- */
.slideDownEdit {
animation-name: slideDown;
-webkit-animation-name: slideDown;
animation-duration: 1s;
-webkit-animation-duration: 1s;
animation-timing-function: ease;
-webkit-animation-timing-function: ease;
visibility: hidden !important;
}
@keyframes slideDown {
0% {
transform: translateY(-100%);
}
50% {
transform: translateY(8%);
}
65% {
transform: translateY(-4%);
}
80% {
transform: translateY(4%);
}
95% {
transform: translateY(-2%);
}
100% {
transform: translateY(0%);
}
}
@-webkit-keyframes slideDown {
0% {
-webkit-transform: translateY(0%);
}
100% {
-webkit-transform: translateY(500%);
visibility: hidden;
}
}
/* Footer
-------------------------------------------------- */
.footer {
background-color: #dddee1;
bottom: 0;
width: 100%;
/* Set the fixed height of the footer here */
height: 60px;
border: 1px solid grey;
}
.footer p {
font-size: 18px;
margin-top: 10px;
color: #777;
position: absolute;
}
.footer a {
color: #b55e5e;
font-size: 16px;
margin-top: 10px;
}
.footer .pull-right {
color: #b55e5e;
font-size: 16px;
margin-top: 10px;
right: 10%;
}
/* Featurettes
------------------------- */
.featurette-divider {
margin: 80px 0;
/* Space out the Bootstrap <hr> more */
}
/* Thin out the marketing headings */
.featurette-heading {
font-weight: 300;
line-height: 1;
letter-spacing: -1px;
margin-top: 120px;
font-size: 50px;
display: inline-block;
}
/* Project Box
------------------------- */
#project-box {
text-align: center;
cursor: pointer;
border: 3px solid #636366;
cursor: pointer;
width: 75%;
margin: auto;
visibility: hidden;
}
#project-divider.featurette-divider {
margin: 10px 0;
}
#project-box a {
font-size: 30px;
margin-left: 300px;
margin-right: 100px;
color: darkred;
display: block;
margin: auto;
}
#project-box p {
font-size: 20px;
color: dimgrey;
}
.project-list {
cursor: pointer;
color: darkred;
}
.slider {
overflow-y: scroll;
max-height: 500px;
/* approximate max height */
transition-property: all;
transition-duration: .5s;
transition-timing-function: cubic-bezier(0, 1, 0.5, 1);
}
.slider.closed {
max-height: 0;
}
.slideUp {
animation-name: slideUp;
-webkit-animation-name: slideUp;
animation-duration: 1s;
-webkit-animation-duration: 1s;
animation-timing-function: ease;
-webkit-animation-timing-function: ease;
visibility: visible !important;
}
@keyframes slideUp {
0% {
transform: translateY(100%);
}
50% {
transform: translateY(-8%);
}
65% {
transform: translateY(4%);
}
80% {
transform: translateY(-4%);
}
95% {
transform: translateY(2%);
}
100% {
transform: translateY(0%);
}
}
@-webkit-keyframes slideUp {
0% {
-webkit-transform: translateY(100%);
}
50% {
-webkit-transform: translateY(-8%);
}
65% {
-webkit-transform: translateY(4%);
}
80% {
-webkit-transform: translateY(-4%);
}
95% {
-webkit-transform: translateY(2%);
}
100% {
-webkit-transform: translateY(0%);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Murray Studio</title>
<meta name="description" content="Main Page for Murray Studio">
<link rel="icon" href="http://s3.amazonaws.com/codecademy-content/courses/ltp2/img/uber/close.png">
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/animations.css">
<link rel="stylesheet" href="main.css">
</head>
<body>
<!-- Wrap the rest of the page in another container to center all the content. -->
<div class="container marketing">
<!-- START THE FEATURETTES -->
<hr class="featurette-divider">
<div class="row featurette">
<div class="first-featurette">
<div class="col-md-7">
<a class="featurette-heading" href="#">Projects.</a>
<p class="lead">Donec ullamcorper nulla non metus auctor fringilla. Vestibulum id ligula porta felis euismod semper. Praesent commodo cursus magna, vel scelerisque nisl consectetur. Fusce dapibus, tellus ac cursus commodo.</p>
<p class="project-list">See Projects.</p>
<div id="object" class="slideUp">
<div id="object" class="slideDownEdit">
<div id="project-box">
<a>Embraer adds third Legacy 500 prototype to flight test campaign</a>
<hr class="featurette-divider" id="project-divider">
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
</div>
</div>
</div>
</div>
<div class="col-md-5">
<img class="featurette-image img-responsive center-block" src="http://a1.dspnimg.com/data/l/423341110329_Qy737Vid_l.jpg" alt="Generic placeholder image">
</div>
</div>
</div>
<hr class="featurette-divider">
</div>
<!-- /END THE FEATURETTES -->
<!-- FOOTER -->
<footer class="footer">
<div class="container">
<p class="pull-right"><a href="#">Back to top</a>
</p>
<p>© 2014 Company, Inc. · <a href="#">Privacy</a> · <a href="#">Terms</a>
</p>
</div>
</footer>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="main.js"></script>
</body>
</html>
答案 0 :(得分:2)
您的问题是因为在webkitAnimationEnd
事件处理程序中您没有检查状态。下面的代码是一个事件处理程序,这意味着每次#project-box
上的动画结束时,该框都会被隐藏。这种情况仅在第3次发生,因为您仅在第二次单击时附加事件处理程序。
$("#project-box").on('webkitAnimationEnd', function() {
$('#project-box').hide();
});
您可以通过检查事件处理程序中的状态来修复它,如下面的代码所示:
var main = function() {
var enabled = false;
//$('#project-box').hide();
$('.project-list').click(function() {
//debugger;
if (enabled == false) {
$('#project-box').addClass("slideUp");
$('#project-box').removeClass("slideDown");
enabled = true;
$('#project-box').show();
} else {
$('#project-box').removeClass("slideUp");
$('#project-box').addClass("slideDown");
enabled = false;
$("#project-box").on('webkitAnimationEnd', function() {
if (enabled == false)
$('#project-box').hide();
});
}
//$('#project-box').toggleClass("slideUp");
});
// $(window).scroll(function () {
// $('#project-box').each(function () {
// var imagePos = $(this).offset().top;
//
// var topOfWindow = $(window).scrollTop();
// if (imagePos < topOfWindow + 400) {
// $(this).addClass("slideUp");
// }
// });
// });
//$('#carousel').carousel('cycle');
}
$(document).ready(main);
/* Exit Animation
-------------------------------------------------- */
.slideDownEdit {
animation-name: slideDown;
-webkit-animation-name: slideDown;
animation-duration: 1s;
-webkit-animation-duration: 1s;
animation-timing-function: ease;
-webkit-animation-timing-function: ease;
visibility: hidden !important;
}
@keyframes slideDown {
0% {
transform: translateY(-100%);
}
50% {
transform: translateY(8%);
}
65% {
transform: translateY(-4%);
}
80% {
transform: translateY(4%);
}
95% {
transform: translateY(-2%);
}
100% {
transform: translateY(0%);
}
}
@-webkit-keyframes slideDown {
0% {
-webkit-transform: translateY(0%);
}
100% {
-webkit-transform: translateY(500%);
visibility: hidden;
}
}
/* Footer
-------------------------------------------------- */
.footer {
background-color: #dddee1;
bottom: 0;
width: 100%;
/* Set the fixed height of the footer here */
height: 60px;
border: 1px solid grey;
}
.footer p {
font-size: 18px;
margin-top: 10px;
color: #777;
position: absolute;
}
.footer a {
color: #b55e5e;
font-size: 16px;
margin-top: 10px;
}
.footer .pull-right {
color: #b55e5e;
font-size: 16px;
margin-top: 10px;
right: 10%;
}
/* Featurettes
------------------------- */
.featurette-divider {
margin: 80px 0;
/* Space out the Bootstrap <hr> more */
}
/* Thin out the marketing headings */
.featurette-heading {
font-weight: 300;
line-height: 1;
letter-spacing: -1px;
margin-top: 120px;
font-size: 50px;
display: inline-block;
}
/* Project Box
------------------------- */
#project-box {
text-align: center;
cursor: pointer;
border: 3px solid #636366;
cursor: pointer;
width: 75%;
margin: auto;
visibility: hidden;
}
#project-divider.featurette-divider {
margin: 10px 0;
}
#project-box a {
font-size: 30px;
margin-left: 300px;
margin-right: 100px;
color: darkred;
display: block;
margin: auto;
}
#project-box p {
font-size: 20px;
color: dimgrey;
}
.project-list {
cursor: pointer;
color: darkred;
}
.slider {
overflow-y: scroll;
max-height: 500px;
/* approximate max height */
transition-property: all;
transition-duration: .5s;
transition-timing-function: cubic-bezier(0, 1, 0.5, 1);
}
.slider.closed {
max-height: 0;
}
.slideUp {
animation-name: slideUp;
-webkit-animation-name: slideUp;
animation-duration: 1s;
-webkit-animation-duration: 1s;
animation-timing-function: ease;
-webkit-animation-timing-function: ease;
visibility: visible !important;
}
@keyframes slideUp {
0% {
transform: translateY(100%);
}
50% {
transform: translateY(-8%);
}
65% {
transform: translateY(4%);
}
80% {
transform: translateY(-4%);
}
95% {
transform: translateY(2%);
}
100% {
transform: translateY(0%);
}
}
@-webkit-keyframes slideUp {
0% {
-webkit-transform: translateY(100%);
}
50% {
-webkit-transform: translateY(-8%);
}
65% {
-webkit-transform: translateY(4%);
}
80% {
-webkit-transform: translateY(-4%);
}
95% {
-webkit-transform: translateY(2%);
}
100% {
-webkit-transform: translateY(0%);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Murray Studio</title>
<meta name="description" content="Main Page for Murray Studio">
<link rel="icon" href="http://s3.amazonaws.com/codecademy-content/courses/ltp2/img/uber/close.png">
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/animations.css">
<link rel="stylesheet" href="main.css">
</head>
<body>
<!-- Wrap the rest of the page in another container to center all the content. -->
<div class="container marketing">
<!-- START THE FEATURETTES -->
<hr class="featurette-divider">
<div class="row featurette">
<div class="first-featurette">
<div class="col-md-7">
<a class="featurette-heading" href="#">Projects.</a>
<p class="lead">Donec ullamcorper nulla non metus auctor fringilla. Vestibulum id ligula porta felis euismod semper. Praesent commodo cursus magna, vel scelerisque nisl consectetur. Fusce dapibus, tellus ac cursus commodo.</p>
<p class="project-list">See Projects.</p>
<div id="object" class="slideUp">
<div id="object" class="slideDownEdit">
<div id="project-box">
<a>Embraer adds third Legacy 500 prototype to flight test campaign</a>
<hr class="featurette-divider" id="project-divider">
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
</div>
</div>
</div>
</div>
<div class="col-md-5">
<img class="featurette-image img-responsive center-block" src="http://a1.dspnimg.com/data/l/423341110329_Qy737Vid_l.jpg" alt="Generic placeholder image">
</div>
</div>
</div>
<hr class="featurette-divider">
</div>
<!-- /END THE FEATURETTES -->
<!-- FOOTER -->
<footer class="footer">
<div class="container">
<p class="pull-right"><a href="#">Back to top</a>
</p>
<p>© 2014 Company, Inc. · <a href="#">Privacy</a> · <a href="#">Terms</a>
</p>
</div>
</footer>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="main.js"></script>
</body>
</html>
或者,在else
循环执行后你可以detach the event handler但这意味着我们在每个循环中都进行了一个附加/分离循环,我认为这不是好事。 / p>
请注意,此答案仅发布以解释您的问题来源。我假设您将有更多(或不同的东西)需要使用额外的.show()
和.hide()
。
如果您的要求只是在切换开关时显示/隐藏元素,那么humble.rumble.6x3的答案中提供的方法是最简单的。
答案 1 :(得分:1)
您可以切换类并轻松获得相同的效果,因为元素已经从画布上移开,无需应用隐藏或显示。
$('.project-list').click(function () {
$('#project-box').toggleClass("slideUp");
$('#project-box').toggleClass("slideDown");
});
var main = function () {
var enabled = false;
//$('#project-box').hide();
$('.project-list').click(function () {
$('#project-box').toggleClass("slideUp");
$('#project-box').toggleClass("slideDown");
});
// $(window).scroll(function () {
// $('#project-box').each(function () {
// var imagePos = $(this).offset().top;
//
// var topOfWindow = $(window).scrollTop();
// if (imagePos < topOfWindow + 400) {
// $(this).addClass("slideUp");
// }
// });
// });
//$('#carousel').carousel('cycle');
}
$(document).ready(main);
&#13;
/* Exit Animation
-------------------------------------------------- */
.slideDownEdit{
animation-name: slideDown;
-webkit-animation-name: slideDown;
animation-duration: 1s;
-webkit-animation-duration: 1s;
animation-timing-function: ease;
-webkit-animation-timing-function: ease;
visibility: hidden !important;
}
@keyframes slideDown {
0% {
transform: translateY(-100%);
}
50%{
transform: translateY(8%);
}
65%{
transform: translateY(-4%);
}
80%{
transform: translateY(4%);
}
95%{
transform: translateY(-2%);
}
100% {
transform: translateY(0%);
}
}
@-webkit-keyframes slideDown {
0% {
-webkit-transform: translateY(0%);
}
100% {
-webkit-transform: translateY(500%);
visibility: hidden;
}
}
/* Footer
-------------------------------------------------- */
.footer {
background-color: #dddee1;
bottom: 0;
width: 100%;
/* Set the fixed height of the footer here */
height: 60px;
border: 1px solid grey;
}
.footer p {
font-size: 18px;
margin-top: 10px;
color: #777;
position: absolute;
}
.footer a {
color: #b55e5e;
font-size: 16px;
margin-top: 10px;
}
.footer .pull-right {
color: #b55e5e;
font-size: 16px;
margin-top: 10px;
right: 10%;
}
/* Featurettes
------------------------- */
.featurette-divider {
margin: 80px 0;
/* Space out the Bootstrap <hr> more */
}
/* Thin out the marketing headings */
.featurette-heading {
font-weight: 300;
line-height: 1;
letter-spacing: -1px;
margin-top: 120px;
font-size: 50px;
display: inline-block;
}
/* Project Box
------------------------- */
#project-box {
text-align: center;
cursor: pointer;
border: 3px solid #636366;
cursor: pointer;
width: 75%;
margin: auto;
visibility: hidden;
}
#project-divider.featurette-divider{
margin: 10px 0;
}
#project-box a {
font-size: 30px;
margin-left: 300px;
margin-right: 100px;
color: darkred;
display: block;
margin: auto;
}
#project-box p {
font-size: 20px;
color: dimgrey;
}
.project-list {
cursor: pointer;
color: darkred;
}
.slider {
overflow-y: scroll;
max-height: 500px; /* approximate max height */
transition-property: all;
transition-duration: .5s;
transition-timing-function: cubic-bezier(0, 1, 0.5, 1);
}
.slider.closed {
max-height: 0;
}
.slideUp{
animation-name: slideUp;
-webkit-animation-name: slideUp;
animation-duration: 1s;
-webkit-animation-duration: 1s;
animation-timing-function: ease;
-webkit-animation-timing-function: ease;
visibility: visible !important;
}
@keyframes slideUp {
0% {
transform: translateY(100%);
}
50%{
transform: translateY(-8%);
}
65%{
transform: translateY(4%);
}
80%{
transform: translateY(-4%);
}
95%{
transform: translateY(2%);
}
100% {
transform: translateY(0%);
}
}
@-webkit-keyframes slideUp {
0% {
-webkit-transform: translateY(100%);
}
50%{
-webkit-transform: translateY(-8%);
}
65%{
-webkit-transform: translateY(4%);
}
80%{
-webkit-transform: translateY(-4%);
}
95%{
-webkit-transform: translateY(2%);
}
100% {
-webkit-transform: translateY(0%);
}
}
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Murray Studio</title>
<meta name="description" content="Main Page for Murray Studio">
<link rel="icon" href="http://s3.amazonaws.com/codecademy-content/courses/ltp2/img/uber/close.png">
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/animations.css">
<link rel="stylesheet" href="main.css">
</head>
<body>
<!-- Wrap the rest of the page in another container to center all the content. -->
<div class="container marketing">
<!-- START THE FEATURETTES -->
<hr class="featurette-divider">
<div class="row featurette">
<div class="first-featurette">
<div class="col-md-7">
<a class="featurette-heading" href="#">Projects.</a>
<p class="lead">Donec ullamcorper nulla non metus auctor fringilla. Vestibulum id ligula porta felis euismod semper. Praesent commodo cursus magna, vel scelerisque nisl consectetur. Fusce dapibus, tellus ac cursus commodo.</p>
<p class="project-list">See Projects.</p>
<div id="object" class="slideUp">
<div id="object" class="slideDownEdit">
<div id="project-box">
<a>Embraer adds third Legacy 500 prototype to flight test campaign</a>
<hr class="featurette-divider" id="project-divider">
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
<p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
</div>
</div>
</div>
</div>
<div class="col-md-5">
<img class="featurette-image img-responsive center-block" src="http://a1.dspnimg.com/data/l/423341110329_Qy737Vid_l.jpg" alt="Generic placeholder image">
</div>
</div>
</div>
<hr class="featurette-divider">
</div>
<!-- /END THE FEATURETTES -->
<!-- FOOTER -->
<footer class="footer">
<div class="container">
<p class="pull-right"><a href="#">Back to top</a>
</p>
<p>© 2014 Company, Inc. · <a href="#">Privacy</a> · <a href="#">Terms</a>
</p>
</div>
</footer>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="main.js"></script>
</body>
</html>
&#13;