jQuery 动画功能只工作一次
jQuery animate function work only one time
我在 SVG 中有一个使用 jQuery 动画功能的开关,但应用于我的 "circle" SVG 元素的 "cs" 属性。
$('#filtres-reglages ul li label input:checkbox').click(function (e) {
var oswitch = $(this).parent().find("svg#switch");
var path = oswitch.find("path");
var circle = oswitch.find("circle");
if($(this).is(':checked')) {
$(circle).animate(
{'foo':20},
{
step: function(foo) {
$(this).attr('cx', 10+foo);
},
duration: 200
}
);
} else {
$(circle).animate(
{'foo':20},
{
step: function(foo) {
$(this).attr('cx', 30-foo);
},
duration: 200
}
);
}
});
HTML:
<label>
<svg id="switch" class="switch-off" xml:space="preserve" enable-background="new 0 0 40 20" viewBox="0 0 40 20" y="0px" x="0px" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" version="1.1">
<g>
<g>
<path d="M30,0L30,0L30,0H10C4.5,0,0,4.5,0,10c0,5.5,4.5,10,10,10h20l0,0h0c5.5,0,10-4.5,10-10C40,4.5,35.5,0,30,0z " fill="#CCCCCC">
</g>
<g>
<circle r="8" cy="10" cx="10" fill="#FFFFFF">
</g>
</g>
</svg>
<input type="checkbox" name="champs_societes[]" value="capital_int">
<span>Capital</span>
</label>
当我的复选框被选中时,我递增 cx,当没有被选中时,我递减。
但是渐进式动画只在第一次起作用。之后直接从30切换到10,再从10切换到30,为什么?
感谢帮助
你想要做的是让 foo 从 0 变为 20,然后再回到 0。
你实际上做的是从 0 到 20,然后到 20。但是在第二部分你从 30 中减去这个值,所以你实际上只是在一个动画步骤中从 20 跳到 (30-20) .
这是你应该做的:
$('label input:checkbox').click(function (e) {
var oswitch = $(this).parent().find("svg#switch");
var path = oswitch.find("path");
var circle = oswitch.find("circle");
if($(this).is(':checked')) {
$(circle).animate(
{'foo':20},
{
step: function(foo) {
$(this).attr('cx', 10+foo);
},
duration: 200
}
);
} else {
$(circle).animate(
{'foo':0},
{
step: function(foo) {
$(this).attr('cx', 10+foo);
},
duration: 200
}
);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<svg id="switch" class="switch-off" xml:space="preserve" enable-background="new 0 0 40 20" viewBox="0 0 40 20" y="0px" x="0px" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" version="1.1">
<g>
<g>
<path d="M30,0L30,0L30,0H10C4.5,0,0,4.5,0,10c0,5.5,4.5,10,10,10h20l0,0h0c5.5,0,10-4.5,10-10C40,4.5,35.5,0,30,0z " fill="#CCCCCC"/>
</g>
<g>
<circle r="8" cy="10" cx="10" fill="#FFFFFF"/>
</g>
</g>
</svg>
<input type="checkbox" name="champs_societes[]" value="capital_int">
<span>Capital</span>
</label>
我在 SVG 中有一个使用 jQuery 动画功能的开关,但应用于我的 "circle" SVG 元素的 "cs" 属性。
$('#filtres-reglages ul li label input:checkbox').click(function (e) {
var oswitch = $(this).parent().find("svg#switch");
var path = oswitch.find("path");
var circle = oswitch.find("circle");
if($(this).is(':checked')) {
$(circle).animate(
{'foo':20},
{
step: function(foo) {
$(this).attr('cx', 10+foo);
},
duration: 200
}
);
} else {
$(circle).animate(
{'foo':20},
{
step: function(foo) {
$(this).attr('cx', 30-foo);
},
duration: 200
}
);
}
});
HTML:
<label>
<svg id="switch" class="switch-off" xml:space="preserve" enable-background="new 0 0 40 20" viewBox="0 0 40 20" y="0px" x="0px" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" version="1.1">
<g>
<g>
<path d="M30,0L30,0L30,0H10C4.5,0,0,4.5,0,10c0,5.5,4.5,10,10,10h20l0,0h0c5.5,0,10-4.5,10-10C40,4.5,35.5,0,30,0z " fill="#CCCCCC">
</g>
<g>
<circle r="8" cy="10" cx="10" fill="#FFFFFF">
</g>
</g>
</svg>
<input type="checkbox" name="champs_societes[]" value="capital_int">
<span>Capital</span>
</label>
当我的复选框被选中时,我递增 cx,当没有被选中时,我递减。
但是渐进式动画只在第一次起作用。之后直接从30切换到10,再从10切换到30,为什么?
感谢帮助
你想要做的是让 foo 从 0 变为 20,然后再回到 0。
你实际上做的是从 0 到 20,然后到 20。但是在第二部分你从 30 中减去这个值,所以你实际上只是在一个动画步骤中从 20 跳到 (30-20) .
这是你应该做的:
$('label input:checkbox').click(function (e) {
var oswitch = $(this).parent().find("svg#switch");
var path = oswitch.find("path");
var circle = oswitch.find("circle");
if($(this).is(':checked')) {
$(circle).animate(
{'foo':20},
{
step: function(foo) {
$(this).attr('cx', 10+foo);
},
duration: 200
}
);
} else {
$(circle).animate(
{'foo':0},
{
step: function(foo) {
$(this).attr('cx', 10+foo);
},
duration: 200
}
);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<svg id="switch" class="switch-off" xml:space="preserve" enable-background="new 0 0 40 20" viewBox="0 0 40 20" y="0px" x="0px" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" version="1.1">
<g>
<g>
<path d="M30,0L30,0L30,0H10C4.5,0,0,4.5,0,10c0,5.5,4.5,10,10,10h20l0,0h0c5.5,0,10-4.5,10-10C40,4.5,35.5,0,30,0z " fill="#CCCCCC"/>
</g>
<g>
<circle r="8" cy="10" cx="10" fill="#FFFFFF"/>
</g>
</g>
</svg>
<input type="checkbox" name="champs_societes[]" value="capital_int">
<span>Capital</span>
</label>