簡要教程
這是一款基于segment.js制作的非常有創意的分段式SVG文字動畫特效。這個文字動畫特效通過動畫SVG的描邊路徑來制作各種文字的動畫效果,效果非常的贊。
這個SVG文字動畫特效的第一個DEMO中的最后幾個例子使用了mo.js插件,一款由Oleg Solomka編寫的用于制作網頁圖形動畫的JavaScript庫插件。通過mo.js,可以制作出效果更為震撼的文字動畫效果。
特效中使用的字體是exquisite lowercase font,一套極富創意的WEB字體。
使用方法
要使用該SVG文字動畫特效,要在頁面中引入segment.js,它用于動畫SVG路徑,d3-ease,用于制作easing動畫過渡效果,以及letters.js。
HTML結構
可以使用一個
容器來包裹需要制作動畫效果的文字。
來源:http://tympanus.net/codrops/2016/03/02/creative-svg-letter-animations/my text
初始化插件
然后我們就可以在JavaScript中獲取這個元素,通過配置參數來制作繪制文字的動畫。所有的參數選項(除了individualDelays)都可以設置為以下的值:
- 單個值:可以被所有字母接收。
- 數組:數組中的第一個元素會被第一個字母接收,第二個元素被第二個字母接收,以此類推。
下面是一個使用配置參數的例子:
// Selecting the container element var el = document.querySelector('.my-text'); // All the possible options (these are the default values) // Remember that every option (except individualDelays) can be defined as single value or array var options = { size: 100, // Font size, defined by the height of the letters (pixels) weight: 1, // Font weight (pixels) rounded: false, // Rounded letter endings color: '#5F6062', // Font color duration: 1, // Duration of the animation of each letter (seconds) delay: [0, 0.05], // Delay animation among letters (seconds) fade: 0.5, // Fade effect duration (seconds) easing: d3_ease.easeCubicInOut.ease, // Easing function individualDelays: false, // If false (default), every letter delay increase gradually, showing letters from left to right always. If you want to show letters in a disorderly way, set it to true, and define different delays for the desired letters. }; // Initializing the text (Letters parameters: container-element, options) var myText = new Letters(el, options);
通過上面的配置,我們已經定義了文字顯示和動畫的選項,插件會在容器中生成SVG文字。默認情況下,文字是被隱藏的,如何觸發文字動畫,可以參看下面的方法。
// Show letters with the default options defined myText.show(); // Hide letters with the default options defined myText.hide(); // Toggle letters with the default options defined myText.toggle(); // An example with all the possible options for triggers // Parameters (JSON): duration, delay, fade, easing, individualDelays, callback var newOptions = { duration: 2, delay: 0.2, fade: 1, easing: d3_ease.easeCircleInOut.ease, individualDelays: false, callback: function(){ myText.hide(); } }; // These new options will override the options defined in the initialization myText.show(newOptions); // Show letters instantly, without any animation at all // There is a hideInstantly and toggleInstantly function, too myText.showInstantly(); // Shortcut for myText.show(0, 0, 0);
每一個SVG字母都被分配一個letter class類,例如:letter-(a, b, c, ...),以及letter-(1, 2, 3, ...)。通過這些class我們可以自定義字母的樣式,例如設置margin值或定位方式等。
/* Setting margin among all letters */ .letter { margin: 0 10px; } /* Setting background to letter m */ .letter-m { background-color: lightsalmon; }
【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。