使用HBuilder写出焦点图代码
时间: 2024-06-23 21:00:49 浏览: 97
在HBuilder中编写焦点图(轮播图或滑动图片)的代码,通常会使用HTML、CSS和JavaScript或者HBuilder提供的UI组件库。以下是一个简单的示例,展示如何使用HBuilder的Webview组件来创建一个焦点图:
```html
<!DOCTYPE html>
<html>
<head>
<style>
.carousel {
position: relative;
width: 100%;
height: 400px; /* 根据需求调整高度 */
overflow: hidden;
}
.carousel-items {
display: flex;
width: auto;
transition: transform 0.6s ease;
}
.carousel-item {
flex: 0 0 auto;
width: 100%;
height: 100%;
background-size: cover;
background-position: center;
cursor: pointer;
}
.carousel-item.active {
transform: translateX(0);
}
.carousel-controls {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
}
</style>
</head>
<body>
<div id="carousel" class="carousel">
<div class="carousel-items">
<!-- 在这里添加你的焦点图图片,每张图片对应一个carousel-item -->
<div class="carousel-item active" style="background-image: url('image1.jpg')"></div>
<div class="carousel-item" style="background-image: url('image2.jpg')"></div>
<div class="carousel-item" style="background-image: url('image3.jpg')"></div>
<!-- 更多图片... -->
</div>
<div class="carousel-controls">
<button onclick="prevSlide()">上一张</button>
<button onclick="nextSlide()">下一张</button>
</div>
</div>
<script>
let currentIndex = 0;
const items = document.querySelectorAll('.carousel-item');
function prevSlide() {
if (currentIndex > 0) {
items[currentIndex].classList.remove('active');
currentIndex--;
items[currentIndex].classList.add('active');
}
}
function nextSlide() {
if (currentIndex < items.length - 1) {
items[currentIndex].classList.remove('active');
currentIndex++;
items[currentIndex].classList.add('active');
}
}
</script>
</body>
</html>
```
阅读全文