teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

修正

2022/05/30 03:40

投稿

jus
jus

スコア60

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

1

修正

2022/05/29 14:44

投稿

jus
jus

スコア60

title CHANGED
File without changes
body 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
+ application/html
20
21
 
22
+ <body>
23
+ <div class="slideBox">
21
- // This file is automatically compiled by Webpack, along with any other files
24
+ <p class="title">cssのみでつくったスライド</p>
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
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>
24
- // that code so it'll be compiled.
28
+ </body>
25
29
 
26
- import Rails from "@rails/ujs"
27
- import Turbolinks from "turbolinks"
28
- import * as ActiveStorage from "@rails/activestorage"
29
- import "channels"
30
+ ```
30
- import "script.js"
31
+ ```
31
- import 'jquery'
32
+ application/scss
32
- import "bootstrap";
33
- import "../stylesheets/application";
34
33
 
34
+ .slideBox {
35
- Rails.start()
35
+ height: 500px;
36
- Turbolinks.start()
36
+ overflow: hidden;
37
- ActiveStorage.start()
37
+ position: relative;
38
+ }
38
39
 
40
+
41
+ .title {
39
- const assets_src = ["assets/1.jpg","assets/2.jpg","assets/first-back.jpg"];
42
+ font-size: 32px;
43
+ color: #fff;
40
- let num = -1;
44
+ z-index: 11;
41
-
42
-
43
- function slideshow_timer(){
45
+ position: absolute;
44
-
46
+ top: 50%;
47
+ left: 50%;
45
- num = Math.floor(Math.random()*assets_src.length);
48
+ transform: translate(-50%,-50%);
46
-
47
- document.getElementById("mypic").src = assets_src[num];
48
-
49
49
  }
50
-
51
- setInterval(slideshow_timer,1000);
52
- ```
53
- ```
54
- application/html
55
50
 
51
+ .item2 {
52
+ z-index: 10;
56
- <body>
53
+ opacity: 0;
54
+ width: 100%;
57
- <header>
55
+ height: 500px;
58
-
56
+ background-position: center center;
59
-
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
+ }
60
65
 
61
- <div class="header-list">
66
+ .item2:nth-of-type(2) {
62
- <ul>
63
- <% if user_signed_in? %>
67
+ -webkit-animation-delay: 10s;
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 %>
75
- </ul>
76
- </div>
77
-
78
- </header>
68
+ animation-delay: 10s;
79
- <%= yield %>
69
+ }
80
- <img id="mypic" width="400" height="300">
81
- </body>
82
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
  ```