写一个具有下拉式导航栏,图片轮播的html
时间: 2024-02-27 19:51:30 浏览: 126
好的,以下是一个具有下拉式导航栏和图片轮播的HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>下拉导航栏和图片轮播</title>
<style>
/* 下拉导航栏样式 */
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
z-index: 1;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #f1f1f1;
}
/* 图片轮播样式 */
.slideshow {
position: relative;
width: 100%;
height: 400px;
overflow: hidden;
}
.slideshow img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
transition: opacity 1s ease-in-out;
}
.slideshow img.active {
opacity: 1;
z-index: 1;
}
.slideshow .arrow {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 50px;
height: 50px;
background-color: #fff;
text-align: center;
cursor: pointer;
transition: background-color 0.3s ease-in-out;
}
.slideshow .arrow:hover {
background-color: #f1f1f1;
}
.slideshow .arrow.left {
left: 0;
}
.slideshow .arrow.right {
right: 0;
}
</style>
</head>
<body>
<!-- 下拉导航栏 -->
<div class="dropdown">
<a href="#">导航</a>
<div class="dropdown-content">
<a href="#">链接 1</a>
<a href="#">链接 2</a>
<a href="#">链接 3</a>
</div>
</div>
<!-- 图片轮播 -->
<div class="slideshow">
<img src="https://picsum.photos/id/1015/600/400" alt="" class="active">
<img src="https://picsum.photos/id/1016/600/400" alt="">
<img src="https://picsum.photos/id/1018/600/400" alt="">
<div class="arrow left">❮</div>
<div class="arrow right">❯</div>
</div>
<script>
let slideIndex = 0;
showSlides();
function showSlides() {
let i;
let slides = document.getElementsByClassName("slideshow")[0].getElementsByTagName("img");
for (i = 0; i < slides.length; i++) {
slides[i].classList.remove("active");
}
slideIndex++;
if (slideIndex > slides.length) {
slideIndex = 1;
}
slides[slideIndex-1].classList.add("active");
setTimeout(showSlides, 3000); // 切换时间间隔为3秒
}
let prev = document.getElementsByClassName("arrow left")[0];
let next = document.getElementsByClassName("arrow right")[0];
prev.addEventListener("click", function() {
let slides = document.getElementsByClassName("slideshow")[0].getElementsByTagName("img");
slides[slideIndex-1].classList.remove("active");
slideIndex--;
if (slideIndex < 1) {
slideIndex = slides.length;
}
slides[slideIndex-1].classList.add("active");
});
next.addEventListener("click", function() {
let slides = document.getElementsByClassName("slideshow")[0].getElementsByTagName("img");
slides[slideIndex-1].classList.remove("active");
slideIndex++;
if (slideIndex > slides.length) {
slideIndex = 1;
}
slides[slideIndex-1].classList.add("active");
});
</script>
</body>
</html>
```
你可以直接复制粘贴到HTML文件中运行查看效果。
阅读全文