-
Notifications
You must be signed in to change notification settings - Fork 0
/
ad-frame.html
84 lines (72 loc) · 2.11 KB
/
ad-frame.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
#slider {
margin: 0 auto;
width: 1000px;
height: 100px;
overflow: hidden;
position: relative;
}
.slides {
display: flex;
transition: left 0.5s ease-in-out;
position: absolute;
}
img {
width: 100px;
height: 100px;
}
.slide {
flex: 0 0 100px;
box-sizing: border-box;
}
</style>
</head>
<body>
<div id="slider">
<div class="slides">
<div class="slide"><img src="images/ad1.webp" alt="Image 1"></div>
<div class="slide"><img src="images/ad2.webp" alt="Image 2"></div>
<div class="slide"><img src="images/ad3.webp" alt="Image 3"></div>
<!-- Add more slides as needed -->
</div>
</div>
<button onclick="nextSlide()">Next</button>
<button onclick="previousSlide()">Previous</button>
<script>
var index=0;
function animate(obj, target) {
clearInterval(obj.timer); // 清除之前可能存在的定时器
obj.timer = setInterval(function() {
var step = (target - obj.offsetLeft) / 10; // 计算每一步移动的距离
if (obj.offsetLeft == target) {
clearInterval(obj.timer); // 到达目标位置时清除定时器
}
obj.style.left = obj.offsetLeft + step + 'px'; // 更新元素的 left 属性,实现移动
}, 30); // 定时器每隔30毫秒执行一次
}
function previousSlide() {
index--;
if (index == -4) {
document.getElementById('slider').style.left=0;
index=0;
}
var myElement = document.getElementById('slider');
animate(myElement, index*100);
}
function nextSlide() {
index++;
if(index==3){
document.getElementById('slider').style.left=0;
index=0;
}
var myElement = document.getElementById('slider'); // 获取要移动的元素
animate(myElement, index*100);
}
</script>
</body>
</html>