JavaScript實現(xiàn)Flash炫光波動特效
更新時間:2015年05月14日 10:01:39 投稿:hebedich
JavaScript寫的炫光波動效果,看到一些Flash效果不錯,用JS也模擬一下,還有很多不完善的地方,給各位參考參考。
看到flash的實現(xiàn)這類的動畫非常的便捷,于是試圖胡搞一下。全部是用dom模擬的像素點,鋸齒是難免的……
這個要避免鋸齒恐怕要再加一次濾鏡了吧,或者用圖片。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>炫光波動效果</title>
<script>
var lightWave = function(T,left,thick,sharp,speed,vibration,amplitude,opacity){
this.cont = T;//炫光容器
this.left = left;//炫光向右偏移量
this.thick = thick;//粗細
this.sharp = sharp;//尖銳度
this.speed = speed;//波動速度
this.vibration = vibration;//單位時間內(nèi)的振動頻率
this.amplitude = amplitude;//振幅
this.opacity = opacity;//透明度
this.cont.style.position = 'relative';
this.move();
}
lightWave.prototype = {
point:function(n,l,t,c,color){
var p = document.createElement('p');
p.innerHTML = ' ';
p.style.top = t + 'px';
p.style.left = l + 'px';
p.style.width = 1 + 'px';
p.style.height = n + 'px';
p.style.filter = 'alpha(opacity='+this.opacity+')';
p.style.lineHeight = 0;
p.style.position = 'absolute';
p.style.background = color;
c.appendChild(p);
return this;
},
color:function(){
var c = ['0','3','6','9','c','f'];
var t = [c[Math.floor(Math.random()*100)%6],'0','f'];
t.sort(function(){return Math.random()>0.5?-1:1;});
return '#'+t.join('');
},
wave:function(){
var l = this.left,t = this.wavelength,color = this.color();
var c = document.createElement('div');
c.style.top = this.amplitude+20+'px';
c.style.position = 'absolute';
c.style.opacity = this.opacity/100;
for(var i=1;i<this.thick;i++){
for(var j=0;j<this.thick*this.sharp-i*i;j++,l++){
this.point(i,l,-9999,c,color);
}
}
for(var i=this.thick;i>0;i--){
for(var j=this.thick*this.sharp-i*i;j>0;j--,l++){
this.point(i,l,-9999,c,color);
}
}
this.cont.appendChild(c);
return c;
},
move:function(){
var wl = this.amplitude;
var vibration = this.vibration;
var w = this.wave().getElementsByTagName('p');
for(var i=0;i<w.length;i++){
w[i].i = i;
}
var m = function(){
for(var i=0,len=w.length;i<len;i++){
if(w[i].ori == true){
w[i].i-=vibration;
var top = w[i].i%180==90?0:wl*Math.cos(w[i].i*Math.PI/180);
w[i].style.top = top+'px';
if(parseFloat(w[i].style.top)<=-wl){
w[i].ori = false;
}
}else{
w[i].i+=vibration;
var top = w[i].i%180==90?0:wl*Math.cos(w[i].i*Math.PI/180);
w[i].style.top = top+'px';
if(parseFloat(w[i].style.top)>=wl){
w[i].ori = true;
}
}
}
}
setInterval(m,this.speed);
}
}
window.onload = function(){
var targetDom = document.body;
new lightWave(targetDom,0,3,36,120,6,20,40);
new lightWave(targetDom,50,2,70,120,10,30,30);
}
</script>
</head>
<body style="background:#000;margin-top:100px">
</body>
</html>
參數(shù):
var lightWave = function (T,left,thick,sharp,speed,vibration,amplitude,opacity){
this .cont = T; //需要添加炫光的容器
this .left = left; //炫光出生時的向右偏移量
this .thick = thick; //粗細程度
this .sharp = sharp; //尖銳程度
this .speed = speed; //波動速度
this.vibration = vibration; //單位時間內(nèi)的振動頻率
this .amplitude = amplitude; //振幅
this .opacity = opacity; //透明度
this .cont.style.position = 'relative';
this .move();
}
大家感興趣可以來討論一下。
另外,還遇到個問題,上面代碼中ie下面的透明度濾鏡不起作用,經(jīng)研究得知,改變父容器的定位會影響子節(jié)點的透明濾鏡的繼承。
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關文章
JS事件Event元素(兼容IE,Firefox,Chorme)
今天,想聊聊JS事件對象。相信大家對于獲得激發(fā)JS事件的原對象的理解,有的人可能簡單停留在IE上。也就是window.event這個對象2012-11-11
UniApp與WebView雙向通信及數(shù)據(jù)傳輸超詳細講解
這篇文章主要介紹了UniApp與WebView雙向通信及數(shù)據(jù)傳輸?shù)南嚓P資料,詳細講解了UniApp與WebView的通信原理、方法對比、數(shù)據(jù)傳輸實戰(zhàn)、調(diào)試技巧、性能優(yōu)化策略及技術風險控制,通過合理選型和優(yōu)化,需要的朋友可以參考下2025-04-04
JavaScript動態(tài)插入script的基本思路及實現(xiàn)函數(shù)
偶爾需要動態(tài)插入javascript代碼的需求,基本思路是動態(tài)創(chuàng)建一個script標簽,設置其src屬性,type屬性等,需要的朋友可以參考下2013-11-11
JavaScript中三種非破壞性處理數(shù)組的方法比較
在這篇文章中,我們將會探索處理數(shù)組的三種方法:for…of循環(huán)、數(shù)組方法.reduce()和數(shù)組方法.flatMap(),文中的示例代碼講解詳細,感興趣的可以了解一下2023-06-06

