簡(jiǎn)要教程
這是一款基于segment.js制作的非常有創(chuàng)意的分段式SVG文字動(dòng)畫(huà)特效。這個(gè)文字動(dòng)畫(huà)特效通過(guò)動(dòng)畫(huà)SVG的描邊路徑來(lái)制作各種文字的動(dòng)畫(huà)效果,效果非常的贊。
這個(gè)SVG文字動(dòng)畫(huà)特效的第一個(gè)DEMO中的最后幾個(gè)例子使用了mo.js插件,一款由Oleg Solomka編寫(xiě)的用于制作網(wǎng)頁(yè)圖形動(dòng)畫(huà)的JavaScript庫(kù)插件。通過(guò)mo.js,可以制作出效果更為震撼的文字動(dòng)畫(huà)效果。
特效中使用的字體是exquisite lowercase font,一套極富創(chuàng)意的WEB字體。
使用方法
要使用該SVG文字動(dòng)畫(huà)特效,要在頁(yè)面中引入segment.js,它用于動(dòng)畫(huà)SVG路徑,d3-ease,用于制作easing動(dòng)畫(huà)過(guò)渡效果,以及l(fā)etters.js。
HTML結(jié)構(gòu)
可以使用一個(gè)
my text
初始化插件
然后我們就可以在JavaScript中獲取這個(gè)元素,通過(guò)配置參數(shù)來(lái)制作繪制文字的動(dòng)畫(huà)。所有的參數(shù)選項(xiàng)(除了individualDelays)都可以設(shè)置為以下的值:
- 單個(gè)值:可以被所有字母接收。
- 數(shù)組:數(shù)組中的第一個(gè)元素會(huì)被第一個(gè)字母接收,第二個(gè)元素被第二個(gè)字母接收,以此類推。
下面是一個(gè)使用配置參數(shù)的例子:
// 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);
通過(guò)上面的配置,我們已經(jīng)定義了文字顯示和動(dòng)畫(huà)的選項(xiàng),插件會(huì)在容器中生成SVG文字。默認(rèn)情況下,文字是被隱藏的,如何觸發(fā)文字動(dòng)畫(huà),可以參看下面的方法。
// 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);
每一個(gè)SVG字母都被分配一個(gè)letter class類,例如:letter-(a, b, c, ...),以及l(fā)etter-(1, 2, 3, ...)。通過(guò)這些class我們可以自定義字母的樣式,例如設(shè)置margin值或定位方式等。
/* Setting margin among all letters */ .letter { margin: 0 10px; } /* Setting background to letter m */ .letter-m { background-color: lightsalmon; }