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
In the article MySQL Optimization: Cache Optimiza...
"What's wrong?" Unless you are accus...
Preface When I was typing my own personal blog, I...
When deleting a table or a piece of data in MySQL...
1. What is an index? An index is a data structure...
Table of contents 1.sleep function 2. setTimeout ...
1. AIDE AIDE (Advanced Intrusion Detection Enviro...
Preface In JavaScript, this is the function calli...
Table of contents 1. Introduction 2. Self-increme...
Problem explanation: When using the CSS animation...
1. Run the .sh file You can run it directly using...
1. Download 1. Click the latest download from the...
Table of contents 1. Register an account on Baidu...
Table of contents Preface The difference between ...
Table of contents Overview Implementation Protect...