質問編集履歴

2

修正

2022/05/30 03:40

投稿

jus
jus

スコア60

test CHANGED
@@ -1 +1 @@
1
- 背景画像がランダムで表示させるスライドショーを作りたい
1
+ css,javascriptを使い、背景画像がランダムで表示させるスライドショーを作りたい
test CHANGED
File without changes

1

修正

2022/05/29 14:44

投稿

jus
jus

スコア60

test CHANGED
File without changes
test CHANGED
@@ -1,85 +1,116 @@
1
1
  ### 前提
2
2
 
3
3
  rails で投稿アプリを作っています。
4
- 背景画像がランダムで流れるように設定したいのですが、
4
+ 背景画像がイドショーのように流れるように設定したいのですが、
5
- 枠みたいなものだけが表示され画像が表示されません。
5
+ 画像が表示されません。
6
6
  画像はapp/assets/imagesに保存しています。
7
7
 
8
- https://www.sejuku.net/blog/68469
8
+ https://yukipan-world.com/web-tips/568/#i-2
9
- このサイトのランダムで表示させたい場合を参照しています。
9
+ このサイトの画像の上にテキストを表示させたい場合を参照しています。
10
10
 
11
+ また、背景画像をランダムに流すには
11
- javascriptどのように使正直認識していません。
12
+ javascriptどのような記述を行えばいいのでしょうか
12
- 併せてどのように適用させればいいのかも教えていただきたいです。
13
+
13
14
 
14
15
 
15
16
 
16
17
  ### 該当のソースコード
17
18
 
18
19
  ```
19
- application/js
20
-
21
- // This file is automatically compiled by Webpack, along with any other files
22
- // present in this directory. You're encouraged to place your actual application logic in
23
- // a relevant structure within app/javascript and only use these pack files to reference
24
- // that code so it'll be compiled.
25
-
26
- import Rails from "@rails/ujs"
27
- import Turbolinks from "turbolinks"
28
- import * as ActiveStorage from "@rails/activestorage"
29
- import "channels"
30
- import "script.js"
31
- import 'jquery'
32
- import "bootstrap";
33
- import "../stylesheets/application";
34
-
35
- Rails.start()
36
- Turbolinks.start()
37
- ActiveStorage.start()
38
-
39
- const assets_src = ["assets/1.jpg","assets/2.jpg","assets/first-back.jpg"];
40
- let num = -1;
41
-
42
-
43
- function slideshow_timer(){
44
-
45
- num = Math.floor(Math.random()*assets_src.length);
46
-
47
- document.getElementById("mypic").src = assets_src[num];
48
-
49
- }
50
-
51
- setInterval(slideshow_timer,1000);
52
- ```
53
- ```
54
20
  application/html
55
21
 
56
22
  <body>
23
+ <div class="slideBox">
24
+ <p class="title">cssのみでつくったスライド</p>
25
+ <div class="item2" style="background-image: url(./assets/images/1.jpg)"></div>
26
+ <div class="item2" style="background-image: url(./assets/images/2.jpg)"></div>
27
+ <div class="item2" style="background-image: url(./assets/images/first-back.jpg)"></div>
57
- <header>
28
+ </body>
58
-
59
-
60
29
 
61
- <div class="header-list">
62
- <ul>
30
+ ```
63
- <% if user_signed_in? %>
64
- <li class="button"><%= link_to "投稿する", new_post_path %></li>
65
- <li class="button"><%= link_to "投稿一覧", posts_path %></li>
66
- <li class="button"><%= link_to "ランキング", post_rank_path %></li>
67
- <li class="button"><%= link_to "マイページへ", user_path(current_user.id) %></li>
68
- <li class="button"><%= link_to "ログアウト", destroy_user_session_path, method: :delete %></li>
69
- <% else %>
70
- <li class="button"><%= link_to "投稿一覧", posts_path %></li>
71
- <li class="button"><%= link_to "ランキング", post_rank_path %></li>
72
- <li class="button"><%= link_to "ログイン", new_user_session_path %></li>
73
- <li class="button"><%= link_to "アカウント作成", new_user_registration_path %></li>
74
- <% end %>
31
+ ```
75
- </ul>
32
+ application/scss
76
- </div>
77
-
78
- </header>
79
- <%= yield %>
80
- <img id="mypic" width="400" height="300">
81
- </body>
82
33
 
34
+ .slideBox {
35
+ height: 500px;
36
+ overflow: hidden;
37
+ position: relative;
38
+ }
39
+
40
+
41
+ .title {
42
+ font-size: 32px;
43
+ color: #fff;
44
+ z-index: 11;
45
+ position: absolute;
46
+ top: 50%;
47
+ left: 50%;
48
+ transform: translate(-50%,-50%);
49
+ }
50
+
51
+ .item2 {
52
+ z-index: 10;
53
+ opacity: 0;
54
+ width: 100%;
55
+ height: 500px;
56
+ background-position: center center;
57
+ background-repeat: no-repeat;
58
+ background-size: cover;
59
+ position: absolute;
60
+ left: 0;
61
+ top: 0;
62
+ -webkit-animation: anime 30s 0s infinite;
63
+ animation: anime 30s 0s infinite;
64
+ }
65
+
66
+ .item2:nth-of-type(2) {
67
+ -webkit-animation-delay: 10s;
68
+ animation-delay: 10s;
69
+ }
70
+
71
+ .item2:nth-of-type(3) {
72
+ -webkit-animation-delay: 20s;
73
+ animation-delay: 20s;
74
+ }
75
+
76
+
77
+ @keyframes anime {
78
+ 0% {
79
+ opacity: 0;
80
+ }
81
+ 30% {
82
+ opacity: 1;
83
+ }
84
+ 35% {
85
+ opacity: 1;
86
+ }
87
+ 50% {
88
+ opacity: 0;
89
+ z-index: 9;
90
+ }
91
+ 100% {
92
+ opacity: 0;
93
+ }
94
+ }
95
+
96
+ @-webkit-keyframes anime {
97
+ 0% {
98
+ opacity: 0;
99
+ }
100
+ 30% {
101
+ opacity: 1;
102
+ }
103
+ 35% {
104
+ opacity: 1;
105
+ }
106
+ 50% {
107
+ opacity: 0;
108
+ z-index: 9;
109
+ }
110
+ 100% {
111
+ opacity: 0;
112
+ }
113
+ }
83
114
  ```
84
115
  ```
85
116
  ```