###実現したいこと
サンプルを見て頂くと早いのですが、
https://jsfiddle.net/zbhr67f1/
ご覧の通り、position:fixed;
しているために、スクロール時にnav
の幅がスクロールバーにかぶってしまいますよね。
これを防ぎ、nav
をスクロールバーの中に収めることは可能でしょうか?
###試したこと
::-webkit-scrollbar
はFirefoxで使えませんし、
Firefoxに合わせてJSでnav
の幅を17px減らすと今度はスマホでおかしくなりますし、
かなり困っています。
何か方法があったら教えて下さい。
宜しくお願い致します。
###JSFiddleのコード
情報が重複しますが、上のJSFiddleのコードはこちらです。
html
1<main> 2 3 <section class="sec1"> 4 <div class="sec1-header">header</div> 5 <nav>nav</nav> 6 <div class="sec1-content"> 7 <ul> 8 <li>1</li><li>2</li><li>3</li> 9 </ul> 10 </div> 11 </section> 12 13 <section> 14 15 </section> 16 17 <section> 18 19 </section> 20 21</main>
css
1ul,li { 2 list-style: none; 3 margin: 0; 4 padding: 0; 5} 6body { 7 background: red; 8 overflow-y: hidden; 9 padding: 0; 10 margin: 0; 11} 12main { 13 display: flex; 14 justify-content: space-between; 15} 16section { 17 height: 100vh; 18 width: 33%; 19 background: khaki; 20 overflow-y: scroll; 21} 22.sec1-header { 23 background: palevioletred; 24 height: 50px; 25 margin: 0 0 5px 0; 26 display: flex; 27 justify-content: center; 28 align-items: center; 29} 30.sec1-content ul li { 31 height: 300px; 32 background: royalblue; 33 margin: 5px 0 5px 0; 34 display: flex; 35 justify-content: center; 36 align-items: center; 37 color: white; 38} 39nav { 40 background: cadetblue; 41 display: flex; 42 justify-content: center; 43 align-items: center; 44 height: 50px; 45} 46nav.is-fixed { 47 position: fixed; 48 top: 0px; 49 z-index: 2; 50 width: 33%; 51}
jQuery
1$(function() { 2 var $win = $('.sec1'), 3 $main = $('.sec1-content'), 4 $nav = $('.sec1 nav'), 5 navHeight = $nav.outerHeight(), 6 navPos = $nav.offset().top, 7 fixedClass = 'is-fixed'; 8 9 $win.on('load scroll', function() { 10 var value = $(this).scrollTop(); 11 if ( value > navPos ) { 12 $nav.addClass(fixedClass); 13 $main.css('margin-top', navHeight); 14 } else { 15 $nav.removeClass(fixedClass); 16 $main.css('margin-top', '0'); 17 } 18 }); 19});
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/11 14:17
退会済みユーザー
2019/01/11 15:13
2019/01/12 00:19 編集
退会済みユーザー
2019/01/12 01:05
2019/01/12 09:26