我从这个网站中得到了一个惊人的波纹效应实现,它使用SVG/循环元素和一个jQuery动画函数来创建用户单击时的连锁反应效果。虽然我对基本编程有一定的了解,但是我对JavaScript和jQuery方法/函数了解较少,所以我从我对JS和jQuery的研究中读到了很多。
我发现了连锁反应的实现;有用的,重量轻和容易,所以我想探索和扩展的代码,以适应我的项目。
好的,我需要知道的第一件事是,当动画停止动画时,我如何重新设置动画所做的更改?我知道这对你来说是个简单的问题,但作为JS和jQuery的初学者,我怎么能做到这一点呢?
下面是演示代码,看看这里到底发生了什么。
我尝试添加函数(如代码中所示):
complete : function(){c.removeAttr('style');}但是没有什么改变,在动画结束后,它仍然存在。
知道我错过了什么吗?
发布于 2016-07-18 07:46:13
将完整回调更改为:
complete : function() { $(this).remove(); }发布于 2016-07-18 07:51:35
为了从循环的样式属性中删除"r“,请尝试
complete : function(){c.css( "r", "" );}Ref:http://api.jquery.com/css/将样式属性的值设置为空字符串(例如,$( "#mydiv" ).css( "color", "" ) )从元素中移除该属性。
或
complete : function(){c.css( "r", "0" );}试着运行下面的代码段:
(function(){
$(".button").on("click", function(e){
var yOffset = e.pageY - $(this).offset().top;
var xOffset = e.pageX - $(this).offset().left;
var self = this;
var xPos = parseInt(xOffset);
var yPos = parseInt(yOffset);
$(this).find("svg").remove(); // Remove existing animation changes
$(this).append('<svg><circle cx="'+xPos+'" cy="'+yPos+'" r="'+0+'"></circle></svg>');
/* Make the animation of SVG - Circle */
var c = $(self).find("circle");
c.animate(
{
"r" : $(self).outerWidth()
},
{
easing: "easeOutQuad",
duration: 500,
step : function(val){},
/*-------------------------
THIS FUNCTION SHOULD WORK
--------------------------*/
complete : function(){c.css( "r", "0" );}
}
);
});
}());.button {
position: relative;
display: inline-block;
height: 40px;
padding: 0 20px;
margin: 0;
background: transparent;
border: none;
color: rgb(0,0,0,0);
font-size: 20px;
font-weight: 900;
text-align: center;
text-transform: uppercase;
-webkit-tap-highlight-color: transparent;
}
.button:hover {
background-color: rgba(158,158,158,0.10);
}
.button:active,
.button:focus {
background-color: rgba(158,158,158, 0.30);
outline: 0;
}
/* SVG - Circle for the ripple effect */
.button svg {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
.button circle {
fill: rgba(0,0,0,0.50);
}<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Reset jQuery animation when it stops animating</title>
</head>
<body>
<button class="button">Button</button>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js'></script>
</body>
</html>
发布于 2016-07-18 07:46:12
您应该使用停止方法来动画停止- https://api.jquery.com/stop/。
您还可以使用clearQueue方法。
https://stackoverflow.com/questions/38431050
复制相似问题