以下是一个简单的图片放大缩小轮播的 JavaScript 源代码示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>图片放大缩小轮播</title>
<style>
#image-container {
width: 500px;
height: 400px;
overflow: hidden;
position: relative;
}
#image-container img {
width: 100%;
height: 100%;
position: absolute;
transition: all 0.5s ease;
}
</style>
</head>
<body>
<div id="image-container">
<img src="image1.jpg" alt="Image 1" id="carousel-image">
</div>
<button onclick="zoomIn()">放大</button>
<button onclick="zoomOut()">缩小</button>
<button onclick="nextImage()">下一张</button>
<button onclick="prevImage()">上一张</button>
<script>
let images = ["image1.jpg", "image2.jpg", "image3.jpg"];
let currentIndex = 0;
let scale = 1;
function updateImage() {
const image = document.getElementById('carousel-image');
image.src = images[currentIndex];
image.style.transform = `scale(${scale})`;
}
function zoomIn() {
scale += 0.1;
updateImage();
}
function zoomOut() {
if (scale > 0.1) {
scale -= 0.1;
updateImage();
}
}
function nextImage() {
currentIndex = (currentIndex + 1) % images.length;
updateImage();
}
function prevImage() {
currentIndex = (currentIndex - 1 + images.length) % images.length;
updateImage();
}
</script>
</body>
</html>
基础概念:
优势:
类型:
应用场景:
可能遇到的问题及原因:
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云