
引入jquery
然后给你要设置动画的对象增加或者删除css3动画的类就可以了。
如我这里用colorchange这个渐变类在css里面写好动画效果以后在js里面给对象添加上就可以实现动画了
<!DOCTYPE html><html>
<head lang="en">
<meta charset="UTF-8">
<title>Test</title>
<style type="text/css">
body{
padding: 20px
background-color:#FFF
}
.colorchange
{
animation:myfirst 5s
-moz-animation:myfirst 5s /* Firefox */
-webkit-animation:myfirst 5s /* Safari and Chrome */
-o-animation:myfirst 5s /* Opera */
}
@keyframes myfirst
{
from {background:red}
to {background:yellow}
}
@-moz-keyframes myfirst /* Firefox */
{
from {background:red}
to {background:yellow}
}
@-webkit-keyframes myfirst /* Safari and Chrome */
{
from {background:red}
to {background:yellow}
}
@-o-keyframes myfirst /* Opera */
{
from {background:red}
to {background:yellow}
}
#main{
width:100px
height:100px
background:red
}
#cgbt{
width: 100px
margin: 20px 0 0 0
text-align: center
cursor: pointer
}
#cgbt:hover{
background-color: #2D93CA
}
</style>
</head>
<body>
<div id="main">
我会变么?
</div>
<div id="cgbt">
点我让上面的变颜色
</div>
<script src="jquery-3.2.1.min.js" type="application/javascript"></script>
<script>
$(document).ready(function(){
$("#cgbt").click(function(){
$("#main").attr("class","colorchange")
})
})
</script>
</body>
</html>
你可以看下jquery的源代码,animate: function( prop, speed, easing, callback ) {
var empty = jQuery.isEmptyObject( prop ),
optall = jQuery.speed( speed, easing, callback ),
doAnimation = function() {
// Operate on a copy of prop so per-property easing won't be lost
var anim = Animation( this, jQuery.extend( {}, prop ), optall )
// Empty animations, or finishing resolves immediately
if ( empty || jQuery._data( this, "finish" ) ) {
anim.stop( true )
}
}
doAnimation.finish = doAnimation
return empty || optall.queue === false ?
this.each( doAnimation ) :
this.queue( optall.queue, doAnimation )
}
jQuery.speed = function( speed, easing, fn ) {
var opt = speed &&typeof speed === "object" ? jQuery.extend( {}, speed ) : {
complete: fn || !fn &&easing ||
jQuery.isFunction( speed ) &&speed,
duration: speed,
easing: fn &&easing || easing &&!jQuery.isFunction( easing ) &&easing
}
opt.duration = jQuery.fx.off ? 0 : typeof opt.duration === "number" ? opt.duration :
opt.duration in jQuery.fx.speeds ? jQuery.fx.speeds[ opt.duration ] : jQuery.fx.speeds._default
// normalize opt.queue - true/undefined/null ->"fx"
if ( opt.queue == null || opt.queue === true ) {
opt.queue = "fx"
}
// Queueing
opt.old = opt.complete
opt.complete = function() {
if ( jQuery.isFunction( opt.old ) ) {
opt.old.call( this )
}
if ( opt.queue ) {
jQuery.dequeue( this, opt.queue )
}
}
return opt
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)