我只是試圖建立一些與jQuery的動畫,這是一個有點棘手的一個奇特看動畫。但那個看起來不錯(DEMO)。
var expElem = $(".hover-spell");
var exp = expElem.text();
var run = false;
expElem.empty();
for(var i = 0; i <= exp.length; i++) {
expElem.append('<span>'+exp.charAt(i)+'</span>');
}
expElem.mouseover(function() {
if(run === true) return false;
run = true;
var stepDuration = 300;
var counter = 0;
(function anim(){
if(counter == exp.length -1) return false; //Remove -1 to get last "e" animated
counter++;
var nth = exp.length;
var elem = $('span:nth-child('+nth+')', expElem);
elem.slideUp(stepDuration, function() {
(function() {
if(counter == 1) return elem.prependTo(expElem);
else return elem.insertAfter($('span:nth-child('+(counter-1)+')', expElem));
})().slideDown(stepDuration, anim);
});
})();
});
爲了使它與懸停(包括mouseleave)一起工作有點複雜。你也可以嘗試一下存儲位置,然後將它們滑過對方,但又有點複雜。
我能想到的任何東西都是將每個字母包裝在一個容器中,以便您可以操縱他們的位置。在JS中從字符串中獲取字母實際上很簡單 - 因爲字符串是數組 - 只是執行'var theString ='ASD'; console.log(theString [2])'你會得到'S'。字符串中的字母數在'theString.length'中。 –