jquery - 用 jquery 旋转然后做其他事情

标签 jquery html css jquery-animate

我正在尝试使用 jquery 旋转一个元素,然后我想在它完成后做一些其他事情。 轮换有效,但为什么警报无效?

参见 jsfiddle:JSFiddle

HTML:

<div class="square">click me</div>

CSS:

.square{
background:red;
height:100px;
width:100px;
cursor:pointer;

JS:

$('.square').click(function () {
  rotate($(this), -180, 1000);
});
function rotate(element, degrees, speed) {
    element.animate({ borderSpacing: degrees }, {
        step: function (now, fx) {
            $(this).css('-webkit-transform', 'rotate(' + now + 'deg)');
            $(this).css('-moz-transform', 'rotate(' + now + 'deg)');
            $(this).css('transform', 'rotate(' + now + 'deg)');
        },
        duration: speed
    }, function () {
        alert("I'm done");
    });
}

最佳答案

你可以更新到这个:

.promise().done(function () {
    alert("E");
 });

Demo


您更新后的函数:

function rotate(element, degrees, speed) {
    element.animate({
        borderSpacing: degrees
    }, {
        step: function (now, fx) {
            $(this).css('-webkit-transform', 'rotate(' + now + 'deg)');
            $(this).css('-moz-transform', 'rotate(' + now + 'deg)');
            $(this).css('transform', 'rotate(' + now + 'deg)');
        },
        duration: speed
    }, 'linear').promise().done(function () { // update like this
        alert("E");
    });
}

使用complete:fn回调:


function rotate(element, degrees, speed) {
    element.animate({
        borderSpacing: degrees
    }, {
        duration: speed,
        step: function (now, fx) {
            $(this).css('-webkit-transform', 'rotate(' + now + 'deg)');
            $(this).css('-moz-transform', 'rotate(' + now + 'deg)');
            $(this).css('transform', 'rotate(' + now + 'deg)');
        },
        complete: function () {
            alert("E");
        }
    });
}

Demo with complete:fn callback


关于jquery - 用 jquery 旋转然后做其他事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24117917/

相关文章:

javascript - javascript 检索 url 表单数据的问题

html - 更改嵌套 div 中元素的样式

HTML、CSS 悬停在图像上

javascript - jQuery Promise 执行顺序与 JavaScript Promise 不同

javascript - 将标签文本拖入段落

jquery - 仅当页面为 "short"时才将页脚放在底部

javascript - 如何在 php 表单重定向后增加数字?

jquery-ui - css3.0 - 内容属性

javascript - Angular nvd3 堆栈面积图 - 扩展选项 - 0% 到 100% 范围

javascript - 设置一次日期格式,因此 jquery 和 jqueryUI 日期选择器使用该日期格式