在这个JS纸牌游戏中添加一个纸牌翻转动画
Adding a card flip animation to this JS card game?
我正在玩这个纸牌游戏,我想添加一个纸牌翻转动画。
我已经搜索了答案,但大多数解决方案都是指jquery(我不想只为一个动画实现)或CSS3(我喜欢这个例子)。可没那么走运。
js游戏使用以下内容(来自cardgamecore.js):
playingCard.redrawCardImage()
强制重抽
playingCard.showFace(bool: face)
设置卡片面朝上或面朝下,并在需要时重新绘制。True = face up, false = face down.
playingCard.prototype.redrawCardImage = function () {
// Set or change the image showing on the card face
if( !this.faceImage || !this.cardSet.backImage ) { return; }
// Bug in Firefox - alt attributes do not change unless they are made _before_ an SRC change
this.cardImage.setAttribute('alt',this.wayup?(this.cardSet.cardNames[this.number-1]+' '+this.suit):this.cardSet.cardWord);
this.cardImage.src = this.wayup ? this.faceImage : this.cardSet.backImage;
};
playingCard.prototype.showFace = function (oWhich) {
// Used to flip a card over
if( this.redrawNewImage != oWhich ) {
this.wayup = oWhich;
this.redrawCardImage();
}
};
所以当一张牌露出它的背面时,那么playingCard.showFace = false
。当你点击它,playingCard.showFace = true
和图像被重新绘制。此时,卡片应该有一个漂亮的翻转动画。
但是怎么做呢?
你要搜索的是"css 3 flip animation"
http://davidwalsh.name/demo/css-flip.php我也在玩纸牌游戏…
首先你可以这样写卡片:
<div class='card' >
<div class='faces'>
<div class='face front'> Hello</div>
<div class='face back'>Goodbye</div>
</div>
</div>
两个面都在你的html中,css将只显示一个:
.card {
-webkit-perspective: 800;
width: 50%;
height: 300px;
position: relative;
margin: 3px;
float:left; /*if you want more than one card in a line*/
}
.card .faces.flipped {
-webkit-transform: rotatey(-180deg); /* this style will help the card to rotate */
}
.card .faces {
width: 100%;
height: 100%;
-webkit-transform-style: preserve-3d; /* This is a 3d transformation */
-webkit-transition: 0.5s;/*the animation last 0.5secs*/
}
.card .faces .face {
width: 100%;
height: 100%;
position: absolute;
-webkit-backface-visibility: hidden ; /*hides the back of the card until transform*/
z-index: 2;
font-family: Georgia;
font-size: 3em;
text-align: center;
line-height: 200px;
}
.card .faces .front {
position: absolute;
z-index: 1;
background: black;
color: white;
cursor: pointer;
}
.card .faces .back {
-webkit-transform: rotatey(-180deg); /*allows the flip to the back of the card*/
background-image: url("image.jpg"); /*background image for the back if you want*/
background-size:100%;
color:white;
border:1px solid black;
}
唯一要做的就是添加'flipped'类。所以也许与其重画
$(".card").on('click',function(){
$(this).children('.faces').toggleClass('flipped');
});
联系了脚本的作者,他告诉我在当前脚本中不重写大部分代码是不可能的。所以这个问题可以结束了。谢谢你的帮助。
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- jQuery:循环一个具有不同超时值的循环
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 从javascript创建一个列表
- 节点导出返回一个空对象
- 使用clickToggle并在单击另一个元素时关闭元素
- 我可以在json对象中添加一个函数吗
- 使用javascript将动态表从一个html页面打印到另一个html页
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 表追加而不附加最后一个元素
- 只翻转一个DIV,而不是所有DIV都属于同一类
- 取一个数组,用翻转的索引/值做一个对象
- 矩形正在翻转和旋转.我只想让它绕一个圆旋转
- Jquery移动翻转开关从两个按钮中至少选择一个
- 当你点击另一个东西时,翻转它的颜色
- 寻找一个HTML5 / Javascript PDF页面翻转器
- 在画布中翻转一个精灵
- 如何添加一个简单的卡片翻转动画
- 在这个JS纸牌游戏中添加一个纸牌翻转动画
- 单击另一个DIV后恢复翻转DIV