回答編集履歴

1

調整

2023/09/01 05:06

投稿

yambejp
yambejp

スコア117707

test CHANGED
@@ -48,3 +48,54 @@
48
48
  </div><!-- box -->
49
49
  </div><!-- wrapper -->
50
50
  ```
51
+
52
+ # 参考
53
+ 一応cssで指定しないkeyframeアニメーションのサンプルです
54
+ ```javascript
55
+ <style>
56
+ #box {
57
+ width: 320px;
58
+ height: 180px;
59
+ background-color: #fdd;
60
+ cursor: pointer;
61
+ }
62
+
63
+ #box p {
64
+ line-height: 180px;
65
+ font-size: 32px;
66
+ text-align: center;
67
+ }
68
+ </style>
69
+ <script>
70
+ let myAnime=new Animation();
71
+ function slideAnimation(element,distances,duration,fill="none"){
72
+ const myKeyframe= new KeyframeEffect(
73
+ element,
74
+ [
75
+ {transform: "translate(0px,0px)",backgroundColor:"red"},
76
+ {transform: `translate(${distances[0]}px,${distances[1]}px)` ,backgroundColor:"blue"},
77
+ ],
78
+ {duration,fill},
79
+ );
80
+ myAnime = new Animation(myKeyframe,document.timeline);
81
+ //myAnime.addEventListener('finish',()=>delete(element.dataset.active));
82
+ };
83
+ function reset(){
84
+ delete(box.dataset.active);
85
+ myAnime.cancel();
86
+ }
87
+ document.addEventListener('click',({target})=>{
88
+ if(target.closest('#box:not([data-active])')){
89
+ slideAnimation(box,[500,400],2000,'forwards');
90
+ box.dataset.active="1";
91
+ myAnime.play();
92
+ }
93
+ });
94
+ </script>
95
+ <div id="wrapper">
96
+ <button onclick="reset()">リセット</button>
97
+ <div id="box">
98
+ <p>box</p>
99
+ </div><!-- box -->
100
+ </div><!-- wrapper -->
101
+ ```