JS realizes the case of eliminating stars

JS realizes the case of eliminating stars

This article example shares the specific code of JS to eliminate stars for your reference. The specific content is as follows

The effect of destroying stars:

Functional requirements:

1. Click on the star and it disappears.
2. Automatically generate a star every second
3. The size and position of the stars are random

Case code and analysis:

HTML and CSS code:

 * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
 
    div {
        position: relative;
        width: 1000px;
        height: 500px;
        margin: 100px auto;
        background-color: black;
    }
</style>
 
<body>
    <div>
 
    </div>
  • In the body we only need a div to hold our randomly generated stars
  • The div box needs to have a position (the random position of the stars is based on the left and top values ​​of the position)

JS code:

var div = document.querySelector('div');//Get the div box function creatImg(num) {
            for (var i = 0; i < num; i++) { //Randomly generate num stars var imgs = document.createElement('img'); //Create img tag imgs.style.position = 'absolute'; //Add absolute positioning to the star image var width = Math.floor(Math.random() * (50 - 10 + 1) + 10);
                var height = width; //Randomly generate width and height, the width and height of the stars are consistent var top = Math.floor(Math.random() * (450 - 0 + 1) + 0);
                var left = Math.floor(Math.random() * (950 - 0 + 1) + 0);
 
                //Change the width, height, left, and top values ​​of the star to randomly generated imgs.style.width = width + 'px';
                imgs.style.height = height + 'px';
                imgs.style.left = left + 'px';
                imgs.style.top = top + 'px';
 
                //Add the link of the star image to the img tag imgs.src = 'images/xingxing.gif';
            
                //Add the created img tag to the div box div.appendChild(imgs);
            }
        }

Note: When modifying properties with units such as width and left, be sure to add the units.

creatImg(5); //Call the function and generate five stars setInterval(function () { //Execute the code every 1s var img = document.querySelectorAll('img'); //Get the star image //Add a click event to each star for (var i = 0; i < img.length; i++) {
                img[i].addEventListener('click', function () {
                    //Delete the clicked img after clicking
                    div.removeChild(this);
                })
            }
            creatImg(1);
        }, 1000);

When getting the picture, you get all the star pictures in the div box, not just a single one

After the pictures are acquired, they exist in the form of a pseudo-array, so you can bind click events one by one by traversing them.

JS knowledge points used in the case: (the ones with color are used in the case)

Node Operation

Create Node

document.createElement()
document.createTextNode()
document.createTextNode()

Add Node

node.appendCild(child) (append element)
node.insertBefore (new child element, position element to be inserted)

Delete Node

node.removeChild(child) Delete a child node in the parent element

Style attribute operations

element.style

1.element.style.backgroundColor = 'red';
2. The styles in JS use camel case naming
3. JS modifies the style operation, which generates inline styles, and the CSS weight is relatively high

element.className

1. Applicable to situations with many styles or complex functions
2. className will directly change the class name of the element, overwriting the original class name
3. You can use multiple class name selectors

Timer

window.setTimeout(call function, [number of milliseconds to delay]);

1. We also call the calling function of setTimeout() as callback function
2. Window can be omitted
3. This calling function can directly write the function or function name or take the string 'character name'
4. The default value of delay in milliseconds is 0 if omitted. The unit must be milliseconds.
5. There may be many timers, and an identifier is often assigned to the timer
6. Execute only once

window.setInterval (call function, [number of milliseconds to delay]);

1. We also call the calling function of setTimeout() as callback function
2. Window can be omitted
3. This calling function can directly write the function or function name or take the string 'character name'
4. The default value of delay in milliseconds is 0 if omitted. The unit must be milliseconds.
5. There may be many timers, and an identifier is often assigned to the timer
6. Repeat

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:
  • js to eliminate stars (web simplified version)
  • JavaScript to achieve the simple version of the star elimination game
  • JS realizes the game of destroying stars

<<:  Design Theory: Textual Expression and Usability

>>:  Analysis of the difference between absolute path and relative path in HTML

Recommend

MySQL optimization connection optimization

In the article MySQL Optimization: Cache Optimiza...

Hello dialog box design experience sharing

"What's wrong?" Unless you are accus...

How to prevent users from copying web page content using pure CSS

Preface When I was typing my own personal blog, I...

Introduction to MySQL method of deleting table data with foreign key constraints

When deleting a table or a piece of data in MySQL...

Detailed Introduction to MySQL Innodb Index Mechanism

1. What is an index? An index is a data structure...

Use of JavaScript sleep function

Table of contents 1.sleep function 2. setTimeout ...

How to detect file system integrity based on AIDE in Linux

1. AIDE AIDE (Advanced Intrusion Detection Enviro...

7 interview questions about JS this, how many can you answer correctly

Preface In JavaScript, this is the function calli...

Why is the MySQL auto-increment primary key not continuous?

Table of contents 1. Introduction 2. Self-increme...

How to view and close background running programs in Linux

1. Run the .sh file You can run it directly using...

MySQL msi installation tutorial under windows10 with pictures and text

1. Download 1. Click the latest download from the...

How to use Baidu Map API in vue project

Table of contents 1. Register an account on Baidu...

Detailed explanation of the watch listener example in vue3.0

Table of contents Preface The difference between ...

JavaScript design pattern learning proxy pattern

Table of contents Overview Implementation Protect...