0

使用 jQuery UIswitchClass方法,它吹了!动画断断续续,至少可以说是不可取的。我正在寻找一种方法来完成相同的代码,可能更快、更清晰、更流畅的动画。最好不使用 jQuery UI。

参考位于<head>页面的标签:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>

我的 jQuery 代码:

$(window).load(function() {
    $(".container").removeClass("hidden");
    $(".container").css({'height':  $(".can2").height()});
    scrollToElement($("#main_menu"));
    var sDelay = 0,
        sDuration = 2000;
    
    var numCandidates = $(".candidate").length;
    $(".candidate").delay(500).each(function(index) {
        var i = parseInt(index) >= 10 ? ++i : 0;
        var $this = $(this);

        if (i > 0)
        {
            $(this).addClass('can' + i);
            var curIndex = 0;
            sDelay = sDelay + 2000;
        }
        else
        {
            var curIndex = (numCandidates - index); // 13, 12, 11, 10, 9, 8, 7, 6, 5, 4
        }

        $(this).css({ left: $(this).offset().left }).delay(sDelay).animate({
            left: i == 0 ? "-10%" : 0
        }, {
            duration: sDuration,
            specialEasing: {
                width: "linear",
                left: i == 0 ? "linear" : "easeOutBounce"
            },
            complete: function() {
                if (i == 0)
                {
                    for (var s=curIndex; s<13; s++)
                    {
                        // alert(".can" + (s+1));
                        $(".can" + (s+1)).switchClass("can" + (s+1), "can" + s, 350, "easeInOutCirc");
                    }
                    $this.addClass("can13", 350, "easeInOutCirc");
                }
                else
                {
                    $(".name").eq(parseInt($(this).data("index"))).text($(this).data("name")).fadeIn('2000');
                }
            }
        });
        sDelay += 1000;
    });
});

HTML结构:

<div class="fullwidth">
    <div class="container hidden">
        <div class="candidate" data-name="Julia Richards"><img class="floatcenter" src="images/head1.png" alt="" /></div>
        <div class="candidate" data-name="Mark Anthony"><img class="floatcenter" src="images/head2.png" alt="" /></div>
        <div class="candidate" data-name="John Davis"><img class="floatcenter" src="images/head3.png" alt="" /></div>
        <div class="candidate" data-name="Cheung Lee"><img class="floatcenter" src="images/head4.png" alt="" /></div>
        <div class="candidate" data-name="Steve Austin"><img class="floatcenter" src="images/head5.png" alt="" /></div>
        <div class="candidate" data-name="Richard Baxter"><img class="floatcenter" src="images/head6.png" alt="" /></div>
        <div class="candidate" data-name="Christina Oakfield"><img class="floatcenter" src="images/head7.png" alt="" /></div>
        <div class="candidate" data-name="Derek Brown"><img class="floatcenter" src="images/head8.png" alt="" /></div>
        <div class="candidate" data-name="Abigail Harris"><img class="floatcenter" src="images/head9.png" alt="" /></div>
        <div class="candidate" data-name="Cindy Krause"><img class="floatcenter" src="images/head10.png" alt="" /></div>
        <div class="candidate can1" data-name="Phillip Wright" data-index="0"><img class="floatcenter" src="images/head11.png" alt="" /></div>
        <div class="candidate can2" data-name="Brendan Johnson" data-index="1"><img class="floatcenter" src="images/head12.png" alt="" /></div>
        <div class="candidate can3" data-name="Chris Lawler" data-index="2"><img class="floatcenter" src="images/head13.png" alt="" /></div>
        <!--// Candidate Names //-->
        <div class="name mission_gothic_reg" style="margin-left: 5%;"></div>
        <div class="name mission_gothic_reg featured_name" style="margin-left: 39%;"></div>
        <div class="name mission_gothic_reg" style="margin-left: 71%;"></div>
    </div>
    <br class="clear" />
</div>

CSS:

.fullwidth {
    width: 100%;
}
.hidden {
    display: none;
}
.container {
    margin: 6em auto 2em; 
    width: 80%; 
    position: relative; 
    display: block;
}
.featured_name {
    bottom: -1em;
    margin-left: 30%;
    width: 40%;
    font-size: 1.5em;
    opacity: 1;
    z-index: 13;
    text-shadow: 0 0 .2em rgba(255, 255, 255, 0.8);
}
.name {
    position: absolute;
    bottom: .8em; 
    text-align: center; 
    opacity: .6; 
    width: 25%; 
    height: 1.5em; 
    line-height: 1.5em; 
    font-size: 1.2em;
    z-index: 13;
    display: none;
}
.candidate {
    position: absolute;
    width: 35%;
    height: auto;
    border-radius: 50%;
    right: -40%;
    border: .1em solid white;
    -moz-box-shadow: 0px 0px .2em rgba(0,0,0,0.8);
    -webkit-box-shadow: 0px 0px .2em rgba(0,0,0,0.8);
    box-shadow: 0px 0px .2em rgba(0,0,0,0.8);
}

.can4, .can5, .can6, .can7, .can8, .can9, .can10, .can11, .can12, .can13 {
    left: -10%;
}

.can13 {
    width: 22% !important;
    opacity: 0.6;
    margin-top: -5%;
    margin-left: 3%;
    z-index: 9;
}

