jQuery简单左右动画
我希望箭头明显地移动到数字并在返回时删除文本。
http://jsfiddle.net/mplungjan/VZsG4/
<span class="arrow">→</span> <span id="span0">zhong</span><span id="span2">1</span><span id="span3">guo</span><span id="span4">2</span>
<br />
<input type="button" id="start" value="start" />
<input type="button" id="reset" value="reset" />
var cnt = 0;
var spanLength = $("span").length;
$("#start").click(function() {
var tId = setInterval(
function() {
if (cnt>spanLength-1) clearInterval(tId);
$(".arrow").animate(
{"right": $("#span"+(cnt+1)).position()}, "slow",
function(){
$("#span"+(cnt++)).hide(); // remove word
$("#span"+(cnt++)).hide(); // remove number
$(".arrow").animate({"left":0}); // move back
}
);
},
1000);
});
$("#reset").click(function() {
clearInterval(tId);
$("span").each(function() {
$(this).show();
});
});
I would like the arrow to move visibly to the digit and remove the text on the way back.
http://jsfiddle.net/mplungjan/VZsG4/
<span class="arrow">→</span> <span id="span0">zhong</span><span id="span2">1</span><span id="span3">guo</span><span id="span4">2</span>
<br />
<input type="button" id="start" value="start" />
<input type="button" id="reset" value="reset" />
var cnt = 0;
var spanLength = $("span").length;
$("#start").click(function() {
var tId = setInterval(
function() {
if (cnt>spanLength-1) clearInterval(tId);
$(".arrow").animate(
{"right": $("#span"+(cnt+1)).position()}, "slow",
function(){
$("#span"+(cnt++)).hide(); // remove word
$("#span"+(cnt++)).hide(); // remove number
$(".arrow").animate({"left":0}); // move back
}
);
},
1000);
});
$("#reset").click(function() {
clearInterval(tId);
$("span").each(function() {
$(this).show();
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码存在一些问题:
.arrow
需要带有position:relative
的 CSS,left
进行动画处理,而不是对right
进行动画处理箭头上的 code>position()
的left
属性var tId
需要处于更广泛的范围内,.arrow
是一个跨度,您跳过的#span1
从未清除间隔cnt = 0
需要添加到重置功能在此处查看工作小提琴→
You had a few issues with your code:
.arrow
needs CSS withposition:relative
left
notright
on the arrowleft
property ofposition()
var tId
needs to be in the broader scope.arrow
is a span, the interval was never being cleared#span1
which was causing a missing element errorcnt = 0
needs to be added to the reset functionSee working fiddle here →