写一个有用JS的导航菜单和浮动菜单,和用jq写的自动播放幻灯片的网页。
时间: 2024-03-27 07:36:40 浏览: 57
自动滚动的jQuery幻灯片代码,可左右翻页,并有导航按钮
导航菜单和浮动菜单的JS代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<title>Navigation and Floating Menu</title>
<style>
nav {
background-color: #333;
overflow: hidden;
position: fixed;
top: 0;
width: 100%;
z-index: 1;
}
nav a {
color: white;
float: left;
font-size: 18px;
padding: 14px 16px;
text-align: center;
text-decoration: none;
}
nav a:hover {
background-color: #ddd;
color: black;
}
.menu {
background-color: #f1f1f1;
border: 1px solid #555;
display: none;
position: absolute;
top: 60px;
z-index: 1;
}
.menu a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.menu a:hover {
background-color: #ddd;
}
.show {
display: block;
}
</style>
</head>
<body>
<nav>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Contact</a>
<a href="#" onclick="showMenu()">Menu</a>
<div class="menu" id="menu">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</nav>
<script>
function showMenu() {
var menu = document.getElementById('menu');
if (menu.classList.contains('show')) {
menu.classList.remove('show');
} else {
menu.classList.add('show');
}
}
</script>
</body>
</html>
```
自动播放幻灯片的jQuery代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<title>Automatic Slideshow</title>
<style>
.slideshow {
position: relative;
height: 300px;
}
.slideshow img {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
max-height: 100%;
max-width: 100%;
}
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</head>
<body>
<div class="slideshow">
<img src="https://picsum.photos/800/300/?random=1" alt="Slide 1">
<img src="https://picsum.photos/800/300/?random=2" alt="Slide 2">
<img src="https://picsum.photos/800/300/?random=3" alt="Slide 3">
</div>
<script>
var slideIndex = 0;
showSlides();
function showSlides() {
var i;
var slides = $('.slideshow img');
for (i = 0; i < slides.length; i++) {
slides.eq(i).hide();
}
slideIndex++;
if (slideIndex > slides.length) {
slideIndex = 1;
}
slides.eq(slideIndex-1).fadeIn(1000);
setTimeout(showSlides, 3000);
}
</script>
</body>
</html>
```
注意,这些代码只是示例代码,需要根据具体的需求进行修改和优化。
阅读全文