纯CSS <a> tag card flip

时间:2018-06-07 19:25:29

标签: javascript html css

I have a card flip animation built with pure css. I was wondering if it's possible to have the card flip animation work only when someone clicks the "Link" <a> tag instead of hovering? I know this is possible with java script but I couldn't find anything to get this working with pure css. Could anyone point me in the right direction? Anything helps, thanks.

@import url('https://fonts.googleapis.com/css?family=Playfair+Display:400,400i,700,700i,900,900i');

@import url('https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i,800,800i');

html, body { 
  height: 100%;
  width: 100%; 
  margin: 0; 
  padding: 0; 
  background-color:#eaeaea;}

.container {
  width:900px;
  height:550px;
  background-color:white;
  position: absolute;
  top:0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;}

.right-div {
  width:540px;
  height:550px;
  background-image:url();
  transform:scale(1);
  background-position: -170px 0px; 
  background-size:cover;
  position: absolute;
  top:0;
  bottom: 0;
  right: 0;
  margin: auto;}

.left-div {
  width:323px;
  height:550px;
  background-color:white;
  position: absolute;
  top:0;
  bottom: 0;
  left: 0;
  margin: auto;}

.content-name {
    font-family: 'Open Sans', sans-serif;
    letter-spacing: 3px;
    font-size: 10px;
    text-transform: uppercase;
    color: #7E7E7E;    
    font-weight: 700;
    margin-top:160px;
    margin-left:40px;}

.content-title {
    font-family: 'Playfair Display', serif;
    font-size: 44px;
    letter-spacing: 3px;
    font-weight: 700;
    color: #2C2C2C;
    margin-top:10px;
    margin-left:40px;}

.content-description {
    margin-top: -20px;
    font-family: 'Open Sans', sans-serif;
    font-size: 13px;
    color: #7e7e7e;
    line-height: 22px;
    margin-left:40px;}

.content-link {
    position:absolute;
    margin-top:20px;
    color: #2C2C2C;
    font-family: 'Open Sans', sans-serif;
    letter-spacing: 3px;
    font-size: 11px;
    text-transform: uppercase;
    font-weight: 700;
    text-decoration: none;
    margin-left:40px;}





/*Flip*/

.flip-container {
  -webkit-perspective: 1000;
  position: fixed;
  top: 50%;
  left: 50%;
  /* bring your own prefixes */
  transform: translate(-50%, -50%);}

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

.flip-container, .front, .back {
	width: 900px;
	height: 550px;}

.flipper {
	-webkit-transition: 0.6s;
	-webkit-transform-style: preserve-3d;
	position: relative;}

.front, .back {
	-webkit-backface-visibility: hidden;
  position: absolute;
  top:0;
  bottom: 0;}

.front {
  z-index: 2;}

.back {
  -webkit-transform: rotateY(180deg);
  background: white;}
<div class="flip-container">
	<div class="flipper">
		<div class="front">
<div class="container">
  <div class="left-div">
      <p class="content-name">title</p>
      <p class="content-title">name</p>
    <p class="content-description">Sed ut perspiciatis unde omnis iste natus <br/> error sit voluptatem accusantium doloremque laudantium, totam rem aperiam.</p>
    <a href="#" class="content-link">Link</a>
  </div>
  <div class="right-div">
  </div>
  </div>
    </div>
    <div class="back">
      :p
		</div>
	</div>
</div>

2 个答案:

答案 0 :(得分:1)

看起来你可以使用:focus-within伪类:

来做你想要的事情
.flip-container .flipper:focus-within  {
  -webkit-transform: rotateY(180deg);
}

&#13;
&#13;
@import url('https://fonts.googleapis.com/css?family=Playfair+Display:400,400i,700,700i,900,900i');

@import url('https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i,800,800i');

html, body { 
  height: 100%;
  width: 100%; 
  margin: 0; 
  padding: 0; 
  background-color:#eaeaea;
}

.container {
  width:900px;
  height:550px;
  background-color:white;
  position: absolute;
  top:0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
 }

.right-div {
  width:540px;
  height:550px;
  background-image:url();
  transform:scale(1);
  background-position: -170px 0px; 
  background-size:cover;
  position: absolute;
  top:0;
  bottom: 0;
  right: 0;
  margin: auto;
}

.left-div {
  width:323px;
  height:550px;
  background-color:white;
  position: absolute;
  top:0;
  bottom: 0;
  left: 0;
  margin: auto;
}

.content-name {
    font-family: 'Open Sans', sans-serif;
    letter-spacing: 3px;
    font-size: 10px;
    text-transform: uppercase;
    color: #7E7E7E;    
    font-weight: 700;
    margin-top:160px;
    margin-left:40px;
}

.content-title {
    font-family: 'Playfair Display', serif;
    font-size: 44px;
    letter-spacing: 3px;
    font-weight: 700;
    color: #2C2C2C;
    margin-top:10px;
    margin-left:40px;
}

.content-description {
    margin-top: -20px;
    font-family: 'Open Sans', sans-serif;
    font-size: 13px;
    color: #7e7e7e;
    line-height: 22px;
    margin-left:40px;
}

.content-link {
    position:absolute;
    margin-top:20px;
    color: #2C2C2C;
    font-family: 'Open Sans', sans-serif;
    letter-spacing: 3px;
    font-size: 11px;
    text-transform: uppercase;
    font-weight: 700;
    text-decoration: none;
    margin-left:40px;
}


/*Flip*/

.flip-container {
  -webkit-perspective: 1000;
  position: fixed;
  top: 50%;
  left: 50%;
  /* bring your own prefixes */
  transform: translate(-50%, -50%);
}

.flip-container .flipper:focus-within  {
  -webkit-transform: rotateY(180deg);
}

.flip-container, .front, .back {
	width: 900px;
	height: 550px;
}

.flipper {
  -webkit-transition: 0.6s;
  -webkit-transform-style: preserve-3d;
  position: relative;
}

.front, .back {
  -webkit-backface-visibility: hidden;
  position: absolute;
  top:0;
  bottom: 0;
}

.front {
  z-index: 2;
}

.back {
  -webkit-transform: rotateY(180deg);
  background: white;
}
&#13;
<div class="flip-container">
  <div class="flipper">
	<div class="front">
      <div class="container">
        <div class="left-div">
          <p class="content-name">title</p>
          <p class="content-title">name</p>
          <p class="content-description">Sed ut perspiciatis unde omnis iste natus <br/> error sit voluptatem accusantium doloremque laudantium, totam rem aperiam.</p>
          <a href="#" class="content-link">Link</a>
        </div>
        <div class="right-div">
        </div>
      </div>
    </div>
    <div class="back">
      :p
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这基本上是滥用CSS,因为这是js的工作。

无论如何,您可以使用:target实现此目的,但是在文档中的其他内容被定位之前,样式不会消失。

div {
  background-color: orange;
  height: 100px;
  width: 100px;
  transition: transform 1.5s ease;
  backface-visibility: hidden;
}

div:target {
  transform: rotateY(180deg);
}
<a href="#test">Click to Flip</a>
<div id="test"></div>