This article shares the specific code for JavaScript to achieve fireworks effects for your reference. The specific content is as follows This effect uses object-oriented programming analyzeOOA
Fly Upward OOD class FireWork{ constructor(){ } bindEvent(){ let _this = this; ele.onclick = function(){ //After fly is finished, call the boom function_this.fly(_this.boom); } } fly(boom){ } boom(){ } } CSS design implementation CSS Code *{ 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 programming implementation JavaScript code <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; //Put it in the 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> Referenced utils.js file 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); } // Create an event name in the DOM object: an array corresponding to the event processing function; // Determine whether the current event processing function is in the DOM object; var event_type = "_" + event_name; if (event_type in dom) { dom[event_type].push(handler_selector); } else { dom[event_type] = [handler_selector]; } // If you use the event name directly as the key value in the object, it will conflict with the original DOM function name; // Because special event names will cause the event to fail to trigger, we need to split the event name here; dom.addEventListener(event_name.split(".")[0], handler_selector) } function off(dom, event_name) { // Get a set of event processing functions corresponding to the event name in the DOM object; var callback_list = dom["_" + event_name]; // Remove events according to all functions in the list; callback_list.forEach(function (event_handler) { dom.removeEventListener(event_name.split(".")[0], event_handler); }) // Clear the event processing function group in the DOM object; 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: There are two animation modes: "buffer" and "liner" var _style = getComputedStyle( dom ); // - 1. Data deformation; for(var attr in attrs ){ attrs[attr] = { target : attrs[attr], now : _style[attr] } // - 2. Speed: positive or negative speed of uniform motion; 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) } } // - turn off the start timer; clearInterval( dom.interval ); dom.interval = setInterval( function() { for(var attr in attrs ){ // Get the current value and attribute value; // attrs[attr].target : target value; // attrs[attr].now : current value; let { target , now } = attrs[attr]; // Buffering speed; 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 ){ // If there is data, the loop will be executed at least once; 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"; } // Assign a value to the object; attrs[attr].now = now; } } } , 30) } The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: MySQL database optimization: index implementation principle and usage analysis
>>: A brief discussion on docker compose writing rules
Table of contents 1. Interface effect preview 2.u...
Inserting Data insert into table name (column nam...
Environmental preparation: Deploy lnmp on a host ...
1. Uninstalling MySQL 5.7 1.1查看yum是否安裝過mysql cd y...
Install mysql under win10 1. Download MySQL from ...
Clicking to switch pictures is very common in lif...
1. Introduction ● Random writing will cause the h...
Generic load/write methods Manually specify optio...
The specific code of the sliding button made with...
1. Introduction to KVM The abbreviation of kernel...
1. Command Introduction The userdel (user delete)...
This article shares the specific code of js imita...
Recently, when I was working on CSS interfaces, I...
In CSS, text is one of the most common things we ...
Today we will implement a fragmented image loadin...