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

質問編集履歴

3

JSファイルであることを追記

2019/11/24 15:28

投稿

digitalhimiko
digitalhimiko

スコア142

title CHANGED
File without changes
body CHANGED
@@ -25,8 +25,8 @@
25
25
  background-color: rgba(0,0,0,0);
26
26
  }
27
27
  ```
28
-
28
+ JavaScriptの該当部分は下記です。
29
- ```JavaScript該当部分
29
+ ```JavaScript
30
30
  const overlay = document.querySelector('.overlay');
31
31
 
32
32
  btn.onclick = function(e){
@@ -40,8 +40,8 @@
40
40
  //overlay.fillStyle = rgba(0,0,0,0.5);
41
41
  //overlay.style.setAttribute("background-color","rgba(0,0,0,5)");
42
42
  ```
43
-
43
+ JavaScriptの全文も記載します。
44
- ```JavaScript全文
44
+ ```JavaScript
45
45
  const displayedImage = document.querySelector('.displayed-img');
46
46
  const thumbBar = document.querySelector('.thumb-bar');
47
47
 

2

全文を追記

2019/11/24 15:27

投稿

digitalhimiko
digitalhimiko

スコア142

title CHANGED
File without changes
body CHANGED
@@ -26,7 +26,7 @@
26
26
  }
27
27
  ```
28
28
 
29
- ```JavaScript
29
+ ```JavaScript該当部分
30
30
  const overlay = document.querySelector('.overlay');
31
31
 
32
32
  btn.onclick = function(e){
@@ -40,6 +40,59 @@
40
40
  //overlay.fillStyle = rgba(0,0,0,0.5);
41
41
  //overlay.style.setAttribute("background-color","rgba(0,0,0,5)");
42
42
  ```
43
+
44
+ ```JavaScript全文
45
+ const displayedImage = document.querySelector('.displayed-img');
46
+ const thumbBar = document.querySelector('.thumb-bar');
47
+
48
+ const btn = document.querySelector('button');
49
+ const overlay = document.querySelector('.overlay');
50
+
51
+ /* Looping through images */
52
+
53
+ createImages();
54
+ function createImages(){
55
+ for(let i = 1; i<6; i++){
56
+ let newImage = document.createElement('img');
57
+ newImage.setAttribute('src',"images/pic" + i + ".jpg ");
58
+ newImage.setAttribute("id","image" + i);
59
+ thumbBar.appendChild(newImage);
60
+ }
61
+ }
62
+
63
+ changeMainPic();
64
+ function changeMainPic(){
65
+ for(let i=1; i<6; i++){
66
+ let imgBtn = document.getElementById("image" + i);
67
+ console.log(imgBtn);
68
+ imgBtn.addEventListener("click",function(){
69
+ displayedImage.setAttribute("src", "images/pic" + i + ".jpg")
70
+ });
71
+ }
72
+ }
73
+
74
+ /* Wiring up the Darken/Lighten button */
75
+ //無名関数の利用
76
+ btn.onclick = function(e){
77
+ if(e.target.textContent === "Darken"){
78
+ e.target.setAttribute("class","light");
79
+ e.target.textContent = "Lighten";
80
+ overlay.style.backgroundColor = "rgba(0,0,0,0.5)";
81
+ //overlay.style.opacity = 0.5;
82
+ //overlay.fillStyle = rgba(0,0,0,0.5);
83
+ //overlay.style.setAttribute("background-color","rgba(0,0,0,5)");
84
+ }
85
+ else{
86
+ e.target.setAttribute("class","dark");
87
+ e.target.textContent = "Darken";
88
+ overlay.style.backgroundColor = "rgba(0,0,0,0)";
89
+ //overlay.style.opacity = 0;
90
+ //overlay.fillStyle = rgba(0,0,0,0);
91
+ //overlay.style.setAttribute("background-color","rgba(0,0,0,0)");
92
+ }
93
+ }
94
+
95
+ ```
43
96
  上記のJavaScript側の最後の3行の書き方ではいずれもrgba()の値を設定することが出来ません。
44
97
  特になぜsetAttributeのブラウザ組み込み関数が使えないのは、既にCSSのファイルでrgba()の値が設定されているからかな、、、?
45
98
  とも思うのですが。

1

overlay変数の宣言/初期化を記入。

2019/11/24 15:26

投稿

digitalhimiko
digitalhimiko

スコア142

title CHANGED
File without changes
body CHANGED
@@ -27,6 +27,8 @@
27
27
  ```
28
28
 
29
29
  ```JavaScript
30
+ const overlay = document.querySelector('.overlay');
31
+
30
32
  btn.onclick = function(e){
31
33
  if(e.target.textContent === "Darken"){
32
34
  e.target.setAttribute("class","light");