基于JavaScript實(shí)現(xiàn)粒子流動(dòng)效果
這是一個(gè)HTML文件,主要包含了一些CSS樣式和JavaScript代碼,用于創(chuàng)建一個(gè)動(dòng)畫(huà)效果。
在CSS部分,定義了一些基本的樣式,包括頁(yè)面的背景顏色、位置、大小等。特別的,定義了兩種球形元素(.ball_A 和 .ball_B)的樣式,以及它們的動(dòng)畫(huà)效果。
在JavaScript部分,定義了一個(gè)名為Ball的類(lèi),用于創(chuàng)建球形元素,并控制它們的運(yùn)動(dòng)。Ball類(lèi)有一些方法,如random用于生成隨機(jī)數(shù),render用于渲染球形元素,update用于更新球形元素的位置。
在頁(yè)面加載時(shí),會(huì)調(diào)用creatBall函數(shù)創(chuàng)建一系列的球形元素,并將它們添加到頁(yè)面中。然后,調(diào)用render函數(shù),使這些球形元素按照預(yù)定的動(dòng)畫(huà)效果進(jìn)行運(yùn)動(dòng)。
總的來(lái)說(shuō),這個(gè)HTML文件的主要功能是創(chuàng)建一個(gè)動(dòng)畫(huà)效果,其中包含一系列的球形元素在頁(yè)面上進(jìn)行運(yùn)動(dòng)。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> body,html{ width: 100%; height: 100%; background: #222; overflow: hidden; position: relative; --m-x:51px; --m-y:51px } .playground{ position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); width: 600px; height: 300px; display: flex; justify-content: space-around; align-items: center; } .ball{ width: 100px; height: 100px; border-radius: 50%; /*border: 1px solid #FFFFFF;*/ position: relative; } .ball_A{ /*border: 1px solid #a101f6;*/ color: #FFFFFF; background: #a101f6; cursor: pointer; animation: scaleBall 0.5s forwards; } .ball_B{ /*border: 1px solid #FFFFFF;*/ color: #FFFFFF; } .ball_B1{ border: none; width: 20px; height: 20px; background:#0d84ff; transform: scale(0); left: -20px; border-radius: 10px 30px 20px 20px / 15px 10px 25px 25px; animation: transBall 0.8s 1.2s forwards; } .text{ display: inline-block; width: 100px; line-height: 100px; color: white; text-align: center; } @keyframes scaleBall { 0%{ transform: scale(1.0); } 100%{ transform: scale(1.3); background: none; /*border: 1px solid #fff;*/ } } .small-ball{ width: 10px; height: 10px; background: #0d84ff; border-radius: 50%; position: absolute; /*animation: moveBall 0.5s forwards;*/ } @keyframes transBall { 0%{ transform: scale(0); border-radius: 5px 10px 15px 5px / 8px 7px 6px 15px; } 50%{ border-radius: 10px 30px 20px 20px / 15px 10px 25px 25px; } 100%{ transform: scale(6); border-radius: 50px } } </style> </head> <body> <div class="playground"> <div class="ball ball_A"> <span class="text">A</span> </div> <div class="ball ball_B"> <span class="text">B</span> </div> </div> <div class="playground"> <div class="ball" style="opacity: 0"> <span class="text">a</span> </div> <div class="ball ball_B1"> <!-- <span class="text">B</span>--> </div> </div> <script> const playground = document.querySelector('.playground') const ctx = document.querySelector('.ball_A') const ctx_b = document.querySelector('.ball_B') const play = playground.getBoundingClientRect() const rect = ctx.getBoundingClientRect() const rect_b = ctx_b.getBoundingClientRect() const list = [] const pox = { y: rect.height, x: rect.width, bx: rect_b.left - play.left, by: rect_b.top - play.top } class Ball{ constructor(con,x,y) { this.x = x; this.y = y; this.width = con.x; this.height = con.y; this.ex = this.random(-20,con.x) this.ey = this.random(-20,con.y) this.dx = this.random(-5, 6); // -5~5 this.dy = this.random(-5, 6); // -5~5 this.dom = '' } random(min,max){ return Math.random()* (max - min) + min; } render(index,step){ const move = `@keyframes moveBall_${index} { 0%{ top:${this.y}px; left: ${this.x}px; } 50%{ top:${this.ey}px; left: ${this.ex}px; } 100%{ top:${this.y}px; left: ${pox.bx - 100}px; } }` const sheet = document.styleSheets[0]; sheet.insertRule(move, 0) const div = document.createElement("div") div.className = 'small-ball' div.style.transform = `scale(${Math.random() + 0.5})` div.style.opacity = Math.random() + 0.5 div.style.animation = `moveBall_${index} ${step}s cubic-bezier(0.23, 1, 0.32, 1) forwards` ctx.appendChild(div) this.dom = div } } creatBall() function creatBall(){ let step = 2 const x = pox.x / 2 const y = pox.y / 2 for (let i = 0; i< 50; i++){ step += 0.01 const ball = new Ball(pox,x,y) ball.render(i,step) list.push(ball) } } </script> </body> </html>
到此這篇關(guān)于基于JavaScript實(shí)現(xiàn)粒子流動(dòng)效果的文章就介紹到這了,更多相關(guān)JavaScript粒子流動(dòng)效果內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
原生javascript實(shí)現(xiàn)獲取指定元素下所有后代元素的方法
這篇文章主要介紹了原生javascript實(shí)現(xiàn)獲取指定元素下所有后代元素的方法,在進(jìn)行web程序設(shè)計(jì)時(shí)是非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10javascript實(shí)現(xiàn)發(fā)送短信倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)發(fā)送短信倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09使用controller接收js傳過(guò)來(lái)的參數(shù)問(wèn)題
這篇文章主要介紹了使用controller接收js傳過(guò)來(lái)的參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02JavaScript 跨域之POST實(shí)現(xiàn)方法
本篇文章主要介紹了JavaScript 跨域之POST實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05javascript實(shí)現(xiàn)獲取瀏覽器版本、瀏覽器類(lèi)型
這篇文章主要介紹了javascript實(shí)現(xiàn)獲取瀏覽器版本,javascript實(shí)現(xiàn)獲取瀏覽器類(lèi)型兩大方面,對(duì)這方面感興趣的朋友可以參考一下2015-12-1210 種最常見(jiàn)的 Javascript 錯(cuò)誤(頻率最高)
本文是小編給大家收藏的JavaScript 中頻度最高的 10 種錯(cuò)誤,我們會(huì)告訴你什么原因?qū)е铝诉@些錯(cuò)誤,以及如何防止這些錯(cuò)誤發(fā)生。需要的朋友參考下2018-02-02