html摩天轮小案例
以下是一个简单的HTML和CSS代码示例,用于创建一个摩天轮小案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Carousel Example</title>
<style>
.carousel {
position: relative;
width: 100%;
height: 230px;
margin: auto;
overflow: hidden;
}
.carousel-inner {
position: absolute;
width: 300%;
transition: all .5s ease;
}
.carousel-inner div {
float: left;
width: 33.33%;
height: 230px;
}
.carousel-control {
position: absolute;
top: 50%;
width: 20px;
height: 60px;
margin-top: -30px;
background-color: rgba(0, 0, 0, 0.2);
text-align: center;
line-height: 60px;
color: white;
font-size: 30px;
cursor: pointer;
}
.carousel-control.left {
left: 0;
}
.carousel-control.right {
right: 0;
}
</style>
</head>
<body>
<div class="carousel">
<div class="carousel-inner" style="left: -33.33%;">
<div><img src="image1.jpg" alt="Image 1"></div>
<div><img src="image2.jpg" alt="Image 2"></div>
<div><img src="image3.jpg" alt="Image 3"></div>
</div>
<div class="carousel-control left">❮</div>
<div class="carousel-control right">❯</div>
</div>
<script>
const carousel = document.querySelector('.carousel');
const inner = document.querySelector('.carousel-inner');
const leftControl = document.querySelector('.carousel-control.left');
const rightControl = document.querySelector('.carousel-control.right');
let slideCount = Math.floor(inner.offsetWidth / carousel.offsetWidth);
let slidePosition = 0;
rightControl.addEventListener('click', function() {
if (slidePosition > -slideCount + 1) {
slidePosition -= 1;
inner.style.left = slidePosition * 100 + '%';
}
});
leftControl.addEventListener('click', function() {
if (slidePosition < 0) {
slidePosition += 1;
inner.style.left = slidePosition * 100 + '%';
}
});
</script>
</body>
</html>
这段代码实现了一个简单的摩天轮效果,用户可以通过点击左右控制按钮来切换图片。这个例子使用了基本的HTML结构,内联CSS样式来控制布局和视觉效果,以及JavaScript来处理切换逻辑。这个示例可以作为学习如何创建简单动画轮播图的起点。
评论已关闭