您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關JavaScript如何實現煙花特效的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
OOA
點擊觸發事件
煙花運動分成兩個階段
向上飛
爆炸
OOD
class FireWork{ constructor(){ } bindEvent(){ let _this = this; ele.onclick = function(){ //fly結束再去調用boom函數 _this.fly(_this.boom); } } fly(boom){ } boom(){ } }
CSS設計實現
CSS代碼
*{ margin: 0; padding: 0; } #box{ width:800px; height:600px; position: relative; margin: 100px auto; background:#000000; border:2px solid red; overflow: hidden; } .ball{ width: 40px; height: 40px; border-radius: 50%; position: absolute; bottom: 0; }
JS編程實現
javascript代碼
<script src="./utils.js"></script> <script> // class FireWork{ constructor(){ this.box = document.getElementById("box"); this.box_offset = { left : box.offsetLeft, top : box.offsetTop } } bindEvent(){ let _this = this; this.box.onclick = function( e ){ e = e || event; _this.x = e.clientX - _this.box_offset.left; _this.y = e.clientY - _this.box_offset.top; _this.fly(_this.boom); } } fly( boom ){ let ele = this.createEle(); let _this = this; // 放入box之中; ele.style.left = this.x + "px"; let _left = this.x ; let _top = this.y ; animate( ele , { top : this.y } , function(){ ele.remove(); _this.boom( _left , _top ); }); } boom( x , y ){ let r = 100; let count = 0 ; let _this = this; for(let i = 0 ; i < 20 ; i ++){ let ele = this.createEle(); ele.style.left = x +"px"; ele.style.top = y + "px"; let _left = parseInt(Math.cos( Math.PI / 10 * i ) * r ) + x ; let _top = parseInt(Math.sin( Math.PI / 10 * i ) * r) + y animate( ele , { left : _left, top : _top, opacity : 0 } , function(){ ele.remove(); }) } } createEle(){ let ele = document.createElement("div"); ele.className = "ball"; ele.style.backgroundColor = `rgb(${parseInt(Math.random() * 255)},${parseInt(Math.random() * 255)},${parseInt(Math.random() * 255)})`; this.box.appendChild( ele ); return ele ; } } new FireWork().bindEvent(); </script>
引用的utils.js文件
function on(dom, event_name, handler_selector, delegation_handler) { if( typeof handler_selector === "string" && typeof delegation_handler === "function"){ return delegation(dom, event_name, handler_selector, delegation_handler); } // 在dom對象里面建立一個事件名 : 事件處理函數對應的數組; // 判定當前事件處理函數是否在dom對象之中; var event_type = "_" + event_name; if (event_type in dom) { dom[event_type].push(handler_selector); } else { dom[event_type] = [handler_selector]; } // 如果直接用事件名當成對象之中的key值,那么會和原有的dom功能名稱沖突; // 因為特殊的事件名會導致事件無法觸發,所以我們在這里要對事件名進行拆分處理; dom.addEventListener(event_name.split(".")[0], handler_selector) } function off(dom, event_name) { // 獲取到dom對象里面事件名對應的一組事件處理函數; var callback_list = dom["_" + event_name]; // 根據列表里面的所有函數進行事件移除 ; callback_list.forEach(function (event_handler) { dom.removeEventListener(event_name.split(".")[0], event_handler); }) // 清空dom對象里面的事件處理函數組; dom["_" + event_name].length = 0; } function trigger(dom, event_type) { dom.dispatchEvent(new Event(event_type)); } function delegation(dom, event_name, selector, event_handler) { dom.addEventListener(event_name, function (e) { e = e || event; var target = e.target || e.srcElement; while (target !== dom) { switch (selector[0]) { case ".": if (selector.slice(1) === target.className) { event_handler.call(target , e ) return; } case "#": if (selector.slice(1) === target.id) { event_handler.call(target, e) return; } default: if (selector.toUpperCase() === target.nodeName) { event_handler.call(target, e) return; } } target = target.parentNode; } }) } function animate( dom , attrs , callback , transition = "buffer", speed = 10 ){ // transition : 有兩種動畫方式 "buffer" , "liner" var _style = getComputedStyle( dom ); // - 1. 數據變形 ; for(var attr in attrs ){ attrs[attr] = { target : attrs[attr], now : _style[attr] } // - 2. 速度 : 勻速運動速度正負 ; if( transition === "liner" ){ attrs[attr].speed = attrs[attr].target > attrs[attr].now ? speed : - speed; } if( attr === "opacity"){ attrs[attr].target *= 100 attrs[attr].now *= 100 }else{ attrs[attr].now = parseInt(attrs[attr].now) } } // - 關閉開啟定時器; clearInterval( dom.interval ); dom.interval = setInterval( function(){ for(var attr in attrs ){ // 取出當前值和屬性值 ; // attrs[attr].target : 目標值; // attrs[attr].now : 當前值; let { target , now } = attrs[attr]; // 緩沖運動時候的速度; if( transition === "buffer" ){ var speed = (target - now ) / 10 ; speed = speed > 0 ? Math.ceil( speed ) : Math.floor( speed ); }else if(transition === "liner"){ var speed = attrs[attr].speed; } if( Math.abs(target - now) <= Math.abs( speed ) ){ if( attr === "opacity"){ dom.style[attr] = target / 100; }else{ dom.style[attr] = target + "px"; } delete attrs[attr]; for(var attr in attrs ){ // 如果有數據循環會執行至少一次; return false; } clearInterval(dom.interval); typeof callback === "function" ? callback() : ""; }else{ now += speed; if( attr === "opacity"){ dom.style[attr] = now / 100; }else{ dom.style[attr] = now + "px"; } // 給對象賦值; attrs[attr].now = now; } } } , 30) }
感謝各位的閱讀!關于“JavaScript如何實現煙花特效”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。