“原生 JS”是指使用JavaScript语言本身提供的特性和API进行开发的编程方式,不依赖于任何第三方库或框架。以下是对“原生 JS”的详细解释:
window
、document
、Array
、Object
等。以下是一个简单的原生JS实现轮播图的例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>原生JS轮播图</title>
<style>
.carousel {
position: relative;
width: 300px;
height: 200px;
overflow: hidden;
}
.carousel img {
position: absolute;
width: 100%;
height: 100%;
opacity: 0;
transition: opacity 1s ease-in-out;
}
.carousel img.active {
opacity: 1;
}
</style>
</head>
<body>
<div class="carousel">
<img src="image1.jpg" alt="Image 1" class="active">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
</div>
<script>
const images = document.querySelectorAll('.carousel img');
let currentIndex = 0;
function showNextImage() {
images[currentIndex].classList.remove('active');
currentIndex = (currentIndex + 1) % images.length;
images[currentIndex].classList.add('active');
}
setInterval(showNextImage, 3000); // 每3秒切换一张图片
</script>
</body>
</html>
在这个例子中,我们使用了原生JS来控制图片的显示和隐藏,实现了简单的轮播图功能,没有依赖任何第三方库。
高校公开课
云+社区沙龙online第6期[开源之道]
云原生API网关直播
云+社区技术沙龙第33期
腾讯云保险行业数字化实践系列直播
一体化监控解决方案
云原生正发声
领取专属 10元无门槛券
手把手带您无忧上云