LoginSignup
2
1

More than 5 years have passed since last update.

メニューバーやサイドバーをモバイル版に対応させる

Last updated at Posted at 2016-03-24

Railsのviewでメニューバーやサイドバーをスマホ対応にさせる

bootstrapをつかうのでもし使えない場合は

Gemfile.rb
gem 'twitter-bootstrap-rails'

してbundle installします.
レイアウトとしてつかうので
_menubar.html.hamlという命名にしておきます.bootstrapでは、グリッドシステムもそうですが、
hidden-xs=>モバイルの画面の大きさだと見えなくなるクラス
visible-xs=>モバイルの画面の大きさでのみ見えるクラス
を提供しているのでこれを利用します

layouts/_menubar.html.haml
.slidebar-nav
  .navbar.visible-xs{role:"navigation"}
    .navabr-header
      %button.navbar-toggle.bg-dark{type:"button","data-toggle"=>"collapse", "data-target"=>"#custom-collapse"}
        %span.sr-only Toggle navigation
        %span.icon-bar.bg-white
        %span.icon-bar.bg-white
        %span.icon-bar.bg-white
      %span.visible-xs.navbar-brand.menubar-title=link_to "",root_path
.collapse.navbar-collapse#custom-collapse{style:"padding: 0px;"}
  .hidden-xs
    %h2= link_to "ここにnavbar-brandと同じタイトルを入れます",root_path
    .p-h-xs
  %ul.menubar-list-module
    /-#ここにmenubarのコンテンツを書きます
    %li=link_to "","".....
     ....
    %li =link_to "",""

こんな風にすればウインドウを小さくした時
タイトルとボタンが横並びになってくれるヘッダーが生成されます(^ ^)

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1