尝试利用animationend https://developer.mozilla.org/en-US/docs/Web/Events/animationend event
$.each(["webkit", "moz", "MS", "o", ""], function (k, v) {
selected[0].addEventListener(v !== "" ? v + "AnimationEnd" : "animationend"
, function (e) {
$(this).unbind('click')
.promise().done(function (el) {
el.fadeOut(500);
console.log("done")
});
})
})
jsfiddlehttp://jsfiddle.net/guest271314/x7gqb1g4/ http://jsfiddle.net/guest271314/x7gqb1g4/
另一种方法;通过维护“无限”循环count !== 0
,通过调用“停止”.stop()
, clearQueue()
环境.data()
flag
// v2
// `d` : duration , `count` : iteration
(function ($) {
$.fn.blink = blink;
function blink(d, count) {
var el = $(this);
$.fx.interval = 0;
return el.each(function (i, elem) {
var elem = $(elem);
elem.data("count", count);
return elem.fadeTo(d, "0", "linear", function () {
elem.fadeTo(d, "1", "linear", function () {
elem.data("count", --count);
return (elem.data("count") !== 0 && !elem.data("stop")
? elem.blink(d, elem.data("count"))
: elem.stop(true, true).data("stop", false))
})
})
}).promise("fx")
};
}(jQuery));
// e.g.,
var selected = $("div")
, button = $("button:first")
, stop = $("button:last");
selected.on("click", function (e) {
// start `$.fn.blink` , log returned `promise` on stop
$(this).blink(750, 10).then(function (el) {
console.log(el, el.queue(), el.data());
})
});
button.on("click", function () {
// unbind `click` event
selected.unbind('click')
.promise().then(function (el) {
el.fadeOut(500);
});
});
stop.on("click", function () {
// stop animation
selected.data("count", null).data("stop", true).clearQueue()
})
jsfiddlehttp://jsfiddle.net/guest271314/33ptL9do/ http://jsfiddle.net/guest271314/33ptL9do/