html +css实现左边一个箭头中间内容右边一个箭头,jquery切换内容
时间: 2024-05-16 21:17:09 浏览: 11
以下是一个实现左右箭头切换内容的html + css + jquery代码示例:
```
<!DOCTYPE html>
<html>
<head>
<title>Arrow content arrow</title>
<style type="text/css">
.container {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.arrow {
width: 50px;
height: 50px;
background-color: gray;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
}
.left-arrow:before {
content: "<";
font-size: 30px;
color: white;
}
.right-arrow:before {
content: ">";
font-size: 30px;
color: white;
}
.content {
margin: 0 20px;
font-size: 24px;
}
</style>
</head>
<body>
<div class="container">
<div class="arrow left-arrow"></div>
<div class="content">Content 1</div>
<div class="arrow right-arrow"></div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script type="text/javascript">
var contents = ["Content 1", "Content 2", "Content 3"]; // 定义要切换的内容数组
var currentIndex = 0; // 初始为第一个内容
$(".left-arrow").click(function() {
currentIndex = (currentIndex - 1 + contents.length) % contents.length; // 计算上一个内容的下标
$(".content").text(contents[currentIndex]); // 切换内容
});
$(".right-arrow").click(function() {
currentIndex = (currentIndex + 1) % contents.length; // 计算下一个内容的下标
$(".content").text(contents[currentIndex]); // 切换内容
});
</script>
</body>
</html>
```
在这个示例中,我们使用了flex布局来让左箭头、内容和右箭头水平居中。左箭头和右箭头分别使用了:before伪元素来实现箭头的样式。内容部分使用了一个div元素,并设置了初始内容和字体大小。在jquery部分,我们定义了一个数组来存储要切换的内容,以及一个变量来记录当前显示的内容的下标。当点击左箭头时,我们计算出上一个内容的下标,并使用text()方法切换内容;当点击右箭头时,我们计算出下一个内容的下标,并同样使用text()方法切换内容。