回答編集履歴

1

chousei

2019/11/15 05:31

投稿

yambejp
yambejp

スコア114843

test CHANGED
@@ -1 +1,169 @@
1
1
  DOMContentLoaded処理が抜けているので、jQueryオブジェクトがつかめてないのでは?
2
+
3
+
4
+
5
+
6
+
7
+ # sample
8
+
9
+ ```javascript
10
+
11
+ <style>
12
+
13
+ * {
14
+
15
+ margin: 0;
16
+
17
+ padding: 0;
18
+
19
+ }
20
+
21
+ .wrapper {
22
+
23
+ width: 350px;
24
+
25
+ }
26
+
27
+ .button_area {
28
+
29
+ background-color: #000;
30
+
31
+ height: 100px;
32
+
33
+ display: flex;
34
+
35
+ justify-content: center;
36
+
37
+ }
38
+
39
+ .button_area div {
40
+
41
+ align-self: center;
42
+
43
+ }
44
+
45
+ .button_area div + div {
46
+
47
+ margin-left: 20px;
48
+
49
+ }
50
+
51
+ .image_wrapper {
52
+
53
+ display: flex;
54
+
55
+ height: 200px;
56
+
57
+ }
58
+
59
+ .none {
60
+
61
+ display: none;
62
+
63
+ }
64
+
65
+ </style>
66
+
67
+ <script>
68
+
69
+ class Photoview {
70
+
71
+ constructor(){
72
+
73
+ document.addEventListener("DOMContentLoaded",()=>{
74
+
75
+ this.el = document.querySelector('.image_wrapper img');
76
+
77
+ this.images = [
78
+
79
+ Object.assign(new Image(),{src:'https://fakeimg.pl/350x200/ff0000/?text=1'}),
80
+
81
+ Object.assign(new Image(),{src:'https://fakeimg.pl/350x200/ff0000/000?text=2'}),
82
+
83
+ Object.assign(new Image(),{src:'https://fakeimg.pl/350x200/ff0000,128/000,255?text=3'}),
84
+
85
+ ];
86
+
87
+ this.index = 0;
88
+
89
+ this.nextBtn = document.querySelector('.next');
90
+
91
+ this.prevBtn = document.querySelector('.prev');
92
+
93
+ this.handleEvent();
94
+
95
+ this.displayImg();
96
+
97
+ });
98
+
99
+ }
100
+
101
+ handleEvent() {
102
+
103
+ this.nextBtn.addEventListener("click",()=>{
104
+
105
+ this.next();
106
+
107
+ });
108
+
109
+ this.prevBtn.addEventListener("click",()=>{
110
+
111
+ this.prev();
112
+
113
+ });
114
+
115
+ }
116
+
117
+ next() {
118
+
119
+ this.index++;
120
+
121
+ if(this.index>this.images.length-1) this.index=0;
122
+
123
+ this.displayImg();
124
+
125
+ }
126
+
127
+ prev() {
128
+
129
+ this.index--;
130
+
131
+ if(this.index<0) this.index=this.images.length-1;
132
+
133
+ this.displayImg();
134
+
135
+ }
136
+
137
+ displayImg() {
138
+
139
+ this.el.src= this.images[ this.index ].src;
140
+
141
+ }
142
+
143
+ }
144
+
145
+ const Photoview_ins = new Photoview;
146
+
147
+ </script>
148
+
149
+
150
+
151
+ <div class="wrapper">
152
+
153
+ <figure class="image_wrapper">
154
+
155
+ <img src="https://fakeimg.pl/350x200/ff0000/?text=1">
156
+
157
+ </figure>
158
+
159
+ <div class="button_area">
160
+
161
+ <div class="prev"><img src="https://fakeimg.pl/50x50/?text=prev"></div>
162
+
163
+ <div class="next"><img src="https://fakeimg.pl/50x50/?text=next"></div>
164
+
165
+ </div>
166
+
167
+ </div>
168
+
169
+ ```