我有一些 div 元素,当我单击它们时,它们会显示它们的背面(翻转卡)并扩展为 600px x 600px 的大小,我希望这些 div 在我单击它时也进入屏幕中心,然后移动当再次单击它时再次返回。我希望它水平和垂直居中。我已经尝试过搜索,但到目前为止他们都没有工作。大多数结果包括以下内容:
.flip.cover.flipped{
left: 50%;
margin-top: -300px;
margin-left: -300px;
position: absolute;
height: 600px;
width: 600px;
top: 50%;
}
我已将其添加到当前的 css 类.flip.cover.flipped
,这是一个使用 javascript 在单击时切换的类,但这只会导致我的 div 元素保持在单击时的位置。看到这个。这是.flip.cover.flipped
完整的课程:
.flip .cover.flipped {
left: 50%;
margin-top: -300px;
margin-left: -300px;
position: absolute;
height: 600px;
width: 600px;
top: 50%;
transform: rotatey(-180deg);
-moz-transform: rotatey(-180deg);
-ms-transform: rotatey(180deg);
-o-transform: rotatey(-180deg);
-webkit-transform: rotatey(-180deg);
z-index: 10;
}
其他结果包括一个我无法测试的自定义 javascript 函数。如果自定义 javascript 函数有效,我只需要一些东西来切换它(如果是这种情况,我真的希望有一些简单的东西,例如$(this).find('.cover').toggleClass('flipped');
,仅用于函数)
我非常感谢任何人对我如何能够做到这一点的帮助。
我最近发现通常的方式是在其父元素中居中元素。问题是我想在窗口内而不是在其父元素内居中元素。
这是HTML结构:
<div class="flip">
<div class="cover">
<div class="face front">
<img src="http://farm4.staticflickr.com/3809/8814925510_b53d082ea6_o.jpg" width="125"/>
</div>
<div class="face back">
Hello World!
</div>
</div>
</div>
这是切换类的javascript:
$('.flip').click(function() {
$(this).find('.cover').toggleClass('flipped');
});
编辑:我正在审查我自己的一些问题,并且由于指向我的示例的链接已更改,我决定在此处显示页面的完整代码以及结果:
$('.flip').click(function() {
$(this).find('.cover').toggleClass('flipped');
});
/*Sorted alphabetically*/
* {
margin: 0px;
padding: 0px;
}
#backbutton {
background-image: url("http://farm9.staticflickr.com/8538/8781018857_feef1c4e6e_o.png");
background-position: center;
background-repeat: no-repeat;
background-size: 90px 32px;
color: #FFFFFA;
display: inline-block;
font-family: Helvetica, Sans-Serif;
font-size: 26px;
float: left;
min-width: 90px;
padding: 4px 0px;
text-align: center;
text-shadow: 0px -0.9px 1px #1e160a, -0.9px 0px 1px #1e160a, 0.9px 0px 1px #f5eee2, 0px 0.9px 1px #f5eee2;
z-index: 2;
}
body {
background-image: url('http://farm3.staticflickr.com/2891/8769949843_f6b2b03c89_o.png');
background-repeat: repeat-y;
background-position: 0px 37px;
background-size: cover;
}
#creditsbutton {
background-image: url("http://farm4.staticflickr.com/3726/8769949955_3d28f7be67_o.png");
background-position: center;
background-repeat: no-repeat;
background-size: 100px 32px;
color: #FFFFFA;
display: inline-block;
font-family: Helvetica, Sans-Serif;
font-size: 26px;
float: right;
min-width: 100px;
padding: 4px 0px;
text-align: center;
text-shadow: 0px -0.9px 1px #1e160a, -0.9px 0px 1px #1e160a, 0.9px 0px 1px #f5eee2, 0px 0.9px 1px #f5eee2;
z-index: 2;
}
.flip {
box-shadow: 0px 15px 6px -6px #2C0600;
-o-box-shadow: 0px 15px 6px -6px #2C0600;
-ms-box-shadow: 0px 15px 6px -6px #2C0600;
-moz-box-shadow: 0px 15px 6px -6px #2C0600;
-webkit-box-shadow: 0px 15px 6px -6px #2C0600;
display: inline-block;
height: 185px;
position: relative;
width: 125px;
perspective: 800;
-o-perspective: 800;
-moz-perspective: 800;
-ms-perspective: 800;
-webkit-perspective: 800;
}
.flip .cover {
height: 100%;
padding: 0px 0px 0px 0px;
position: relative;
text-align: center;
width: 100%;
transform-style: preserve-3d;
-o-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
-ms-transform-style: preserve-3d;
-webkit-transform-style: preserve-3d;
transition: 0.5s;
-o-transition: 0.5s;
-moz-transition: 0.5s;
-ms-transition: 0.5s;
-webkit-transition: 0.5s;
}
.flip .cover .back {
background: white;
color: black;
cursor: pointer;
transform: rotatey(-180deg);
-moz-transform: rotatey(-180deg);
-ms-transform: rotatey(-180deg);
-o-transform: rotatey(-180deg);
-webkit-transform: rotatey(-180deg);
}
.flip .cover.flipped {
left: 50%;
margin-top: -300px;
margin-left: -300px;
position: absolute;
height: 600px;
width: 600px;
top: 50%;
transform: rotatey(-180deg);
-moz-transform: rotatey(-180deg);
-ms-transform: rotatey(180deg);
-o-transform: rotatey(-180deg);
-webkit-transform: rotatey(-180deg);
z-index: 10;
}
.flip .cover .face {
font-family: Helvetica, Sans Serif;
height: 100%;
line-height: 200px;
position: absolute;
text-align: center;
width: 100%;
backface-visibility: hidden;
-o-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-ms-backfae-visibility: hidden;
-webkit-backface-visibility: hidden;
}
.flip .cover .front {
background: black;
color: white;
cursor: pointer;
position: absolute;
}
#header {
background-image: url("http://farm9.staticflickr.com/8137/8774763204_453c37e1aa_o.png");
background-repeat: no-repeat;
background-size: cover;
min-width: 673px;
padding: 4px 14px;
z-index: 1;
}
.headerbuttonsegment-centre {
background-image: url("http://farm6.staticflickr.com/5330/8787597480_6696f22a79_o.png");
background-position: center;
background-repeat: no-repeat;
color: #FFFFFA;
display: inline-block;
font-family: Helvetica, Sans-Serif;
font-size: 26px;
padding: 4px 0px;
text-align: center;
text-shadow: 0px -0.9px 1px #1e160a, -0.9px 0px 1px #1e160a, 0.9px 0px 1px #f5eee2, 0px 0.9px 1px #f5eee2;
z-index: 2;
}
#headerbuttonsegment-left {
background-image: url("http://farm9.staticflickr.com/8560/8787597614_733a58b275_o.png");
background-position: center;
background-repeat: no-repeat;
color: #FFFFFA;
display: inline-block;
font-family: Helvetica, Sans-Serif;
font-size: 26px;
padding: 4px 0px;
text-align: center;
text-shadow: 0px -0.9px 1px #1e160a, -0.9px 0px 1px #1e160a, 0.9px 0px 1px #f5eee2, 0px 0.9px 1px #f5eee2;
z-index: 2;
}
#headerbuttonsegment-right {
background-image: url("http://farm6.staticflickr.com/5466/8787597568_43fd6869fb_o.png");
background-position: center;
background-repeat: no-repeat;
color: #FFFFFA;
display: inline-block;
font-family: Helvetica, Sans-Serif;
font-size: 26px;
padding: 4px 0px;
text-align: center;
text-shadow: 0px -0.9px 1px #1e160a, -0.9px 0px 1px #1e160a, 0.9px 0px 1px #f5eee2, 0px 0.9px 1px #f5eee2;
z-index: 2;
}
#shelf {
list-style-type: none;
padding: 10px 50px 20px;
position: relative;
}
.spacing {
display: inline-block;
width: 50px;
}
.wrap-centre {
min-width: 603px;
text-align: center;
width: 100%
}
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<body>
<div id='header'>
<a href="http://markscamilleri.tumblr.com">
<div id="backbutton">
Back
</div>
</a>
<div class='wrap-centre'>
<div id="headerbuttonsegment-left" style=" min-width: 150px; background-size: 150px 32px;">
Bookshelf
</div>
<div class="headerbuttonsegment-centre" style=" min-width: 175px; background-size: 175px 32px;">
Want to Read
</div>
<div id="headerbuttonsegment-right" style=" min-width: 150px; background-size: 150px 32px;">
Favourites
</div>
<a href="#" target="_blank">
<div id='creditsbutton'>
Credits
<!--Do not remove Credits!!-->
</div>
</a>
</div>
</div>
<div id="shelf">
<div class="flip">
<div class="cover">
<div class="face front">
<img src="http://farm3.staticflickr.com/2866/8804333739_9d644bcdf6_o.jpg" width="125" />
</div>
<div class="face back">
Hello World!
</div>
</div>
</div>
<div class="spacing">
</div>
<div class="flip">
<div class="cover">
<div class="face front">
<img src="http://farm4.staticflickr.com/3809/8814925510_b53d082ea6_o.jpg" width="125" />
</div>
<div class="face back">
Hello World!
</div>
</div>
</div>
</div>
</body>