-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-slideshow.html
131 lines (109 loc) · 4.07 KB
/
simple-slideshow.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
{{ $imgs := .Page.Resources.ByType "image" }}
{{ if .Get "dir"}}
{{ $imgs = .Page.Resources.Match (.Get "dir") }}
{{ end }}
{{ $icon := resources.Get (.Get "icon" | default "svg/simple-slideshow-arrow.svg") }}
{{ $sortOrder := .Get "sortOrder" | default "asc" }}
{{ $loopImages := .Get "loopImages" | default false }}
{{ $topSlideBar := .Get "topSlideBar" | default true }}
{{ $scrollIntoView := .Get "scrollIntoView" | default true }}
{{ $maxThumbnailHeight := .Get "maxThumbnailHeight" | default 150 }}
{{ $id := now.UnixNano }}
{{ with $imgs }}
<style>
.simple-slideshow-slides-{{$id}} {
margin-bottom: 5px;
overflow-x: scroll;
white-space: nowrap;
/* works by rotating the container... */
{{ if $topSlideBar }}
transform: rotateX(180deg);
{{ end }}
}
.simple-slideshow-slide-{{$id}} {
transition: opacity .3s ease-out;
/* ...and the imgs */
{{ if $topSlideBar }}
transform: rotateX(180deg);
{{ end }}
}
.simple-slideshow-slide-{{$id}}:hover {
cursor: pointer;
opacity: .5;
}
.simple-slideshow-imgs-container-{{$id}} {
position: relative;
}
.simple-slideshow-arrow-{{$id}} {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 50px;
height: 50px;
}
.simple-slideshow-arrow-{{$id}}:hover {
cursor: pointer;
}
.right-arrow-{{$id}} {
transform: translateY(-50%) rotateZ(180deg);
right: 0;
}
.simple-slideshow-img-{{$id}} {
max-width: 100%;
margin: auto;
}
</style>
{{ $showPrevious := safeJS (print "showPrevious_" $id) }}
{{ $showNext := safeJS (print "showNext_" $id) }}
{{ $showSlide := safeJS (print "showSlide_" $id) }}
<div class="simple-slideshow-container-{{$id}}">
<div class="simple-slideshow-slides-{{$id}}">
{{ range $index, $img := sort . "Title" $sortOrder }}
{{ $thumbnail := $img.Fit (print 9999 "x" $maxThumbnailHeight) }}
<img class="simple-slideshow-slide-{{$id}}" src="{{ $thumbnail.RelPermalink }}" width="{{ $thumbnail.Width }}" height="{{ $thumbnail.Height }}" onclick="{{ $showSlide }}({{ $index }})">
{{ end }}
</div>
<div class="simple-slideshow-imgs-{{$id}}">
<div class="simple-slideshow-imgs-container-{{$id}}">
<img src="{{ $icon.RelPermalink }}" class="simple-slideshow-arrow-{{$id}}" onclick="{{ $showPrevious }}()"/>
<img src="{{ $icon.RelPermalink }}" class="simple-slideshow-arrow-{{$id}} right-arrow-{{$id}}" onclick="{{ $showNext }}()"/>
{{ range $img := . }}
<img src="{{ $img.RelPermalink }}" style="display: none; aspect-ratio: {{ $img.Width }} / {{ $img.Height }};" class="simple-slideshow-img-{{$id}}" loading="lazy">
{{ end }}
</div>
</div>
</div>
{{ $imgs := safeJS (print "img_" $id) }}
{{ $curIndex := safeJS (print "curIndex_" $id) }}
<script>
const {{ $imgs }} = Array.from(document.getElementsByClassName("simple-slideshow-img-{{$id}}"))
.sort((a,b) => {{ $sortOrder }} === 'asc' ? a.src > b.src : a.src < b.src);
let {{ $curIndex }} = 0;
{{ $imgs }}[0].style.display = "block";
function {{ $showPrevious }}() {
{{ $showSlide }}({{ $curIndex }} - 1);
}
function {{ $showNext }}() {
{{ $showSlide }}({{ $curIndex }} + 1);
}
function {{ $showSlide }}(n) {
{{ if $loopImages }}
if (n < 0)
n = {{ $imgs }}.length - 1;
else if (n > {{ $imgs }}.length -1)
n = 0;
{{ else }}
if (n < 0 || n > {{ $imgs }}.length-1)
return;
{{ end }}
{{ $imgs }}[{{ $curIndex }}].style.display = "none";
{{ $imgs }}[n].style.display = "block";
{{ $curIndex }} = n;
{{ if $scrollIntoView }}
setTimeout( // eliminates the time between waiting for img and scrolling.
() => {{ $imgs }}[{{ $curIndex }}].scrollIntoView({ behavior: "smooth", block: "center" }),
150);
{{ end }}
}
</script>
{{ end }}