Chapter 29: Learn about Event Listener with Balloon Popping Project in LWC

Hello Friends, in this chapter we will build Balloon Popping Project using Lightning Web Component.

Note: I have used addEventListener to build this project so that you have good understanding of event listeners though there are more ways to build this project.

Topics Covered :

  • DOM Event Listener
  • Code Snippet

app.html

<template>
    <div class="wrapper">
           <h1>Pop the balloons by moving</br>your mouse over them</h1>
           <div class="yay-no-balloons"><span class="blue">Wow!</span> All balloons popped!</div>
           <div class="balloon-gallery">
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
            <div class="balloon"></div>
           </div>
       </div>
   </template>

app.js

import { LightningElement } from 'lwc';

export default class App extends LightningElement {
 popped = 0;
 textmsg = 'pop';

    constructor(){
        super();
        this.template.addEventListener('mouseover',this.buttonClicked);
    }
       
    buttonClicked=(event)=>{
      if (event.target.className === "balloon"){
                event.target.style.backgroundColor = "#ededed";    
                event.target.innerHTML ='POP';    
                this.popped++
                this.checkAllPopped();
    }   

};
     checkAllPopped(){
    if (this.popped === 20){
        console.log('all popped!');
         const gallery = this.template.querySelector('.balloon-gallery');
         const message = this.template.querySelector('.yay-no-balloons');
       // gallery.innerHTML = '';
       message.style.display = 'block';
    }
    }
}


app.css

body{
    font-family:sans-serif;
    padding: 30px;
    background:#ededed;
}

.wrapper{
    max-width: 690px;
    margin: 0 auto;
}

.blue {
    color:#3f7abe;
}

h1{
    margin: auto;
    margin-top: 50px;
    margin-bottom: 50px;
    color: #08a3d9;
    text-transform: uppercase;
    font-size: 30px;
    color: #000380;
}

.balloon-gallery div{
    background: #ff3300;
    height: 121px;
    width: 119px;
    text-align: center;
    color: #ff3300;
    font-size: 40px;
    font-family: sans-serif, arial;
    border-radius: 100%;
    margin-top: 20px;
    display: inline-block;
    /* float: left; */
    margin: 2.5px 5 px 2.5px 0px;
}

.balloon-gallery div:nth-child(3n){
    background: #ffce00;
    color: #ffce00;
}

.balloon-gallery div:nth-child(2n){
    background:#3f7abe;
    color:#3f7abe;
}

.balloon-gallery div:nth-child(5n){
    background:#8e7a8e;
    color:#8e7a8e;
}

.balloon-gallery div:nth-child(13){
    background:#8e7a8e;
    color:#8e7a8e;
}

.balloon-gallery div:nth-child(10n){
    background:#ff3300;
    color: #ff3300;
}

.balloon-gallery div:nth-child(4n){
    clear:right;
}

.yay-no-balloons {
    display:none;
    color:#ff3300;
    font-size:100px;
}

Hope you enjoyed the chapter !

 

Together we can learn faster !

Join LWC study group on Telegram 

Subscribe to Youtube channel and blog to get latest updates

Did you enjoy this article?
Signup today and receive free updates straight in your inbox.
I agree to have my personal information transfered to MailChimp ( more information )
50% LikesVS
50% Dislikes