回答編集履歴

2

コードの追加

2019/02/13 04:07

投稿

yu-smc
yu-smc

スコア610

test CHANGED
@@ -29,3 +29,93 @@
29
29
  JSのmethodのclickeHadle内を見ると、そのメソッド自体がクリックイベントに反応して発火するはずなのに中身にイベントリスナが書かれているのが怪しかったので、それを消して、yに適当な数値を入れて動かすとスムーススクロールうまくいきました。
30
30
 
31
31
  その周辺のコードを修正してみてはいかがでしょうか。
32
+
33
+
34
+
35
+ <追記2>
36
+
37
+ こちらが動作したコードです。
38
+
39
+
40
+
41
+ ```js
42
+
43
+ new Vue({
44
+
45
+ el:'#s1',
46
+
47
+ methods:{
48
+
49
+ clickHadle:function(){
50
+
51
+ let smoothScrollRequestId = null;
52
+
53
+ const smoothScrollTo = function(x, y){
54
+
55
+ const duration = 500; // スムーススクロールが完了するまでの時間 [ms] (必要に応じて変更)
56
+
57
+ const easing = function (t, b, c, d) { // 移動量の計算関数 (同上)
58
+
59
+ return -c *(t/=d)*(t-2) + b;
60
+
61
+ };
62
+
63
+
64
+
65
+ const startX = window.pageXOffset, startY = window.pageYOffset; // 初期スクロール位置
66
+
67
+ const cx = x - startX, cy = y - startY; // 終了値
68
+
69
+ const startTime = new Date().getTime(); // 開始時刻
70
+
71
+ let t = 0; // 現在時刻
72
+
73
+ const scroll = function(){
74
+
75
+ t = new Date().getTime() - startTime;
76
+
77
+ window.scrollTo(startX + easing(t, 0, cx, duration), startY + easing(t, 0, cy, duration));
78
+
79
+ if (t < duration) smoothScrollRequestId = requestAnimationFrame(scroll);
80
+
81
+ };
82
+
83
+ cancelAnimationFrame(smoothScrollRequestId);
84
+
85
+ smoothScrollRequestId = requestAnimationFrame(scroll);
86
+
87
+ };
88
+
89
+ // クリック時
90
+
91
+ /* if (!Element.prototype.matches) Element.prototype.matches = Element.prototype.msMatchesSelector; */
92
+
93
+ /* const href = e.target.getAttribute("href");
94
+
95
+ if (!e.target.matches("a[href^='#'], a[href^='#'] *")) return; // 内部リンク以外は何もしない
96
+
97
+
98
+
99
+ const dest = href == top ? document.body : document.getElementById(href.slice(1)); // 移動先
100
+
101
+ if (!dest) return; */
102
+
103
+
104
+
105
+ const y = 300 // yに適当な値を代入
106
+
107
+ smoothScrollTo(0, y);
108
+
109
+
110
+
111
+ /* e.preventDefault(); */
112
+
113
+
114
+
115
+ }
116
+
117
+ }
118
+
119
+ });
120
+
121
+ ```

1

新たな発見

2019/02/13 04:07

投稿

yu-smc
yu-smc

スコア610

test CHANGED
@@ -21,3 +21,11 @@
21
21
  https://github.com/rigor789/vue-scrollto#readme
22
22
 
23
23
  (ご存知の上でしたらすみません!)
24
+
25
+
26
+
27
+ <追記>
28
+
29
+ JSのmethodのclickeHadle内を見ると、そのメソッド自体がクリックイベントに反応して発火するはずなのに中身にイベントリスナが書かれているのが怪しかったので、それを消して、yに適当な数値を入れて動かすとスムーススクロールうまくいきました。
30
+
31
+ その周辺のコードを修正してみてはいかがでしょうか。