iscrollのスクロールバーのスタイルを変更
こんにちは、ゼノフィ渡部です。
前回は「iscroll.js」を使用して、スマートフォンで横スクロールバーを出しつつ、横スクロールする方法をご紹介いたしました。
今回は、スクロールバーのスタイルを変更する方法をご紹介いたします。
前回同様、元となる「jquery.js」と「iscroll.js」をダウンロードして、読み込みます。
1 2 | <script type="text/javascript" src="jquery1.7.1.js"></script> <script type="text/javascript" src="iscroll.js"></script> |
下のソースのポイントは「scrollbarClass: ‘scrollbar’,」です。scrollbarClassオプションを使用すると、「scrollbar」とした場合、縦のスクロールバーのクラス名は「scrollbarV」、横は「scrollbarH」となります。また、scrollbarClassオプションでクラスを指定した場合、元々あるスクロールバーのスタイルは消えてしまうので、プロパティを自身で記述する必要があります。上書きする形ではないので、注意が必要です。
下記スクリプトはHTMLファイルに直接(<script type="text/javascript"></script>内に)記述しています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | $(document).ready(function () { var element = $(".scroll_x")[0]; var scroll_x = new iScroll(element, { // スクロールバーのクラス名を決定。 //「scrollbar」の場合、縦のスクロールバーのクラス名は「scrollbarV」、横は「scrollbarH」となる。 scrollbarClass: 'scrollbar', // スクロールバーを常時表示 hideScrollbar: false, // 慣性スクロール禁止 bounce: false, // スクロール領域からの画面全体の縦スクロールを有効に onBeforeScrollStart: function(){} }); }); |
元のスクロールバーのスタイルは「http://cubiq.org/iscroll-4」の「at the demo CSS」に記述があるので、参考にすると幾分かスタイルの作成が楽になると思います。
下のCSSソースの「.scrollbarH」はスクロールバー自身の位置や幅、高さを指定しています。「.scrollbarH > div」ではscrollbarHのdivにネストする形で生成されるdivに対してスタイルを決めています。「.scrollbarH > div」はスクロールバーのカラーや形を指定しています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /* スクロールバー */ .scrollbarH { position: absolute; z-index: 100; height: 8px; bottom: 1px; left: 0; right: 0; } .scrollbarH > div { position: absolute; z-index: 100; height: 100%; background-image: gradient(linear, 0 0, 100% 0, from(#5774AA), to(#1F7EC6)); background-image: -webkit-gradient(linear, 0 0, 100% 0, from(#5774AA), to(#1F7EC6)); background-clip: padding-box; -webkit-background-clip: padding-box; box-sizing: border-box; -webkit-box-sizing: border-box; border: 1px solid #0000FF; border-radius: 4px; -webkit-border-radius: 4px; box-shadow: inset 1px 1px 0 rgba(255,255,255,0.5); -webkit-box-shadow: inset 1px 1px 0 rgba(255,255,255,0.5); } |
サンプルはスマートフォンで確認ができます。通常のスクロールバーは黒が薄まったカラーですが、サンプルでは青色に変更しています。
Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | $(document).ready(function () { var element = $(".scroll_x")[0]; var scroll_x = new iScroll(element, { // スクロールバーのクラス名を決定。 //「scrollbar」の場合、縦のスクロールバーのクラス名は「scrollbarV」、横は「scrollbarH」となる。 scrollbarClass: 'scrollbar', // スクロールバーを常時表示 hideScrollbar: false, // 慣性スクロール禁止 bounce: false, // スクロール領域からの画面全体の縦スクロールを有効に onBeforeScrollStart: function(){} }); }); |
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | body { text-size-adjust: none; -webkit-text-size-adjust: none; } /* 要素からの相対位置 */ .scroll_x { position: relative; padding: 0 0 15px; } /* テーブル */ table { width: 900px; border-top: 1px solid #CCC; border-left: 1px solid #CCC; table-layout: fixed; border-collapse: separate; border-spacing: 0; } table th, table td { padding: 2px; border-right: 1px solid #CCC; border-bottom: 1px solid #CCC; } table th { background: #EEF1F4; } /* スクロールバー */ .scrollbarH { position: absolute; z-index: 100; height: 8px; bottom: 1px; left: 0; right: 0; } .scrollbarH > div { position: absolute; z-index: 100; height: 100%; background-image: gradient(linear, 0 0, 100% 0, from(#5774AA), to(#1F7EC6)); background-image: -webkit-gradient(linear, 0 0, 100% 0, from(#5774AA), to(#1F7EC6)); background-clip: padding-box; -webkit-background-clip: padding-box; box-sizing: border-box; -webkit-box-sizing: border-box; border: 1px solid #0000FF; border-radius: 4px; -webkit-border-radius: 4px; box-shadow: inset 1px 1px 0 rgba(255,255,255,0.5); -webkit-box-shadow: inset 1px 1px 0 rgba(255,255,255,0.5); } |
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <title> iscrollのスクロールバーのスタイルを変更 </title> <script type="text/javascript" src="jquery1.7.1.js"></script> <script type="text/javascript" src="iscroll.js"></script> <script type="text/javascript"> $(document).ready(function () { var element = $(".scroll_x")[0]; var scroll_x = new iScroll(element, { // スクロールバーのクラス名を決定。 //「scrollbar」の場合、縦のスクロールバーのクラス名は「scrollbarV」、横は「scrollbarH」となる。 scrollbarClass: 'scrollbar', // スクロールバーを常時表示 hideScrollbar: false, // 慣性スクロール禁止 bounce: false, // スクロール領域からの画面全体の縦スクロールを有効に onBeforeScrollStart: function(){} }); }); </script> <style type="text/css"> body { text-size-adjust: none; -webkit-text-size-adjust: none; } /* 要素からの相対位置 */ .scroll_x { position: relative; padding: 0 0 15px; } /* テーブル */ table { width: 900px; border-top: 1px solid #CCC; border-left: 1px solid #CCC; table-layout: fixed; border-collapse: separate; border-spacing: 0; } table th, table td { padding: 2px; border-right: 1px solid #CCC; border-bottom: 1px solid #CCC; } table th { background: #EEF1F4; } /* スクロールバー */ .scrollbarH { position: absolute; z-index: 100; height: 8px; bottom: 1px; left: 0; right: 0; } .scrollbarH > div { position: absolute; z-index: 100; height: 100%; background-image: gradient(linear, 0 0, 100% 0, from(#5774AA), to(#1F7EC6)); background-image: -webkit-gradient(linear, 0 0, 100% 0, from(#5774AA), to(#1F7EC6)); background-clip: padding-box; -webkit-background-clip: padding-box; box-sizing: border-box; -webkit-box-sizing: border-box; border: 1px solid #0000FF; border-radius: 4px; -webkit-border-radius: 4px; box-shadow: inset 1px 1px 0 rgba(255,255,255,0.5); -webkit-box-shadow: inset 1px 1px 0 rgba(255,255,255,0.5); } </style> </head> <body> <h1> テーブル下にスタイルを変更したスクロールバーを表示 </h1> <div class="scroll_x"> <table> <col style="width: 20%" /> <col style="width: 20%" /> <col style="width: 20%" /> <col style="width: 20%" /> <tr> <th> タイトル </th> <th> タイトル </th> <th> タイトル </th> <th> タイトル </th> </tr> <tr> <td> データ </td> <td> データ </td> <td> データ </td> <td> データ </td> </tr> <tr> <td> データ </td> <td> データ </td> <td> データ </td> <td> データ </td> </tr> </table> </div> </body> </html> |
