【动画】JQuery实现冒泡排序算法动画演示

1 前言

冒泡排序是大家最熟悉的算法,也是最简单的排序算法,因其排序过程很象气泡逐渐向上漂浮而得名。为了更好的理解其基本的思想,毛三胖利用JQuery实现了冒泡排序的动画演示,并计划陆续实现其它排序算法的动画演示。现将冒泡排序JQuery实现的基本思路和代码分享如下:

2 动画演示

2.1 演示地址

冒泡排序动画演示

2.2 30秒GIF

演示动画前30秒gif图,图片大小1.60M。

30秒动画

3 动画设计及实现

因为JavaScript中并不存在类似sleep()这样的函数,所以只能利用setInterval()来实现动画效果。因此不能利用算法的双重循环实现方式,只能算法过程拟合到一个时间轴上来实现动画效果。

3.1 Html代码

<ul id="myList">
    <li>97</li>
    <li>72</li>
    <li>68</li>
    <li>29</li>
    <li>51</li>
    <li>45</li>
    <li>88</li>
    <li>32</li>
    <li>41</li>
    <li>12</li>
</ul>

3.2 核心JS代码

每隔一秒执行一次协作,完成排序后清除interval

function go() {
    //设置当前相比较两元素样式
    setCss();
    interval = setInterval(function () {
        //times趟数,达到数组长度-1,结束循环
        if(times < count -1) {
            //交换函数,如必要实现两元素交换
            var change = exchange();
            //如不交换,指针向前
            if (!change) {
                current++;
                next++;
                //内部循环次数逐渐减少
                if (current == count - 1 - times) {
                    times++;
                    current = 0;
                    next = 1;
                    //保留每一趟的历史数据
                    drawData();
                }
                setCss();
            }
        } else {
            //排序完成,清理
            $lis.removeClass("active");
            clearInterval(interval);
        }
    },1000);
}

3.3 交换动效

交换的动态效果利用了github的JQuery的swap,地址:Github jquery.swap.js

源代码如下:

(function( $ ) {
    $.fn.swap = function(a, b) {
        t = this
        if(t.length == 1 && a.length == 1 && b == undefined ){
            return _swap(t, a);
        }else if(t.length > 1 && typeof(a) == "number" && typeof(b) == "number" ){
            _swap(t[a], t[b])
            return t;
        }else{
            $.error( 'Argument Error!' );
        }
    };

    function _swap(a, b){
        var from = $(a),
            dest = $(b),
            from_pos = from.offset(),
            dest_pos = dest.offset(),
            from_clone = from.clone(),
            dest_clone = dest.clone(),
            total_route_vertical   = dest_pos.top + dest.height() - from_pos.top,
            route_from_vertical    = 0,
            route_dest_vertical    = 0,
            total_route_horizontal = dest_pos.left + dest.width() - from_pos.left,
            route_from_horizontal  = 0,
            route_dest_horizontal  = 0

        from.css("opacity", 0);
        dest.css("opacity", 0);

        from_clone.insertAfter(from).css({position: "absolute", width: from.outerWidth(), height: from.outerHeight()}).offset(from_pos).css("z-index", "999")
        dest_clone.insertAfter(dest).css({position: "absolute", width: dest.outerWidth(), height: dest.outerHeight()}).offset(dest_pos).css("z-index", "999")

        if(from_pos.top != dest_pos.top)
            route_from_vertical = total_route_vertical - from.height()
        route_dest_vertical = total_route_vertical - dest.height()
        if(from_pos.left != dest_pos.left)
            route_from_horizontal = total_route_horizontal - from.width()
        route_dest_horizontal = total_route_horizontal - dest.width()

        from_clone.animate({
                top: "+=" + route_from_vertical + "px",
                left: "+=" + route_from_horizontal + "px",
            },
            "slow",
            function(){
                dest.insertBefore(this).css("opacity", 1);
                $(this).remove();
            });

        dest_clone.animate({
                top: "-=" + route_dest_vertical + "px",
                left: "-=" + route_dest_horizontal + "px"
            },
            "slow",
            function(){
                from.insertBefore(this).css("opacity", 1);
                $(this).remove();
            });

        return from;
    }
})( jQuery );

4 结语

目前,只完成了冒泡排序和直接插入排序两个算法的动画演示,其它的慢慢来完成吧。要学习完整的源代码可直接源文获取。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,111评论 25 709
  • 所谓“方法论”,是指以解决问题为目的的一种思考方式或可执行性强的策略。把握住事物的根本规则,可以复用,切实有效。 ...
    小和阅读 3,873评论 0 4
  • 1.labyrinthine The streets of the Old City are narrow and...
    Mr_Oldman阅读 1,157评论 0 0
  • ——说书人的一场戏 原本只剩下浅薄的叙事,来铺垫起这份仪式感般出戏的生命,前些天听到的歌,歌词都是那样的陌生,唯一...
    天空l阅读 3,790评论 0 51
  • 各行各业都缺少不了销售岗位,之前各行各业的销售工作经历中,“销售冠军”没那么亮眼。那仅仅是个人维度的成功,是以个人...
    Candice欢阅读 1,531评论 0 0

友情链接更多精彩内容