.can12 {
    width: 15% !important;
    opacity: 0.4;
    margin-top: -10%;
    margin-left: 20%;
    z-index: 8;
}

.can11 {
    width: 12% !important;
    opacity: 0.3;
    margin-top: -12%;
    margin-left: 33%;
    z-index: 7;
}

.can10 {
    width: 10% !important;
    opacity: 0.2;
    margin-top: -14%;
    margin-left: 43%;
    z-index: 6;
}

.can9 {
    width: 10% !important;
    opacity: 0.2;
    margin-top: -14%;
    margin-left: 52%;
    z-index: 5;
}

.can8 {
    width: 10% !important;
    opacity: 0.2;
    margin-top: -14%;
    margin-left: 61%;
    z-index: 5;
}

.can7 {
    width: 10% !important;
    opacity: 0.2;
    margin-top: -13%;
    margin-left: 70%;
    z-index: 6;
}

.can6 {
    width: 12% !important;
    opacity: 0.3;
    margin-top: -12%;
    margin-left: 79%;
    z-index: 7;
}

.can5 {
    width: 15% !important;
    opacity: 0.4;
    margin-top: -10%;
    margin-left: 89%;
    z-index: 8;
}

.can4 {
    width: 22% !important;
    opacity: 0.6;
    margin-top: -5%;
    margin-left: 98%;
    z-index: 9;
}

.can3 {
    margin-top: 0;
    margin-left: 66%;
    opacity: 0.8;
    width: 35% !important;
    z-index: 10;
}

.can2 {
    margin-left: 30%;
    width: 40% !important;
    z-index: 11;
    opacity: 1;
    margin-top: 1em;         
}

.can1 {
    margin-left: 0;
    width: 35% !important;
    z-index: 10;
    opacity: 0.8;
    margin-top: 0;
}

最终结果图片: 动画完成后

.switchClass使用jQuery UI 完成此 操作的URL : http ://opportunityfinance.net/Test/newest_conference-2013/meetcandidates.html

有没有办法做到这一点更清洁,代码可能更少(但这对我来说并不重要),使用更少的系统资源,以及整体上更流畅和/或恒定的动画?或者根本不使用 jQuery UI,因为 jQuery UI.switchClass方法有时会跳过一个元素。

更新:

将 jQuery 代码更改为以下...这仍然可以改进性能吗?

$(window).load(function() {
    $("#container").removeClass("hidden");
    $("#container").css({'height':  $(".can2").height()});
    scrollToElement($("#main_menu"));
    var sDelay = 0,
        sDuration = 2000;
    var xName = 0;
    
    var numCandidates = $(".candidate").length;
    $("#container .candidate").delay(500).each(function(index) {
        var i = index >= 10 ? ++i : 0;
        var $this = $(this);

        if (i > 0)
        {
            $this.addClass('can' + i);
            var curIndex = 0;
            sDelay = sDelay + 1500;
        }
        else
        {
            var curIndex = (numCandidates - index); // 13, 12, 11, 10, 9, 8, 7, 6, 5, 4
        }

        $(this).css({ left: $this.offset().left }).delay(sDelay).animate({
            left: i == 0 ? "-10%" : 0
        }, {
            duration: sDuration,
            specialEasing: {
                width: "linear",
                left: i == 0 ? "linear" : "easeOutBounce"
            },
            complete: function() {
                if (i == 0)
                {
                    for (var s=curIndex; s<13; s++)
                    {
                        // alert(".can" + (s+1));
                        $("#container .can" + (s+1)).switchClass("can" + (s+1), "can" + s, 400, "easeInOutCirc");
                    }
                    $this.addClass("can13", 400, "easeInOutCirc");
                }
                else
                {
                    ++xName;
                    $("#name" + xName).text($this.data("name")).fadeIn('2000');
                }
            }
        });
        sDelay += 650;
    });
});

将 HTML 从指向类name值更改,而是这样做:

<div id="name1" class="mission_gothic_reg" style="margin-left: 5%;"></div>
<div id="name2" class="mission_gothic_reg" style="margin-left: 39%;"></div>
<div id="name3" class="mission_gothic_reg" style="margin-left: 71%;"></div>

此外,在存在container类名的 id 值中添加container,因此我可以引用 id 而不是类并将其用作其他类选择器的父级。

为此更改了 CSS(删除.name.featured_namecss 规则并添加了以下内容):

#name2 {
    bottom: -1em;
    margin-left: 30%;
    width: 40%;
    font-size: 1.5em;
    opacity: 1;
    z-index: 13;
    text-shadow: 0 0 .2em rgba(255, 255, 255, 0.8);
}
#name1, #name2, #name3 {
    position: absolute;
    bottom: .8em; 
    text-align: center; 
    opacity: .6; 
    width: 25%; 
    height: 1.5em; 
    line-height: 1.5em; 
    font-size: 1.2em;
    z-index: 13;
    display: none;
}

我还能做些什么来改进这个动画吗?因为它对我来说似乎仍然有点滞后......

另外,我注意到宽度在动画宽度下降时会稍微倾斜。意思是高度下降了一点,但过了一会儿它又回到了正确的宽度和高度,我能做些什么吗?

4

0 回答 0