I was hoping css3 animations, especially when I mark it as "forwards" would actually change my element's transforms. But it doesn't.
See: http://jsfiddle.net/foreyez/3b3f6oyx/
---USE CHROME BROWSER---
HTML:
<div style='position:fixed;bottom:10px'>
<button onclick='down()'>Go Down</button>
<button onclick='right()'>Go Right</button>
</div>
CSS:
@-webkit-keyframes animdownkf {
0% { }
100% { transform: translateY(100px); }
}
@-webkit-keyframes animrightkf {
0% { }
100% { transform: translateX(100px); }
}
#box {
width:50px;
height:50px;
background:black;
}
button {
height:100px;
width:100px;
}
.animdown {
-webkit-animation:1s animdownkf forwards;
}
.animright {
-webkit-animation:1s animrightkf forwards;
}
JAVASCRIPT:
function down() {
$('#box').removeClass('animright');
$('#box').addClass('animdown');
}
function right() {
$('#box').removeClass('animdown');
$('#box').addClass('animright');
}
What I'd like it to do is persist state, so if I clicked on "go down" it would go down, and then if I clicked on "go right" it would go right FROM THE LOCATION it is currently at.
But it doesn't seem to do this. I know css3 transitions allows to do this but I specifically want css3 animations because I need to use keyframes. I just want my 100% to change the actual element's style. Is there a way to do this?
You need jquery for that...
http://jsfiddle.net/swestphal/9kanuLc5/
<script type="text/javascript">
$(document).ready(function() {
$('#moveright').click(function() {
$('#box').animate({
'marginLeft' : "+=100px" //moves right
});
});
$('#movedown').click(function() {
$('#box').animate({
'marginTop' : "+=100px" //moves down
});
});
});
</script>
<button id="moveright">Move right</button> <button id="movedown">Move Down</button><div id="box" style="position:absolute;">x</div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.