回答編集履歴
8
add solution to background changing
answer
CHANGED
@@ -78,4 +78,132 @@
|
|
78
78
|
background-position: center center;
|
79
79
|
background-size: cover;
|
80
80
|
}
|
81
|
-
```
|
81
|
+
```
|
82
|
+
|
83
|
+
---
|
84
|
+
|
85
|
+
追記:左側で写真のスライドみたいなのを作りたいとコメントいただいたので、CSS Transition で簡単な実装を追記します。
|
86
|
+
|
87
|
+
まずは ul を捨てて、一つの a だけ残してください。
|
88
|
+
|
89
|
+
```html
|
90
|
+
<div class="main_l">
|
91
|
+
<a role="img"></a>
|
92
|
+
</div>
|
93
|
+
```
|
94
|
+
|
95
|
+
a に main_l を満たすための CSS:
|
96
|
+
|
97
|
+
```css
|
98
|
+
.main_l { display: block; }
|
99
|
+
.main_l a {
|
100
|
+
display: block;
|
101
|
+
width: 100%;
|
102
|
+
height: 100%;
|
103
|
+
}
|
104
|
+
```
|
105
|
+
|
106
|
+
a の background を設定します。background-image を後で JavaScript に設定します。
|
107
|
+
|
108
|
+
```css
|
109
|
+
.main_l a {
|
110
|
+
background-position: center center;
|
111
|
+
background-size: cover;
|
112
|
+
}
|
113
|
+
```
|
114
|
+
|
115
|
+
JavaScript について、まずは設定を説明しましょう:
|
116
|
+
|
117
|
+
```javascript
|
118
|
+
let config = {
|
119
|
+
selector: ".main_l a",
|
120
|
+
bgUrls: [
|
121
|
+
"https://placehold.jp/1000x1000.png?text=Slide%201",
|
122
|
+
"https://placehold.jp/1000x1000.png?text=Slide%202",
|
123
|
+
"https://placehold.jp/1000x1000.png?text=Slide%203",
|
124
|
+
"https://placehold.jp/1000x1000.png?text=Slide%204"
|
125
|
+
],
|
126
|
+
fadeDelay: 1,
|
127
|
+
interval: 5,
|
128
|
+
onTransition: currentUrl => {
|
129
|
+
let mainL = document.querySelector(".main_l a");
|
130
|
+
mainL.setAttribute(
|
131
|
+
"href",
|
132
|
+
`#anchor_${currentUrl.substring(currentUrl.length - 1)}`
|
133
|
+
);
|
134
|
+
mainL.setAttribute(
|
135
|
+
"aria-label",
|
136
|
+
`Slide ${currentUrl.substring(currentUrl.length - 1)}`
|
137
|
+
);
|
138
|
+
}
|
139
|
+
};
|
140
|
+
```
|
141
|
+
|
142
|
+
selector はスライドになる要素の CSS 選択符です。複数の要素が見つかる場合、最初の要素を使います。
|
143
|
+
|
144
|
+
bgUrls は背景画像の URL の Array です。
|
145
|
+
|
146
|
+
fadeDelay は画像の切り替えのとき fade 動画の長さです。単位は秒。
|
147
|
+
|
148
|
+
interval は画像の切り替えの間隔時間です。単位は秒。
|
149
|
+
|
150
|
+
onTransition は毎回画像切り替えの際に実行したい関数です。関連のため、切り替え後表示する画像の URL を引数で渡します。これで切り替えごとURLによってリンクを変えます。
|
151
|
+
|
152
|
+
実装は下記のようになります:
|
153
|
+
|
154
|
+
```javascript
|
155
|
+
(function(config) {
|
156
|
+
// background を変える要素を選択
|
157
|
+
let mainL = document.querySelector(config.selector);
|
158
|
+
|
159
|
+
// background 画像を事前ロード
|
160
|
+
// じゃないと第一回コードする際 fade-in 動画見えない
|
161
|
+
let bgUrls = config.bgUrls;
|
162
|
+
function preloadImgUrls(urlArr) {
|
163
|
+
for (let url of urlArr) {
|
164
|
+
let img = new Image();
|
165
|
+
img.src = url;
|
166
|
+
}
|
167
|
+
}
|
168
|
+
preloadImgUrls(bgUrls);
|
169
|
+
|
170
|
+
// 最初の background を設定、今の background への index を記録
|
171
|
+
// 次の background 循環で取得
|
172
|
+
let currentBg = 0;
|
173
|
+
mainL.style.backgroundImage = `url(${bgUrls[currentBg]})`;
|
174
|
+
function getNextBgUrl() {
|
175
|
+
currentBg = (currentBg + 1) % bgUrls.length;
|
176
|
+
return bgUrls[currentBg];
|
177
|
+
}
|
178
|
+
|
179
|
+
// fade 動画を設定
|
180
|
+
let fadeDelay = config.fadeDelay;
|
181
|
+
mainL.style.transition = `opacity ${fadeDelay}s ease-in-out`;
|
182
|
+
async function waitForMs(ms) {
|
183
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
184
|
+
}
|
185
|
+
async function fadeOut() {
|
186
|
+
mainL.style.opacity = 0;
|
187
|
+
await waitForMs(fadeDelay * 1000);
|
188
|
+
}
|
189
|
+
async function fadeIn() {
|
190
|
+
await waitForMs(20); // fix animation fade in
|
191
|
+
// ここまたないと fade-in 動画たまに見えない
|
192
|
+
mainL.style.opacity = "";
|
193
|
+
await waitForMs(fadeDelay * 1000);
|
194
|
+
}
|
195
|
+
|
196
|
+
// 背景画像の循環を起動
|
197
|
+
let onTransition = config.onTransition;
|
198
|
+
onTransition(bgUrls[currentBg]);
|
199
|
+
setInterval(async () => {
|
200
|
+
await fadeOut();
|
201
|
+
let url = getNextBgUrl();
|
202
|
+
mainL.style.backgroundImage = `url(${url})`;
|
203
|
+
onTransition(url);
|
204
|
+
await fadeIn();
|
205
|
+
}, config.interval * 1000);
|
206
|
+
})(config);
|
207
|
+
```
|
208
|
+
|
209
|
+
[Demo](https://codesandbox.io/s/peaceful-khorana-z2d25?file=/index.js) で実装の様子を見れます。
|
7
use background
answer
CHANGED
@@ -50,4 +50,32 @@
|
|
50
50
|
|
51
51
|
```css
|
52
52
|
.main_l img { width: 100vw; }
|
53
|
+
```
|
54
|
+
|
55
|
+
---
|
56
|
+
|
57
|
+
追記:完璧に余白ないようにするには、img 要素ではなく、background を使います。目が不自由な方のために、a 要素に role="img" をつけて、aria-label="説明文" もつけます。
|
58
|
+
|
59
|
+
```html
|
60
|
+
<ul>
|
61
|
+
<li style="background-image: url(images/bg01.jpg)">
|
62
|
+
<a href="XXX" role="img" aria-label="画像1"></a>
|
63
|
+
</li>
|
64
|
+
<li style="background-image: url(images/bg2.jpg)">
|
65
|
+
<a href="XXX" role="img" aria-label="画像2"></a>
|
66
|
+
</li>
|
67
|
+
<li style="background-image: url(images/bg03.jpg)">
|
68
|
+
<a href="XXX" role="img" aria-label="画像3"></a>
|
69
|
+
</li>
|
70
|
+
<li style="background-image: url(images/bg04.jpg)">
|
71
|
+
<a href="XXX" role="img" aria-label="画像4"></a>
|
72
|
+
</li>
|
73
|
+
</ul>
|
74
|
+
```
|
75
|
+
|
76
|
+
```css
|
77
|
+
.main_l li {
|
78
|
+
background-position: center center;
|
79
|
+
background-size: cover;
|
80
|
+
}
|
53
81
|
```
|
6
fix width
answer
CHANGED
@@ -42,4 +42,12 @@
|
|
42
42
|
|
43
43
|
```css
|
44
44
|
.main_l { flex-direction: column; }
|
45
|
+
```
|
46
|
+
|
47
|
+
---
|
48
|
+
|
49
|
+
追記:コメントの実例を改めてみたら、その効果は画像の width を 100vw に固定したものです。下記のようで設定できます。
|
50
|
+
|
51
|
+
```css
|
52
|
+
.main_l img { width: 100vw; }
|
45
53
|
```
|
5
add change flex-direction
answer
CHANGED
@@ -34,4 +34,12 @@
|
|
34
34
|
|
35
35
|
```css
|
36
36
|
.main_l_box1 { height: 100vh; }
|
37
|
+
```
|
38
|
+
|
39
|
+
---
|
40
|
+
|
41
|
+
追記:ひたすら 100vh 設定したら左側他の調整が難しくなりそうなので、下記のようでは内容の高さが固定されなくても左側を満たします。flex の方向を縦にします。画像の高さが十分であれば上下に余白がなくなれます。けど、ウィンドが短くなると、左右に余白が出るので、画像に min-width を設定してください。
|
42
|
+
|
43
|
+
```css
|
44
|
+
.main_l { flex-direction: column; }
|
37
45
|
```
|
4
leave no space above and below
answer
CHANGED
@@ -26,4 +26,12 @@
|
|
26
26
|
justify-content: center;
|
27
27
|
align-items: center;
|
28
28
|
}
|
29
|
+
```
|
30
|
+
|
31
|
+
---
|
32
|
+
|
33
|
+
追記:上下の余白をなくしたいと依頼されました:
|
34
|
+
|
35
|
+
```css
|
36
|
+
.main_l_box1 { height: 100vh; }
|
29
37
|
```
|
3
fix width
answer
CHANGED
@@ -21,6 +21,7 @@
|
|
21
21
|
|
22
22
|
```css
|
23
23
|
.main_l {
|
24
|
+
overflow: hidden; /* width を内容に影響されないように */
|
24
25
|
display: flex;
|
25
26
|
justify-content: center;
|
26
27
|
align-items: center;
|
2
縦で中央固定追加
answer
CHANGED
@@ -13,4 +13,16 @@
|
|
13
13
|
|
14
14
|
---
|
15
15
|
|
16
|
-
追記:`overflow: scroll`使うと上下だけではなく左右スクロールバーも出そうです。上下だけでほしいなら、`overflow-y`を使ってください。左右だけでほしいなら、`overflow-x`を使ってください。
|
16
|
+
追記:`overflow: scroll`使うと上下だけではなく左右スクロールバーも出そうです。上下だけでほしいなら、`overflow-y`を使ってください。左右だけでほしいなら、`overflow-x`を使ってください。
|
17
|
+
|
18
|
+
---
|
19
|
+
|
20
|
+
追記:`.main_l` の中の画像を中央に固定したい場合はこうしてください:
|
21
|
+
|
22
|
+
```css
|
23
|
+
.main_l {
|
24
|
+
display: flex;
|
25
|
+
justify-content: center;
|
26
|
+
align-items: center;
|
27
|
+
}
|
28
|
+
```
|
1
add fix for extra scroll bar
answer
CHANGED
@@ -9,4 +9,8 @@
|
|
9
9
|
|
10
10
|
position などいりません。
|
11
11
|
|
12
|
-
[Demo](https://codesandbox.io/s/peaceful-khorana-z2d25)
|
12
|
+
[Demo](https://codesandbox.io/s/peaceful-khorana-z2d25)
|
13
|
+
|
14
|
+
---
|
15
|
+
|
16
|
+
追記:`overflow: scroll`使うと上下だけではなく左右スクロールバーも出そうです。上下だけでほしいなら、`overflow-y`を使ってください。左右だけでほしいなら、`overflow-x`を使ってください。
|