6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsでheaderとfooterを固定(fixed)にして、中のコンテンツのみをスクロールさせる方法

Last updated at Posted at 2017-04-05

ページスクロール時にheaderとfooterは固定して中のコンテンツのみをスクロールさせる方法です。
以下コードはRailsでなくても使用できます。

layouts/_user_header.html.erb
<!-- Start header --><header>
<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <%= link_to "アプリロゴ", root_path, class: "navbar-brand" %>
    </div>
    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav navbar-right">
        <li><%= link_to "ページ1", hoge_path %></li>

        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">メニュー<span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><%= link_to "ページ2", fuga_path %></li>
            <li><<%= link_to "ページ3", fuga_path %>/li>
          </ul>
        </li>
        <% if user_signed_in? %>
          <li><%= link_to 'プロフィール', user_path(current_user) %></li>
          <li><%= link_to 'ログアウト', destroy_user_session_path, method: :delete %></li>
        <% else %>
          <li><%= link_to 'ログイン', new_user_session_path %></li>
          <li><%= link_to 'サインアップ', new_user_registration_path %></li>
        <% end %>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
<!-- //END header --></header>
show.html.erb
<%= render 'layouts/user_header' %>
<div class="wrapper">
-- コンテンツ --
</div>
<footer class="hoge_footer">フッター</footer>
aplication.css.scss
html {
  position: relative;
  min-height: 100%;
}

body {
  /* フッタと同じ高さに設定 */
  margin-bottom: 60px;

  /* 一番下までスクロールした時の余白 */
  padding-bottom: 0;
}
job_administrations.scss
.hoge_footer {
  position: fixed;
  bottom: 0;
  margin-bottom: 0;
  width: 100%;

  /* フッタの高さ */
  height: 60px;
  background: #fff;
  color: #000;
}

参考サイト
http://jirolabo.hatenablog.com/entry/2014/11/10/210550

6
5
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
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?