如何爲HTML5中的頁面上的文本的單個字符製作動畫。就像Flash中的這個例子一樣。 http://www.greensock.com/splittextfield/如何用html5動畫字符文本
2
A
回答
3
你不得不包裹裏的每個字符<span>
然後使用CSS position
/top
/left
移動跨度。
您無法完全重現Flash示例的效果,因爲該示例使用了模糊效果。 CSS不能做到這一點; SVG可以,而IE的非標準filter
可以,但這將意味着一個完整的代碼分支。
你可以通過設置font-size
來改變每個字母的大小,但是爲了這樣剪切/旋轉效果,你必須使用CSS transform這個例子。這尚未標準化,瀏覽器支持存在漏洞。
這裏有一個驗證的概念,我只是砍死了主要針對Firefox(遺憾的碼長):
<p id="effect">I've got a lovely bunch of coconuts.</p>
<button id="animate">Animate</button>
// Add ECMA262-5 method binding if not supported natively
//
if (!('bind' in Function.prototype)) {
Function.prototype.bind= function(owner) {
var that= this;
if (arguments.length<=1) {
return function() {
return that.apply(owner, arguments);
};
} else {
var args= Array.prototype.slice.call(arguments, 1);
return function() {
return that.apply(owner, arguments.length===0? args : args.concat(Array.prototype.slice.call(arguments)));
};
}
};
}
// Lightweight class/instance system
//
Function.prototype.makeSubclass= function() {
function Class() {
if (!(this instanceof Class))
throw 'Constructor function requires new operator';
if ('_init' in this)
this._init.apply(this, arguments);
}
if (this!==Object) {
Function.prototype.makeSubclass.nonconstructor.prototype= this.prototype;
Class.prototype= new Function.prototype.makeSubclass.nonconstructor();
}
return Class;
};
Function.prototype.makeSubclass.nonconstructor= function() {};
// Abstract base for animated linear sliding switch between 0 and 1
//
var Animation= Object.makeSubclass();
Animation.prototype._init= function(period, initial) {
this.period= period;
this.interval= null;
this.aim= initial || 0;
this.t= 0;
};
Animation.prototype.set= function(aim) {
if (aim===this.aim)
return;
this.aim= aim;
var now= new Date().getTime();
if (this.interval===null) {
this.t= now;
this.interval= window.setInterval(this.update.bind(this), 32);
} else {
this.t= now-this.t-this.period+now
this.update();
}
};
Animation.prototype.toggle= function() {
this.set(this.aim===0? 1 : 0);
};
Animation.prototype.update= function() {
var now= new Date().getTime();
var x= Math.min((now-this.t)/this.period, 1);
this.show(this.aim===0? 1-x : x);
if (x===1) {
window.clearInterval(this.interval);
this.interval= null;
}
};
Animation.prototype.show= function(d) {};
// Animation that spins each character in a text node apart
//
var ExplodeAnimation= Animation.makeSubclass();
ExplodeAnimation.prototype._init= function(node, period) {
Animation.prototype._init.call(this, period, 0);
this.spans= [];
// Wrap each character in a <span>
//
for (var i= 0; i<node.data.length; i++) {
var span= document.createElement('span');
span.style.position= 'relative';
span.appendChild(document.createTextNode(node.data.charAt(i)));
node.parentNode.insertBefore(span, node);
this.spans.push(span);
}
// Make up random positions and speeds for each character.
// Possibly this should be re-randomised on each toggle?
//
this.randomness= [];
for (var i= this.spans.length; i-->0;)
this.randomness.push({
dx: Math.random()*200-100, dy: Math.random()*200-150,
sx: Math.random()*1.5, sy: Math.random()*1.5,
dr: Math.random()*240-120, og: Math.random()+0.5
});
node.parentNode.removeChild(node);
};
ExplodeAnimation.prototype.show= function(d) {
for (var i= this.spans.length; i-->0;) {
var style= this.spans[i].style;
var r= this.randomness[i];
style.left= d*r.dx+'px';
style.top= d*r.dy+'px';
var transform= 'rotate('+Math.floor(d*r.dr)+'deg) scale('+(d*r.sx+1-d)+', '+(d*r.sy+1-d)+')';
if ('transform' in style)
style.transform= transform;
else if ('MozTransform' in style)
style.MozTransform= transform;
var o= 1-Math.pow(d, r.og);
if ('opacity' in style)
style.opacity= o+'';
else if ('filter' in style)
style.filter= 'opacity(alpha='+Math.ceil(o*100)+')';
}
};
var animation= new ExplodeAnimation(document.getElementById('effect').firstChild, 1000);
document.getElementById('animate').onclick= animation.toggle.bind(animation);
這可以通過增加控制當前的重心和更好的3D空間建模得到改善完全隨機轉換,加上更好的瀏覽器支持除Firefox以外的瀏覽器中的縮放/旋轉。
(IE有其自己的非標準CSS transform filters有可能支持;的Webkit和Opera已經webkitTransform
和oTransform
風格,但他們拒絕改變相對定位內嵌跨度,所以你必須絕對位置各字符,這意味着讀取所有正常位置作爲基準線。)在這一點上我感到無聊。)
相關問題
- 1. Html5畫布文字互動?
- 2. HTML5畫布:如何動畫上下文?
- 3. HTML5動畫如何生成?
- 4. HTML5畫布字符跳轉
- 5. HTML5 Canvas基本線動畫
- 6. 如何在HTML5畫布中動畫樹
- 7. 如何在Tkinter中通過字符動畫文本?
- 8. HTML5數字時鐘動畫
- 9. HTML5畫布文本編輯
- 10. HTML5畫布 - 更新文本
- 11. HTML5畫布顏色文本
- 12. 動畫佔位符文本?
- 13. 用動畫移動文本字段
- 14. 使用html5和/或CSS3動畫文本運動
- 15. Html5畫布動畫
- 16. HTML5畫布,文字和JavaScript
- 17. HTML5畫布:透視文字
- 18. 動態HTML5畫布動畫
- 19. 如何在本機iPhone應用程序中呈現HTML5動畫?
- 20. 使用HTML5畫布文本 - SEO後果
- 21. 使用html5畫布的徑向文本
- 22. 使用HTML5 CSS3動畫使用文本到語音的音頻?
- 23. HTML5畫布 - 在畫布上拖動文本問題
- 24. 用html5畫布操縱文字
- 25. HTML5動畫
- 26. 動畫與HTML5
- 27. Html5的動畫
- 28. HTML5/CSS3動畫
- 29. 如何用html5畫布創建垂直表格標題文本?
- 30. 如何使用文本輸入在HTML5畫布中移動對象?
重新標記。問題似乎不是關於閃光。 你爲什麼只限於HTML5? – Eric 2010-05-14 22:06:43
@Eric:當然,這就是所有最酷的孩子最近都在談論的。 – 2010-05-14 22:07:15
你不知道。 CSS和JavaScript這樣做,而不是HTML。 – Rob 2010-05-15 00:10:33