flask150张图片点击第150张返回到第一张
时间: 2023-08-06 13:07:50 浏览: 50
在 Flask 中,可以使用模板语言渲染动态的 HTML 页面,这里我们可以通过 JavaScript 实现图片的轮播效果。下面是实现图片轮播效果的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>图片轮播</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<style>
.slideshow-container {
max-width: 800px;
position: relative;
margin: auto;
}
.mySlides {
display: none;
width: 100%;
height: 500px;
}
.prev, .next {
position: absolute;
top: 50%;
margin-top: -22px;
font-size: 18px;
font-weight: bold;
padding: 16px;
background-color: rgba(0, 0, 0, 0.3);
color: #fff;
cursor: pointer;
z-index: 1;
}
.prev {
left: 0;
}
.next {
right: 0;
}
</style>
</head>
<body>
<div class="slideshow-container">
{% for i in range(1, 151) %}
<div class="mySlides">
<img src="{{ url_for('static', filename='images/' + str(i) + '.jpg') }}" style="width:100%">
</div>
{% endfor %}
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<script>
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
if (n > slides.length) {
slideIndex = 1
}
if (n < 1) {
slideIndex = slides.length
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex - 1].style.display = "block";
}
</script>
</body>
</html>
```
在上面的示例中,我们使用了 JavaScript 实现了图片轮播效果。通过 showSlides() 函数来显示当前的图片,通过 plusSlides() 函数来切换图片。当图片达到最后一张时,再次点击 next 按钮时,会跳转到第一张图片,实现了点击第 150 张图片返回到第一张的效果。
注意,在 Flask 中,需要将图片放在 static 文件夹下,并使用 url_for() 函数来生成图片的 URL。例如,`url_for('static', filename='images/1.jpg')` 表示 static 文件夹下的 images 文件夹中的 1.jpg 图片。