javascript - 移动没有动画的div

标签 javascript jquery html

下面的代码工作得很好,但我想用更高效的东西来改变 .animate 效果。我不想要 css 的过渡,而是在不显示运动的情况下移动 div 的东西。我需要“立即”执行此操作。

$(document).on("click",".box",function(){
    var transparent = $(".transparent");
    var transparent_left = transparent.offset().left;
    var transparent_top = transparent.offset().top;
    var box = $(this);
    var width = parseInt(box.outerWidth());
    var this_top = box.offset().top;
    var this_left = box.offset().left;
    var result;

    if(transparent_top === (this_top + width)
    && transparent_left === this_left){ 
        change_score();
        box.finish().animate({top: "+="+width}, 100);
        transparent.finish().animate({top: "-="+width}, 100, function(){animate_callback(result)});
    }else if(transparent_top === this_top
    && transparent_left === (this_left + width)){ 
        change_score();
        box.finish().animate({left: "+="+width}, 100);
        transparent.finish().animate({left: "-="+width}, 100, function(){animate_callback(result)});
    }else if(transparent_top === this_top 
    && (transparent_left + width) === this_left){ 
        change_score();
        box.finish().animate({left: "-="+width}, 100);
        transparent.finish().animate({left: "+="+width}, 100, function(){animate_callback(result)});
    }else if((transparent_top + width) === this_top
    && transparent_left === this_left){ 
        change_score();
        box.finish().animate({top: "-="+width}, 100);
        transparent.finish().animate({top: "+="+width}, 100, function(){animate_callback(result)});
    }
});

最佳答案

如果您不想使用transferanimation,剩下的方法是:

设置你的div

position: absolute;

并通过 javascript 更改 div 的顶部、左侧、右侧、底部。

  1. 如果您不想要绝对位置,您可以通过更改边距来更改 div 的位置。

关于javascript - 移动没有动画的div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40865216/

相关文章:

javascript - PHP Ajax 调用结束时刷新当前页面

javascript - 使用普通 javascript 单击删除父元素

html - 使用多个类来设置 CSS 样式

java - WebView 中的 Youtube 视频无法自动播放

javascript - AngularJS 从 Controller 外部的 AJAX 获取返回值

javascript - 将所有 href 值更改为数组值理论与实践

javascript - 当括号中的变量值破坏我的 addClass 时,jquery 是转义括号

Javascript在底部,函数调用在主体?

html - 将纯色背景应用于 p 标签

javascript - 在用文本填充某些字段时启用提交按钮