這個效果在引導頁、關於我們一類的板塊裡很常見:一段文字帶著動畫進場,停留片刻後以另一種動畫退場,下一段緊接著出現,並在下方標註作者。示例封裝了一個 quoteRotator 插件來驅動整個流程。
演示效果

實現思路
做法是把所有待輪播的內容放在一個 ul 中,每個 li 用 data- 自定義屬性描述自己的行為:
data-author:作者署名;data-easein:進場動畫名;data-easeout:退場動畫名。插件依次取出每個 li,讀這些屬性決定用哪一組動畫,動畫本身則交由 animate.css 提供的關鍵幀類完成。這種「用 HTML 屬性聲明行為、由 JS 統一驅動」的模式,讓內容與效果徹底分離——換文案不用碰 JS,換動畫只需改屬性值。
完整代碼
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>jQuery制作動態文字變化輸出效果</title>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/animate.css">
<script src="js/jquery.1.9.1.min.js" type="text/javascript"></script>
<script src="js/jquery.quoterotator.min.js" type="text/javascript"></script>
<script>
jQuery(document).ready(function($) {
$('#words').quoteRotator();
});
</script>
</head>
<body>
<div id="wrapper">
<div id="words">
<ul class="word-container">
<li data-author="--- Morrie Schwartz" data-easeout="lightSpeedOut">
The most important thing in life is to learn how to give out love, and to let it come in.
</li>
<li data-author="">
所 有 的 瞬 間 都 會 淹 沒 於 時 間 的 洪 流,就 像 淚 水 消 逝 在 雨 中。
All those moments will be lost in time, like tears in rain.
</li>
<li data-author="" data-easeout="fadeOutDown">
The animation can be in random or pre-defined in the HTML. Next quote animation will be all in fadeInDown. Optional click to next quote and hover to pause the slideshow.
</li>
<li data-author="Mark Twain (1835 - 1910)" data-easein="fadeInDown">
Always do right. This will gratify some people and astonish the rest.
</li>
<li data-author="--- Susan Rice, Stanford University Commencement, 2010" data-easeout="bounceOut">
Progress is the product of human agency. Things get better because we make them better. Things go wrong when we get too comfortable, when we fail to take risks or seize opportunities.
</li>
<li data-author="--- Arlo Guthrie (1947 - )" data-easein="bounceIn">
You can't have a light without a dark to stick it in.
</li>
<li data-author="--- Mahatma Gandhi (1869 - 1948)" data-easein="lightSpeedIn">
You must not lose faith in humanity. Humanity is an ocean; if a few drops of the ocean are dirty, the ocean does not become dirty.
</li>
<li data-author="--- Abraham Lincoln (1809 - 1865), (attributed)">
When I do good, I feel good; when I do bad, I feel bad, and that is my religion.
</li>
<li data-author="--- John Wanamaker (1838 - 1922), (attributed)">
Half the money I spend on advertising is wasted; the trouble is I don't know which half.
</li>
</ul>
<div class="quote">
<blockquote>
<p class="quote-content"></p>
</blockquote>
<cite class="quote-author"></cite>
</div>
</div>
</div>
</body>
</html>
插件約定的幾個行為
從示例的屬性設計可以看出,這個 rotator 支持:隨機或按固定順序播放;通過 data-easein / data-easeout 為每一條單獨指定進退場動畫;點擊手動切換下一條;鼠標懸浮時暫停自動播放。
其中「懸浮暫停」是輪播類組件的必備能力——用戶正在閱讀時被強制切走,體驗是很糟的。
自己在項目裡實現時的建議
如果不引入第三方插件,用 jQuery 自己寫也并不難,骨架大致是這樣:
(function($){
$.fn.quoteRotator = function(options){
var settings = $.extend({
interval: 5000, // 每條停留時間
speed: 600 // 動畫時長
}, options);
return this.each(function(){
var $wrap = $(this);
var $items = $wrap.find('li');
var i = 0;
function show(){
$items.eq(i).fadeIn(settings.speed);
}
function next(){
$items.eq(i).fadeOut(settings.speed, function(){
$(this).hide();
i = (i + 1) % $items.length;
show();
});
}
var timer = setInterval(next, settings.interval);
// 懸浮暫停
$wrap.on('mouseenter', function(){ clearInterval(timer); });
$wrap.on('mouseleave', function(){ timer = setInterval(next, settings.interval); });
show();
});
};
})(jQuery);
注意末尾 return this.each(...) 保持鏈式調用,以及用 $.extend 合併默認配置——這正是 jQuery 插件的標準寫法。
在線演示地址
https://demo.jb51.net/js/2017/jquery-quoterotator-txt-cartoon-codes/
附:完整實例代碼點擊此處本站下載。
相關特效工具推薦
在線特效文字/彩色文字生成工具:
https://tools.jb51.net/aideddesign/colortext
在線彩虹文字/顏色漸變文字生成工具:
https://tools.jb51.net/aideddesign/txt_caihongzi
在線發光字生成工具:
https://tools.jb51.net/aideddesign/txt_faguangzi
仿古書排版文字豎排轉換工具:
https://tools.jb51.net/transcoding/shupai