hbuilder制作轮播图,鼠标扫过旁边的字才会换图片的代码
时间: 2024-03-24 12:38:16 浏览: 55
以下是 HBuilder 制作带有鼠标悬浮效果的轮播图的代码示例,当鼠标经过标题时,才会切换图片:
HTML 代码:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Slider</title>
<style>
.slider-container {
position: relative;
width: 600px;
height: 400px;
}
.slider-img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
transition: opacity 0.5s;
}
.slider-img.active {
opacity: 1;
}
.slider-titles {
position: absolute;
top: 350px;
left: 0;
width: 100%;
text-align: center;
}
.slider-titles a {
display: inline-block;
margin: 0 10px;
font-size: 20px;
color: #fff;
text-decoration: none;
opacity: 0.5;
transition: opacity 0.5s;
}
.slider-titles a:hover {
opacity: 1;
}
</style>
</head>
<body>
<div class="slider-container">
<img class="slider-img active" src="image1.jpg">
<img class="slider-img" src="image2.jpg">
<img class="slider-img" src="image3.jpg">
<div class="slider-titles">
<a href="#" onmouseover="changeSlide(0)">Image 1</a>
<a href="#" onmouseover="changeSlide(1)">Image 2</a>
<a href="#" onmouseover="changeSlide(2)">Image 3</a>
</div>
</div>
<script>
var slideIndex = 0;
var slides = document.getElementsByClassName("slider-img");
var titles = document.getElementsByClassName("slider-titles")[0].getElementsByTagName("a");
function changeSlide(index) {
if (index !== slideIndex) {
slides[slideIndex].className = "slider-img";
titles[slideIndex].style.opacity = 0.5;
slideIndex = index;
slides[slideIndex].className = "slider-img active";
titles[slideIndex].style.opacity = 1;
}
}
</script>
</body>
</html>
```
在这个例子中,JavaScript 代码被写在了 <script> 标签中间。首先获取了所有的图片和标题元素,然后定义了 changeSlide() 函数用于切换图片和标题的激活状态。在标题元素中,通过 onmouseover 事件调用 changeSlide() 函数,传入对应的图片索引,实现了鼠标悬浮效果。
阅读全文