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

回答編集履歴

1

HTML追記

2018/01/25 05:49

投稿

x_x
x_x

スコア13749

answer CHANGED
@@ -1,4 +1,66 @@
1
1
  ```JavaScript
2
2
  //$nav = $('tr'),
3
3
  $nav = $('thead'),
4
+ ```
5
+
6
+ -- HTML追記
7
+ ```HTML
8
+ <!DOCTYPE html>
9
+ <html>
10
+ <head><meta charset="UTF-8" /><title>title</title>
11
+ <style>
12
+ .is-fixed {
13
+ position: fixed;
14
+ top: 0;
15
+ left: 0;
16
+ z-index: 2;
17
+ width: 100%;
18
+ }
19
+ </style>
20
+ </head>
21
+ <body>
22
+ <table>
23
+ <thead>
24
+ <tr>
25
+ <th>id</th>
26
+ <th>タイトル</th>
27
+ </tr>
28
+ </thead>
29
+ <tbody>
30
+ <tr>
31
+ <td>1</td>
32
+ <td>プログラミング全般</td>
33
+ </tr>
34
+ <tr>
35
+ <td>2</td>
36
+ <td>PHP</td>
37
+ </tr>
38
+ </tbody>
39
+ </table>
40
+ <main style="height:200vh"><p>main</p></main>
41
+ <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
42
+ <script>
43
+ $(function() {
44
+ var $win = $(window),
45
+ $main = $('main'),
46
+ $nav = $('thead'),
47
+ navHeight = $nav.outerHeight(),
48
+ navPos = $nav.offset().top,
49
+ fixedClass = 'is-fixed';
50
+
51
+ $win.on('load scroll', function() {
52
+ var value = $(this).scrollTop();
53
+ if ( value > navPos ) {
54
+ $nav.addClass(fixedClass);
55
+ $main.css('margin-top', navHeight);
56
+ console.log("動作せず")
57
+ } else {
58
+ $nav.removeClass(fixedClass);
59
+ $main.css('margin-top', '0');
60
+ }
61
+ });
62
+ });
63
+ </script>
64
+ </body>
65
+ </html>
4
66
  ```