变换旋转和翻转效果- CSS3和jQuery
Transform rotate and flip effect - CSS3 and jQuery
我正在制作一款纸牌游戏并制作一个简单的翻转效果。我坚持使用CSS旋转。
没有旋转的代码工作得很好:
$(document).ready(function() {
var card = $("#card");
var width = card.width();
var height = card.height();
var left = card.offset().left;
var top = card.offset().top;
card.delay(500).animate({
"width" : "0px",
"height" : height+"px",
"left" : "+="+(width / 2)+"px"
}, 1000, function() {
$(this).css('background-color', '#ff0000').animate({
"width" : width+"px",
"left" : "-="+(width / 2)+"px"
}, 1000);
});
});
https://jsfiddle.net/Lo8egkra/1/但是如果我添加旋转:
$(document).ready(function() {
var card = $("#card");
card.css({"transform": "rotate(90deg)"});
var width = card.width();
var height = card.height();
var left = card.offset().left;
var top = card.offset().top;
card.delay(500).animate({
"width" : "0px",
"height" : height+"px",
"left" : "+="+(width / 2)+"px"
}, 1000, function() {
$(this).css('background-color', '#ff0000').animate({
"width" : width+"px",
"left" : "-="+(width / 2)+"px"
}, 1000);
});
});
https://jsfiddle.net/Lo8egkra/2/你可以看到。它改变它的位置和宽度和高度大小,翻转效果是相当错误的。可能是我做错了什么,但我已经尝试了几个小时,没有成功。
我很难用javascript做到这一点,所以我建议用CSS3来解决这个问题。
这是一个代码片段,CSS看起来有点长,但这是因为浏览器的所有前缀:
$(document).ready(function() {
$("button.rotate").click(function() {
$("#card").css({
"transform": "rotate(90deg)"
});
$("#card").toggleClass('flip');
setTimeout(function() {
$('#card').toggleClass('flip');
}, 1000);
$("button").hide();
});
$("#card").toggleClass('flip');
setTimeout(function() {
$('#card').toggleClass('flip');
}, 1000);
});
#card {
position: relative;
width: 300px;
height: 200px;
}
#card .front {
position: absolute;
z-index: 999;
-webkit-transform: rotateX(0deg) rotateY(0deg);
-webkit-transform-style: preserve-3d;
-webkit-backface-visibility: hidden;
-moz-transform: rotateX(0deg) rotateY(0deg);
-moz-transform-style: preserve-3d;
-moz-backface-visibility: hidden;
transition: all 1s ease-in-out;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
}
#card.flip .front {
z-index: 9;
-webkit-transform: rotateX(180deg);
-moz-transform: rotateX(180deg);
}
#card .back {
position: absolute;
width: 300px;
height: 200px;
background: #ff0000;
overflow: hidden;
z-index: 999;
-webkit-transform: rotateX(-180deg);
-webkit-transform-style: preserve-3d;
-webkit-backface-visibility: hidden;
-moz-transform: rotateX(-180deg);
-moz-transform-style: preserve-3d;
-moz-backface-visibility: hidden;
transition: all 1s ease-in-out;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
}
#card.flip .back {
z-index: 10;
-webkit-transform: rotateX(0deg) rotateY(0deg);
-moz-transform: rotateX(0deg) rotateY(0deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="card">
<div class="front">
<img width="300" height="200" src="http://orig02.deviantart.net/4493/f/2009/309/5/a/joker_card_by_hojcat.jpg">
</div>
<div class="back"></div>
</div>
<br /><br />
<button class="rotate">Rotate</button>
无论如何,如果这不是适合你的解决方案,我希望它能给你一些知识,有其他的方法来做翻转
哦,我想我明白了。
$(document).ready(function() {
var card = $("#card");
card.css({"transform": "rotate(90deg)"});
var width = card.width();
var height = card.height();
var left = card.offset().left;
$({width : height}).delay(500).animate({width : 0}, {
duration: 500,
step: function (now) {
card.css({"width" : now, "height" : width});
card.css("left", left + (width / 2 - card.height() / 2));
},
complete: function() {
card.css('background-color', '#ff0000');
$({width : 0}).animate({width : height}, {
duration: 500,
step: function(now) {
card.css({"width" : now, "height" : width});
card.css("left", left + (width / 2 - card.height() / 2));
}
});
}
});
});
https://jsfiddle.net/Lo8egkra/3/相关文章:
- jQuery鼠标输出调用CSS3动画
- Jquery and CSS3 Rotation
- 如何在不使用css转换的情况下将jQuery animate与css3属性相结合
- 将 JQuery Animation 转换为 Javascript/CSS3
- 'toggel'使用css3/jquery来回旋转元素
- 为什么当我用jQuery制作长方体动画时,CSS3长方体阴影会分离
- 添加CSS3转换浮动导航栏-JQUERY
- CSS3 replacement for jQuery.fadeIn and fadeOut
- jQuery有没有,或者有没有jQuery插件,内置了监听CSS3动画事件的功能(例如animationEnd)
- jquery和多个css3背景
- CSS3转换+jQuery翻转卡问题
- 我将如何使用CSS3对动态创建的jQuery UI元素进行样式设置
- 如何在 jquery transit 中暂停和重新启动 css3 转换
- 如何使用 jQuery 或 CSS3 使搜索栏响应式
- 带有 JQuery 动画的 CSS3 菜单无法正确格式化和显示
- CSS3/Jquery Bubbles animation
- 如何实现推送页面的 JQuery 或 CSS3 响应式侧边菜单
- 使用 jQuery fadeIn 还是 CSS3 动画更好
- CSS3 Jquery Gradients-有什么方法可以读取设置的值吗
- 为什么[我的]CSS3/jQuery转换如此不完美地平滑,以及我如何使它们更加平滑