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. 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>
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 OperationCreate Node document.createElement() Add Node node.appendCild(child) (append element) Delete Node node.removeChild(child) Delete a child node in the parent element Style attribute operationselement.style 1.element.style.backgroundColor = 'red'; element.className 1. Applicable to situations with many styles or complex functions Timerwindow.setTimeout(call function, [number of milliseconds to delay]); 1. We also call the calling function of setTimeout() as callback function window.setInterval (call function, [number of milliseconds to delay]); 1. We also call the calling function of setTimeout() as callback function 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:
|
<<: Design Theory: Textual Expression and Usability
>>: Analysis of the difference between absolute path and relative path in HTML
Before talking about the structural markup of web...
This article shares the specific code for JavaScr...
MySQL replace and replace into are both frequentl...
Let's take a look at zabbix monitoring sqlser...
For databases that have been running for a long t...
MySQL is a very powerful relational database. How...
MySQL row to column operation The so-called row-t...
The warning points in this article have nothing t...
Table of contents Preface Front-end structure Bac...
JBoss uses Tomcat as the Web container, so the co...
Usage: date [options]... [+format] or: date [-u|-...
Table of contents 1 redis configuration file 2 Do...
Table of contents 1. Anti-shake 2. Throttling 3. ...
Table of contents From father to son: 1. In the s...
Preface Fix the footer area at the bottom. No mat...