当鼠标悬停在按钮上时,在CSS上翻转动画

时间:2018-11-27 19:42:09

标签: html css css-animations flip

我正在尝试获得一个CSS翻转动画,该动画基于鼠标指针在按钮上的悬停效果。 现在您可以在this codepen看到类似的内容 在此Codepen示例中,将鼠标指针悬停在卡片上时,您将获得一个翻转动画。

enter image description here

我有以下html代码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Card Flipped</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta content="A fully featured admin theme which can be used to build CRM, CMS, etc." name="description" />
        <meta content="Coderthemes" name="author" />
        <!-- App favicon -->
        <link rel="shortcut icon" href="assets/images/favicon.ico">

        <!-- App css -->
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    </head>

    <body>

        <!-- Begin page -->
        <div class="wrapper">
                        <div class="row">
                            <div class="col-md-6 col-lg-3">

                                <!-- Simple card -->
                                  <div class="flip-container" >
                             <div class="flipper">
                             <div class="front">
                                <!-- front content -->
                                <div class="card d-block">
                                      <img class="card-img-top" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ8MjPXuGsKGugRrvrD9YQtR_nZD6Ka4Q_7ZpyzJV2TEPtfadpP" alt="Card image cap" height="180" weight="80">
                                    <div class="card-body">
                                        <h5 class="card-title">Introduction to particle physics</h5>
                                        <p class="card-text">Some quick example text to build on the card title and make
                                            up the bulk of the card's content. Some quick example text to build on the card title and make up.</p>
                                        <a href="javascript: void(0);" class="btn btn-primary">Buy</a>
                                        <a href="javascript: void(0);" class="btn btn-primary view-details">View Details</a>
                                    </div> <!-- end card-body-->
                                </div> <!-- end card-->
                             </div>

                                    <div class="back">
                                        <h1>Do you like it?</h2><!-- back content -->
                                    </div>
                                </div>
                            </div>

        </div>
        <!-- END wrapper -->

        <script src="assets/js/app.min.js"></script>
    </body>
</html>

此CSS代码:

/* FLIP DOUBLE SIDE START ANIMATION CODE */
/* entire container, keeps perspective */
/* flip the pane when hovered */

.flip-container:hover .flipper{
        transform: rotateY(180deg);
    }

/*
.view-details:hover .flipper {
    transform: rotateY(180deg);
  }
*/

.flip-container, .front, .back { /*Attention*/
    width: 280px;
    height: 480px;
}


/* flip speed goes here */
.flipper {
    transition: 0.6s;
    transform-style: preserve-3d;

    position: relative;
}

/* hide back of pane during swap */
.front, .back {
    backface-visibility: hidden;

    position: absolute;
    top: 0;
    left: 0;
}

/* front pane, placed above back */
.front {
    z-index: 2;
    /* for firefox 31 */
    transform: rotateY(0deg);
}

/* back, initially hidden pane */
.back {
    transform: rotateY(180deg);
}
/* FLIP DOUBLE SIDE END CODE ANIMATION*/

但是我想得到的是,当您将鼠标指针放在viw详细信息按钮上时,您会得到整个卡片的翻转动画。 视图详细信息按钮包含一个称为“视图详细信息”的类。所以我尝试更改css行:

.flip-container:hover .flipper{
        transform: rotateY(180deg);
    }

对于另一个:

.view-details:hover .flipper{   transform: rotateY(180deg);
        }

但是显然没有任何反应。 顺便说一下,我从David Walsh web page

获得了CSS代码和html代码的一部分

1 个答案:

答案 0 :(得分:2)

什么都没有发生,因为您试图为仅使用CSS的孩子设置父选择器动画。不幸的是,当前无法仅使用CSS选择元素的父级。您将需要使用这样的Javascript / jQuery解决方案来达到您想要的效果。

$('a.view-details').mouseover(function() {
  $(this).parents('.flipper').css('transform', 'rotateY(180deg)');
}).mouseout(function() {
	$(this).parents('.flipper').css('transform', '');
});
/* FLIP DOUBLE SIDE START ANIMATION CODE */
/* entire container, keeps perspective */

.flip-container,
.front,
.back {
	/*Attention*/
	width: 280px;
	height: 480px;
}

/* flip speed goes here */
.flipper {
	transition: 0.6s;
	transform-style: preserve-3d;
	position: relative;
}

/* hide back of pane during swap */
.front,
.back {
	backface-visibility: hidden;
	position: absolute;
	top: 0;
	left: 0;
}

/* front pane, placed above back */
.front {
	z-index: 2;
	/* for firefox 31 */
	transform: rotateY(0deg);
}

/* back, initially hidden pane */
.back {
	transform: rotateY(180deg);
}
/* FLIP DOUBLE SIDE END CODE ANIMATION*/
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="utf-8" />
	<title>Card Flipped</title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta content="A fully featured admin theme which can be used to build CRM, CMS, etc." name="description" />
	<meta content="Coderthemes" name="author" />
	<!-- App favicon -->
	<link rel="shortcut icon" href="assets/images/favicon.ico">

	<!-- App css -->
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>

	<!-- Begin page -->
	<div class="wrapper">
		<div class="row">
			<div class="col-md-6 col-lg-3">

				<!-- Simple card -->
				<div class="flip-container">
					<div class="flipper">
						<div class="front">
							<!-- front content -->
							<div class="card d-block">
								<img class="card-img-top" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ8MjPXuGsKGugRrvrD9YQtR_nZD6Ka4Q_7ZpyzJV2TEPtfadpP" alt="Card image cap" height="180" weight="80">
								<div class="card-body">
									<h5 class="card-title">Introduction to particle physics</h5>
									<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content. Some quick example text to build on the card title and make up.</p>
									<a href="javascript: void(0);" class="btn btn-primary">Buy</a>
									<a href="javascript: void(0);" class="btn btn-primary view-details">View Details</a>
								</div>
								<!-- end card-body-->
							</div>
							<!-- end card-->
						</div>

						<div class="back">
							<h1>Do you like it?</h2>
								<!-- back content -->
						</div>
					</div>
				</div>

			</div>
			<!-- END wrapper -->

			<script src="assets/js/app.min.js"></script>
</body>

</html>