回答編集履歴

4

調整

2024/09/26 09:51

投稿

yambejp
yambejp

スコア116724

test CHANGED
@@ -33,32 +33,27 @@
33
33
  </style>
34
34
  <script>
35
35
  window.addEventListener('DOMContentLoaded', ()=>{
36
- let mouseX = window.innerWidth / 2;
36
+ const mouse={X:window.innerWidth / 2,Y:window.innerHeight};
37
- let mouseY = window.innerHeight / 2;
38
- let illustrationX = mouseX;
39
- let illustrationY = mouseY;
40
- const illustration = document.querySelector(".illustration");
37
+ const illust={X:mouse.X,Y:mouse.Y,elem:document.querySelector(".illustration")};
41
- illustration.onload = function () {
42
- illustration.setAttribute('hidden',1);
38
+ illust.elem.setAttribute('hidden',1);
43
- illustrationX = mouseX;
39
+ illust.X = mouse.X;
44
- illustrationY = mouseY;
40
+ illust.Y = mouse.Y;
45
- updateIllustration();
46
- };
47
- function updateIllustration() {
41
+ const updateIllustration=()=>{
48
- let dx = mouseX - illustrationX;
42
+ let dx = mouse.X - illust.X;
49
- let dy = mouseY - illustrationY;
43
+ let dy = mouse.Y - illust.Y;
50
44
  let delayFactor = 0.1;
51
- illustrationX += dx * delayFactor;
45
+ illust.X += dx * delayFactor;
52
- illustrationY += dy * delayFactor;
46
+ illust.Y += dy * delayFactor;
53
- illustration.style.transform = 'translate(' + (illustrationX - illustration.width / 2) + 'px, ' + (illustrationY - illustration.height / 2) + 'px)';
47
+ illust.elem.style.transform = 'translate(' + (illust.X - illust.elem.width / 2) + 'px, ' + (illust.Y - illust.elem.height / 2) + 'px)';
54
48
  requestAnimationFrame(updateIllustration);
55
49
  }
50
+ updateIllustration();
56
- document.addEventListener('mousemove',function(e) {
51
+ document.addEventListener('mousemove',e=>{
57
- illustration.removeAttribute('hidden');
52
+ illust.elem.removeAttribute('hidden');
58
53
  const orgY = document.body.getBoundingClientRect().top;
59
54
  const r = e.target.getBoundingClientRect();
60
- mouseX = e.offsetX + r.left;
55
+ mouse.X = e.offsetX + r.left;
61
- mouseY = e.offsetY + r.top - orgY;
56
+ mouse.Y = e.offsetY + r.top - orgY;
62
57
  });
63
58
  const maxClicks = 6;
64
59
  const randomImages = [
@@ -68,19 +63,19 @@
68
63
  'https://via.placeholder.com/100x100/ffff00/ffffff?text=4',
69
64
  'https://via.placeholder.com/100x100/00ffff/ffffff?text=5'
70
65
  ].map(x=>Object.assign(document.createElement('img'),{src:x,className:'random-image'}));
71
- document.addEventListener('click',function(event) {
66
+ document.addEventListener('click',()=>{
72
- const clickCounter=document.querySelectorAll('.random-image').length+1;
67
+ const clickCounter=document.querySelectorAll('.random-image').length;
73
- if (clickCounter < maxClicks) {
68
+ if (clickCounter < maxClicks-1) {
74
- let randomIndex = Math.floor(Math.random() * randomImages.length);
69
+ const randomIndex = Math.floor(Math.random() * randomImages.length);
75
70
  const randomImage = randomImages[randomIndex].cloneNode(true);
76
- randomImage.style.left = (mouseX - randomImage.width / 2) + 'px';
71
+ randomImage.style.left = `${mouse.X - randomImage.width / 2}px`;
77
- randomImage.style.top = (mouseY - randomImage.height / 2) + 'px';
72
+ randomImage.style.top = `${mouse.Y - randomImage.height / 2}px`;
78
73
  document.body.appendChild(randomImage);
79
74
  }else{
80
- document.querySelectorAll('.random-image').forEach(function (image) {
75
+ document.querySelectorAll('.random-image').forEach(image=>{
81
76
  image.style.opacity = 0;
82
- image.addEventListener('transitionend', function () {
77
+ image.addEventListener('transitionend',()=>{
83
- image.remove();
78
+ image.remove();
84
79
  });
85
80
  });
86
81
  }
@@ -90,3 +85,4 @@
90
85
  <img src='https://via.placeholder.com/50x50/000000/ffffff?text=Cursor' alt="" class="illustration">
91
86
  <div id="hero"></div>
92
87
  ```
88
+ ※再調整

3

chousei

2024/09/26 09:09

投稿

yambejp
yambejp

スコア116724

test CHANGED
@@ -22,7 +22,6 @@
22
22
  will-change: transform;
23
23
  z-index: 5;
24
24
  }
25
- .active {display: block;}
26
25
  .random-image {
27
26
  position: absolute;
28
27
  transition: opacity 0.5s linear;
@@ -54,19 +53,13 @@
54
53
  illustration.style.transform = 'translate(' + (illustrationX - illustration.width / 2) + 'px, ' + (illustrationY - illustration.height / 2) + 'px)';
55
54
  requestAnimationFrame(updateIllustration);
56
55
  }
57
- document.addEventListener('mousemove',onMouseMove);
56
+ document.addEventListener('mousemove',function(e) {
58
- function onMouseMove(e) {
59
57
  illustration.removeAttribute('hidden');
60
58
  const orgY = document.body.getBoundingClientRect().top;
61
59
  const r = e.target.getBoundingClientRect();
62
60
  mouseX = e.offsetX + r.left;
63
- mouseY = e.offsetY + r.top-orgY;
61
+ mouseY = e.offsetY + r.top - orgY;
64
- }
62
+ });
65
- hero.addEventListener('mouseover', () => handlerMouseStoker(true));
66
- hero.addEventListener('mouseout', () => handlerMouseStoker(false));
67
- function handlerMouseStoker(isActive) {
68
- illustration.classList.toggle('active', isActive);
69
- }
70
63
  const maxClicks = 6;
71
64
  const randomImages = [
72
65
  'https://via.placeholder.com/100x100/ff0000/ffffff?text=1',
@@ -94,9 +87,6 @@
94
87
  });
95
88
  });
96
89
  </script>
97
- </body>
98
-
99
- </html>
100
90
  <img src='https://via.placeholder.com/50x50/000000/ffffff?text=Cursor' alt="" class="illustration">
101
91
  <div id="hero"></div>
102
92
  ```

2

調整

2024/09/26 09:01

投稿

yambejp
yambejp

スコア116724

test CHANGED
@@ -1,11 +1,102 @@
1
- 「.illustration 」に設定されている「display: none;」を外てください
1
+ ざっくりリファクタリングときました
2
- それとonMouseMoveにはclientではくoffsetを指定しくださ
2
+ 実際には使っていソースが残っているかもしれませんが、あしからず
3
- ```javascript
3
+ ```html
4
- function onMouseMove(event) {
5
- //mouseX = event.clientX;
4
+ <style>
5
+ body {
6
- //mouseY = event.clientY;
6
+ margin: 0;
7
- mouseX = event.offsetX;
7
+ position: relative;
8
+ height: 200vh;
9
+ width: 100%;
8
- mouseY = event.offsetY;
10
+ background-color: #e97d7d;
9
- }
11
+ }
12
+ #hero {
13
+ width: 100vw;
14
+ height: 120vh;
15
+ background-color: rgb(129, 125, 236);
16
+ margin-top: 0;
17
+ position: absolute;
18
+ }
19
+ .illustration {
20
+ position: absolute;
21
+ pointer-events: none;
22
+ will-change: transform;
23
+ z-index: 5;
24
+ }
25
+ .active {display: block;}
26
+ .random-image {
27
+ position: absolute;
28
+ transition: opacity 0.5s linear;
29
+ opacity: 1;
30
+ }
31
+ * {
32
+ outline: 2px solid red;
33
+ }
34
+ </style>
35
+ <script>
36
+ window.addEventListener('DOMContentLoaded', ()=>{
37
+ let mouseX = window.innerWidth / 2;
38
+ let mouseY = window.innerHeight / 2;
39
+ let illustrationX = mouseX;
40
+ let illustrationY = mouseY;
41
+ const illustration = document.querySelector(".illustration");
42
+ illustration.onload = function () {
43
+ illustration.setAttribute('hidden',1);
44
+ illustrationX = mouseX;
45
+ illustrationY = mouseY;
46
+ updateIllustration();
47
+ };
48
+ function updateIllustration() {
49
+ let dx = mouseX - illustrationX;
50
+ let dy = mouseY - illustrationY;
51
+ let delayFactor = 0.1;
52
+ illustrationX += dx * delayFactor;
53
+ illustrationY += dy * delayFactor;
54
+ illustration.style.transform = 'translate(' + (illustrationX - illustration.width / 2) + 'px, ' + (illustrationY - illustration.height / 2) + 'px)';
55
+ requestAnimationFrame(updateIllustration);
56
+ }
57
+ document.addEventListener('mousemove',onMouseMove);
58
+ function onMouseMove(e) {
59
+ illustration.removeAttribute('hidden');
60
+ const orgY = document.body.getBoundingClientRect().top;
61
+ const r = e.target.getBoundingClientRect();
62
+ mouseX = e.offsetX + r.left;
63
+ mouseY = e.offsetY + r.top-orgY;
64
+ }
65
+ hero.addEventListener('mouseover', () => handlerMouseStoker(true));
66
+ hero.addEventListener('mouseout', () => handlerMouseStoker(false));
67
+ function handlerMouseStoker(isActive) {
68
+ illustration.classList.toggle('active', isActive);
69
+ }
70
+ const maxClicks = 6;
71
+ const randomImages = [
72
+ 'https://via.placeholder.com/100x100/ff0000/ffffff?text=1',
73
+ 'https://via.placeholder.com/100x100/00ff00/ffffff?text=2',
74
+ 'https://via.placeholder.com/100x100/0000ff/ffffff?text=3',
75
+ 'https://via.placeholder.com/100x100/ffff00/ffffff?text=4',
76
+ 'https://via.placeholder.com/100x100/00ffff/ffffff?text=5'
77
+ ].map(x=>Object.assign(document.createElement('img'),{src:x,className:'random-image'}));
78
+ document.addEventListener('click',function(event) {
79
+ const clickCounter=document.querySelectorAll('.random-image').length+1;
80
+ if (clickCounter < maxClicks) {
81
+ let randomIndex = Math.floor(Math.random() * randomImages.length);
82
+ const randomImage = randomImages[randomIndex].cloneNode(true);
83
+ randomImage.style.left = (mouseX - randomImage.width / 2) + 'px';
84
+ randomImage.style.top = (mouseY - randomImage.height / 2) + 'px';
85
+ document.body.appendChild(randomImage);
86
+ }else{
87
+ document.querySelectorAll('.random-image').forEach(function (image) {
88
+ image.style.opacity = 0;
89
+ image.addEventListener('transitionend', function () {
90
+ image.remove();
91
+ });
92
+ });
93
+ }
94
+ });
95
+ });
96
+ </script>
97
+ </body>
98
+
99
+ </html>
100
+ <img src='https://via.placeholder.com/50x50/000000/ffffff?text=Cursor' alt="" class="illustration">
101
+ <div id="hero"></div>
10
102
  ```
11
-

1

調整

2024/09/26 07:34

投稿

yambejp
yambejp

スコア116724

test CHANGED
@@ -1 +1,11 @@
1
1
  「.illustration 」に設定されている「display: none;」を外してください
2
+ それとonMouseMoveにはclientではなくoffsetを指定してください
3
+ ```javascript
4
+ function onMouseMove(event) {
5
+ //mouseX = event.clientX;
6
+ //mouseY = event.clientY;
7
+ mouseX = event.offsetX;
8
+ mouseY = event.offsetY;
9
+ }
10
+ ```
11
+