-
Notifications
You must be signed in to change notification settings - Fork 0
/
sliderCarousel.html
90 lines (77 loc) · 2.8 KB
/
sliderCarousel.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
85
86
87
88
89
90
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<title>Carousel Slider</title>
</head>
<body>
<style>
.image-container {
justify-content: center;
align-items: center;
position: relative;
width: 1200px;
height: auto;
overflow: hidden;
margin-left: 30em;
}
.image-container img {
display: none;
width: 100%;
height: auto;
}
.image-container img.active {
display: block;
}
</style>
<div class="image-container">
<img src="images/1.jpg" alt="Image 1">
<img src="images/2.jpg" alt="Image 2">
<img src="images/underground.jpg" alt="Image 3">
<img src="images/underground2.jpg" alt="Image 4">
<img src="images/underground3.jpg" alt="Image 5">
</div>
<script>
$(document).ready(function() {
var images = $('.image-container img');
var activeIndex = 0;
// Set initial active image
images.eq(activeIndex).addClass('active');
// Hover event for cycling images
$('.image-container').hover(
function() {
$(this).on('mousemove', function(e) {
var containerWidth = $(this).width();
var mouseX = e.pageX - $(this).offset().left;
var imageWidth = containerWidth / images.length;
// Calculate index based on mouse position
var index = Math.floor(mouseX / imageWidth);
// Update active image class
images.removeClass('active');
images.eq(index).addClass('active');
// Update active index
activeIndex = index;
});
},
function() {
// Remove mousemove event on hover out
$(this).off('mousemove');
}
);
// Keyboard event for cycling images
$(document).on('keydown', function(e) {
if (e.keyCode === 37) {
// Left arrow key
activeIndex = (activeIndex - 1 + images.length) % images.length;
} else if (e.keyCode === 39) {
// Right arrow key
activeIndex = (activeIndex + 1) % images.length;
}
// Update active image class
images.removeClass('active');
images.eq(activeIndex).addClass('active');
});
});
</script>
</body>
</html>