以下是 jQuery+CSS3文字动画插件Morphext代码 的示例演示效果:
部分效果截图:
HTML代码(index.html):
<!doctype html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery文字动画插件Morphext</title>
<link rel="stylesheet" type="text/css" href="css/default.css">
<link rel="stylesheet" href="css/animate.min.css">
<style type="text/css">
.morphext > .animated {
display: inline-block;
}
.content h3{font-weight: 300}
</style>
<!--[if IE]>
<script src="http://libs.useso.com/js/html5shiv/3.7/html5shiv.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="content bgcolor-8">
<h3 class="center">Morphext <span id="js-rotating1">So Simple, Very Doge, Much Wow, Such Cool</span> </h3>
<h3 class="center">Photoshop是 <span id="js-rotating2">图像处理,作品设计,制作高质量图片</span> 的常用图像软件。</h3>
<h3 class="center">你了解Photoshop中的 <span id="js-rotating3">通道的概念,通道于曲线,通道与色阶,通道与蒙版,通道与应用图像</span> 等命令吗?</h3>
</div>
</div>
<script src="js/jquery-1.8.3.min.js"></script>
<script src="js/morphext.min.js"></script>
<script>
$("#js-rotating1").Morphext({
animation: "bounceOut"
});
$("#js-rotating2").Morphext({
animation: "flip"
});
$("#js-rotating3").Morphext({
animation: "lightSpeedIn"
});
</script>
</body>
</html>
JS代码(morphext.js):
/*! * Morphext - Text Rotating Plugin for jQuery * https://github.com/MrSaints/Morphext * * Built on jQuery Boilerplate * http://jqueryboilerplate.com/ * * Copyright 2014 Ian Lai and other contributors * Released under the MIT license * http://ian.mit-license.org/ */
/*eslint-env browser */
/*global jQuery:false */
/*eslint-disable no-underscore-dangle */
(function ($){
"use strict";
var pluginName = "Morphext",defaults ={
animation:"bounceIn",separator:",",speed:2000,complete:$.noop}
;
function Plugin (element,options){
this.element = $(element);
this.settings = $.extend({
}
,defaults,options);
this._defaults = defaults;
this._init();
}
Plugin.prototype ={
_init:function (){
var $that = this;
this.phrases = [];
this.element.addClass("morphext");
$.each(this.element.text().split(this.settings.separator),function (key,value){
$that.phrases.push(value.trim());
}
);
this.index = -1;
this.animate();
this.start();
}
,animate:function (){
if ((this.index + 1) === this.phrases.length){
this.index = -1;
}
++this.index;
this.element[0].innerHTML = "<span class=\"animated " + this.settings.animation + "\">" + this.phrases[this.index] + "</span>";
if ($.isFunction(this.settings.complete)){
this.settings.complete.call(this);
}
}
,start:function (){
var $that = this;
this._interval = setInterval(function (){
$that.animate();
}
,this.settings.speed);
}
,stop:function (){
this._interval = clearInterval(this._interval);
}
}
;
$.fn[pluginName] = function (options){
return this.each(function(){
if (!$.data(this,"plugin_" + pluginName)){
$.data(this,"plugin_" + pluginName,new Plugin(this,options));
}
}
);
}
;
}
)(jQuery);
CSS代码(morphext.css):
.morphext > .animated{display:inline-block;}