Home > Common Problem > body text

JavaScript special effects code collection

小老鼠
Release: 2023-12-14 09:44:44
Original
1979 people have browsed it

JavaScript is a very powerful programming language that can be used to create a variety of dynamic and interactive web page effects. The following are some common JavaScript special effect code examples:

1. Mouseover effect

html
<!DOCTYPE html>  
<html>  
<head>  
 <title>Hover Effect</title>  
 <style>  
 .hover-effect {  
 width: 100px;  
 height: 100px;  
 background-color: #f00;  
 transition: background-color 0.5s ease;  
 }  
 .hover-effect:hover {  
 background-color: #0f0;  
 }  
 </style>  
</head>  
<body>  
 <div class="hover-effect"></div>  
</body>  
</html>
Copy after login

2. Carousel effect

html
<!DOCTYPE html>  
<html>  
<head>  
 <title>Carousel Effect</title>  
 <style>  
 .carousel {  
 width: 300px;  
 height: 200px;  
 overflow: hidden;  
 position: relative;  
 }  
 .carousel img {  
 width: 100%;  
 height: auto;  
 }  
 .carousel img:first-child {  
 position: absolute;  
 left: 0;  
 top: 0;  
 }  
 .carousel img:nth-child(2) {  
 position: absolute;  
 right: 0;  
 top: 0;  
 }  
 .carousel img:nth-child(3) {  
 position: absolute;  
 left: 0;  
 bottom: 0;  
 }  
 .carousel img:nth-child(4) {  
 position: absolute;  
 right: 0;  
 bottom: 0;  
 }  
 </style>  
</head>  
<body>  
 <div class="carousel">  
 <img src="image1.jpg" alt="Image 1">  
 <img src="image2.jpg" alt="Image 2">  
 <img src="image3.jpg" alt="Image 3">  
 <img src="image4.jpg" alt="Image 4">  
 </div>  
 <script>  
 var carousel = document.querySelector('.carousel');  
 var images = carousel.querySelectorAll('img');  
 var currentIndex = 0;  
 setInterval(function() {  
 images[currentIndex].style.display = 'none';  
 currentIndex = (currentIndex + 1) % images.length;  
 images[currentIndex].style.display = 'block';  
 }, 2000); // 每2秒切换一次图片  
 </script>  
</body>  
</html>
Copy after login

The above is the detailed content of JavaScript special effects code collection. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template