2013-11-01 64 views
1

当使用动画改变HTML中的某些内容时,jquery animate'移动'/将文本左右移动,而不是仅替换文本。Jquery动画移动文本

入住这里:http://jsfiddle.net/x2VYn/

function cycleIntro() { 
     var whoArr = ["colleagues", "girlfriend", "homies", "nemesis", "lover", "dad", "mom", "brother", "sister", "enemies", "friends", "classmates", "best friend", "imaginary friend", "grandma'"]; 
     var whatArr = ["pool", "backgammon", "dart", "chess", "ping pong", "bob", "headsup poker", "ludo", "yatzy", "foosball", "tennis", "street basket", "golf", "water polo"]; 

     var whotxt = whoArr[Math.floor(Math.random() * whoArr.length)]; 
     while ($("#intro #who").html() == whotxt) { 
      whotxt = whoArr[Math.floor(Math.random() * whoArr.length)]; 
     } 
     var whattxt = whatArr[Math.floor(Math.random() * whatArr.length)]; 
     while ($("#intro #what").html() == whattxt) { 
      whattxt = whatArr[Math.floor(Math.random() * whatArr.length)]; 
     } 
     $("#intro #who").animate({ width: "toggle" }, 200, function() { 
      $("#intro #who").html(whotxt).animate({ width: "toggle" }, 300, function() { 
       $("#intro #what").delay(50).animate({ width: "toggle" }, 200, function() { 
        $("#intro #what").html(whattxt).animate({ width: "toggle" }, 300, function() { 
         setTimeout(cycleIntro, 1500); 
        }); 
       }); 
      }); 
     }); 
    } 
    setTimeout(cycleIntro, 1500); 

有谁知道如何调整呢?

回答

1

设置vertical-align您操作的跨度属性bottom可修复此问题。我已经更新了小提琴here

+0

使用高度不能解决问题。 '宽度'是我希望它看起来的样子。但是,每当切换单词时都会发生错误 - 它似乎插入了某种填充/页边距,这会移动/移动文本。 – 2mas

+0

对不起!我误解了这个问题。请看我更新的答案。 – Csarsam

+0

太棒了!那肯定会有诀窍!非常感谢! – 2mas