2017-03-05 35 views
0

我期待當你抱着你的鼠標懸停在徽標,文字變動,以重新對marieforleo.com更改文本,以隨機的答案

類似的標誌的東西。 然後當你離開時,它變回。 但每次移動時,它都會變爲不同的隨機文本。

任何想法如何創建?我用jQuery猜測

千恩萬謝

馬克

回答

0

我猜它使用的mouseenter()/鼠標離開()來調用它使用的Math.random功能選擇從文本這些文本的數組並將其設置在HTML中。

1

使用Math.Random()函數。random()方法返回從0(包含)到1(不包括)的隨機數。

HTML: -

<span id="originalText">Hello,Dhiren</span> 
<span id="newText">ExampleText</span> 

的JavaScript: -

$(document).ready(function(){ 
    var originalText = $("#originalText").text(); 
    $("#originalText").mouseenter(function() { 
     var text = ''; 
       var quotes = new Array("Softwre Engineer", "Front End Developer", "Back End developer", "Database Administrator"); 

     var randno = Math.floor (Math.random() * quotes.length); 

      text += quotes[randno]; 

     $("#newText").text(text); 
    }); 
    $("#originalText").mouseleave(function() { 
     $("#newText").text(originalText); 
    }); 
}) 

工作Example這裏..

希望它的工作! 快樂編碼!

+0

謝謝Dhiren,你知道我怎麼也給文本添加動畫,所以不是隻是出現,我希望它看起來像打字。 即文本從左側出現1個字母,然後從右側一次消失一個字母。非常感謝 - 我們可以使用 - https://github.com/mattboldt/typed.js/ –

+0

@MarkWiltshire此鏈接coud幫助你... http://codepen.io/hi-im-si/pen/DHoup –