admin管理员组文章数量:1278854
I am learning this 3D effect of flipping content of a div. On hover the div code below works perfectly. But What I want is that, if someone only click on the button flip this flipping of div should work. I want this flipping effect only if button is clicked not if hover or anything.
<style>
#f1_container {
position: relative;
margin: 10px auto;
width: 450px;
height: 281px;
z-index: 1;
}
#f1_container {
perspective: 1000;
}
#f1_card {
width: 100%;
height: 100%;
transform-style: preserve-3d;
transition: all 1.0s linear;
}
#f1_container:hover #f1_card {
transform: rotateX(180deg);
box-shadow: -5px 5px 5px #aaa;
}
.face {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}
.face.back {
display: block;
transform: rotateX(180deg);
box-sizing: border-box;
padding: 10px;
color: white;
text-align: center;
background-color: #aaa;
}
</style>
<div id="f1_container">
<div id="f1_card" class="shadow">
<div class="front face">
lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />
lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />
lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />
</div>
<div class="back face center">
<p>This is nice for exposing more information about an image.</p>
<p>Any content can go here.</p>
</div>
</div>
</div>
<button class="btn btn-primary" id="flip_content">Flip</button>
I am learning this 3D effect of flipping content of a div. On hover the div code below works perfectly. But What I want is that, if someone only click on the button flip this flipping of div should work. I want this flipping effect only if button is clicked not if hover or anything.
<style>
#f1_container {
position: relative;
margin: 10px auto;
width: 450px;
height: 281px;
z-index: 1;
}
#f1_container {
perspective: 1000;
}
#f1_card {
width: 100%;
height: 100%;
transform-style: preserve-3d;
transition: all 1.0s linear;
}
#f1_container:hover #f1_card {
transform: rotateX(180deg);
box-shadow: -5px 5px 5px #aaa;
}
.face {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}
.face.back {
display: block;
transform: rotateX(180deg);
box-sizing: border-box;
padding: 10px;
color: white;
text-align: center;
background-color: #aaa;
}
</style>
<div id="f1_container">
<div id="f1_card" class="shadow">
<div class="front face">
lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />
lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />
lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />
</div>
<div class="back face center">
<p>This is nice for exposing more information about an image.</p>
<p>Any content can go here.</p>
</div>
</div>
</div>
<button class="btn btn-primary" id="flip_content">Flip</button>
Share
Improve this question
asked Dec 28, 2014 at 0:25
asdlfkjlkjasdlfkjlkj
2,3586 gold badges22 silver badges29 bronze badges
4 Answers
Reset to default 8You could use JavaScript to handle click events on button
.
1st Approach: Using .classList.toggle()
.
content.classList.toggle('flip')
will add the class .flip
, if it doesn't exist and will remove if it exists.
var btn = document.getElementById('flip_content');
var content = document.getElementById('f1_card');
btn.onclick = function() {
content.classList.toggle('flip');
}
#f1_container {
position: relative;
margin: 10px auto;
width: 450px;
height: 281px;
z-index: 1;
}
#f1_container {
perspective: 1000;
}
#f1_card {
width: 100%;
height: 100%;
transform-style: preserve-3d;
transition: all 1.0s linear;
}
.flip {
transform: rotateX(180deg);
box-shadow: -5px 5px 5px #aaa;
}
.face {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}
.face.back {
display: block;
transform: rotateX(180deg);
box-sizing: border-box;
padding: 10px;
color: white;
text-align: center;
background-color: #aaa;
}
<div id="f1_container">
<div id="f1_card" class="shadow">
<div class="front face">
lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />
</div>
<div class="back face center">
<p>This is nice for exposing more information about an image.</p>
<p>Any content can go here.</p>
</div>
</div>
</div>
<button class="btn btn-primary" id="flip_content">Flip</button>
2nd Approach: Using .classList.add()
and .classList.remove()
.
You could keep track of clicks, if the count is divisible by 2
, add the class .flip
, else remove it.
var btn = document.getElementById('flip_content');
var content = document.getElementById('f1_card');
var c = 0;
btn.onclick = function() {
(c++ % 2 == 0) ? content.classList.add('flip'): content.classList.remove('flip');
}
#f1_container {
position: relative;
margin: 10px auto;
width: 450px;
height: 281px;
z-index: 1;
}
#f1_container {
perspective: 1000;
}
#f1_card {
width: 100%;
height: 100%;
transform-style: preserve-3d;
transition: all 1.0s linear;
}
.flip {
transform: rotateX(180deg);
box-shadow: -5px 5px 5px #aaa;
}
.face {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}
.face.back {
display: block;
transform: rotateX(180deg);
box-sizing: border-box;
padding: 10px;
color: white;
text-align: center;
background-color: #aaa;
}
<div id="f1_container">
<div id="f1_card" class="shadow">
<div class="front face">lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />
</div>
<div class="back face center">
<p>This is nice for exposing more information about an image.</p>
<p>Any content can go here.</p>
</div>
</div>
</div>
<button class="btn btn-primary" id="flip_content">Flip</button>
3rd Approach: Using .className
.
var btn = document.getElementById('flip_content');
var content = document.getElementById('f1_card');
var c = 0;
btn.onclick = function() {
content.className = (c++ % 2 == 0) ? content.className + ' flip' : content.className.split(' ')[0];
}
#f1_container {
position: relative;
margin: 10px auto;
width: 450px;
height: 281px;
z-index: 1;
}
#f1_container {
perspective: 1000;
}
#f1_card {
width: 100%;
height: 100%;
transform-style: preserve-3d;
transition: all 1.0s linear;
}
.flip {
transform: rotateX(180deg);
box-shadow: -5px 5px 5px #aaa;
}
.face {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}
.face.back {
display: block;
transform: rotateX(180deg);
box-sizing: border-box;
padding: 10px;
color: white;
text-align: center;
background-color: #aaa;
}
<div id="f1_container">
<div id="f1_card" class="shadow">
<div class="front face">lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />
</div>
<div class="back face center">
<p>This is nice for exposing more information about an image.</p>
<p>Any content can go here.</p>
</div>
</div>
</div>
<button class="btn btn-primary" id="flip_content">Flip</button>
.classList
returns an array of all class
es assigned to the element.
For instance,
<div id="div" class="one two three four five"></div>
document.getElementById('div').classList
//['one', 'two', 'three', 'four', 'five']
.className
returns a string of all class
es(ma seperated) assigned to the element.
For instance,
<div id="div" class="one two three four five"></div>
document.getElementById('div').className
//'one two three four five'
You need to add js, here shown with jquery to trigger a class change instead of hover
$(function(){
$('#flip_content').click(function(e){
$('#f1_container').addClass('flip');
});
});
And change the hover selector to the class .flip
#f1_container.flip #f1_card {
transform: rotateX(180deg);
box-shadow: -5px 5px 5px #aaa;
}
You can use some JavaScript for this like so:
Working example here: http://jsfiddle/cjLm77ek/
JavaScript (jQuery):
$(document).ready(function() {
$("#flip_content").click(function() {
$("#f1_card").css("transform", "rotateX(180deg)");
$("#f1_card").css("box-shadow", "-5px 5px 5px #aaa");
});
});
HTML:
<div id="f1_container">
<div id="f1_card" class="shadow">
<div class="front face">lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />lets see if anything happen
<br />
</div>
<div class="back face center">
<p>This is nice for exposing more information about an image.</p>
<p>Any content can go here.</p>
</div>
</div>
</div>
<button class="btn btn-primary" id="flip_content">Flip</button>
CSS:
#f1_container {
position: relative;
margin: 10px auto;
width: 450px;
height: 281px;
z-index: 1;
}
#f1_container {
perspective: 1000;
}
#f1_card {
width: 100%;
height: 100%;
transform-style: preserve-3d;
transition: all 1.0s linear;
}
/* disable hover change
#f1_container:hover #f1_card {
transform: rotateX(180deg);
box-shadow: -5px 5px 5px #aaa;
}
*/
.face {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}
.face.back {
display: block;
transform: rotateX(180deg);
box-sizing: border-box;
padding: 10px;
color: white;
text-align: center;
background-color: #aaa;
}
You can achieve this using only CSS by using the checkbox:
Also you can give more custom styling to the label to make it look like a button and place it anywhere in the HTML but note the checkbox that i added in in the f1_container
. It should be right before the element you want to flip.
This solution does has limitation that it works fine only on modern browsers.
JSFiddle Output: http://jsfiddle/7h20gryw/
#f1_container {
position: relative;
margin: 10px auto;
width: 450px;
height: 281px;
z-index: 1;
}
#f1_container {
perspective: 1000;
}
#f1_card {
width: 100%;
height: 100%;
transform-style: preserve-3d;
transition: all 1.0s linear;
}
input[type=checkbox]:checked ~ #f1_card {
transform: rotateX(180deg);
box-shadow: -5px 5px 5px #aaa;
}
.face {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}
.face.back {
display: block;
transform: rotateX(180deg);
box-sizing: border-box;
padding: 10px;
color: white;
text-align: center;
background-color: #aaa;
}
input[type=checkbox] {
position: absolute;
top: -9999px;
left: -9999px;
/* For mobile, it's typically better to position checkbox on top of clickable
area and turn opacity to 0 instead. */
}
input[type=checkbox] + label {
color: #ccc;
font-style: italic;
}
input[type=checkbox]:checked + label {
color: #f00;
font-style: normal;
}
<div id="f1_container">
<input type="checkbox" id="ossm" name="ossm">
<div id="f1_card" class="shadow flipme">
<div class="front face">
lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />
lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />
lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />lets see if anything happen <br />
</div>
<div class="back face center">
<p>This is nice for exposing more information about an image.</p>
<p>Any content can go here.</p>
</div>
</div>
</div>
<label for="ossm">Flip</label>
本文标签: javascriptFlip content of a div on clicking a buttonStack Overflow
版权声明:本文标题:javascript - Flip content of a div on clicking a button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741213582a2359610.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论