Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <header>
  <h1>【jQuery】ページ内リンクでスムーズスクロール(固定ヘッダーがあったら)</h1>
</header>

<section>
  <div class="inner">

    <ul class="list" id="m_01">
      <li><a href="#1">1</a></li>
      <li><a href="#2">2</a></li>
      <li><a href="#3">3</a></li>
      <li><a href="#4">4</a></li>
      <li><a href="#5">5</a></li>
      <li><a href="#6">6</a></li>
    </ul>

    <div class="oneArea" id="1">
      <div class="one_title">1</div>
      <a class="button" href="#m_01">▲ メニューに戻る</a>
    </div>

    <div class="oneArea" id="2">
      <div class="one_title">2</div>
      <a class="button" href="#m_01">▲ メニューに戻る</a>
    </div>

    <div class="oneArea" id="3">
      <div class="one_title">3</div>
      <a class="button" href="#m_01">▲ メニューに戻る</a>
    </div>

    <div class="oneArea" id="4">
      <div class="one_title">4</div>
      <a class="button" href="#m_01">▲ メニューに戻る</a>
    </div>

    <div class="oneArea" id="5">
      <div class="one_title">5</div>
      <a class="button" href="#m_01">▲ メニューに戻る</a>
    </div>

    <div class="oneArea" id="6">
      <div class="one_title">6</div>
      <a class="button" href="#m_01">▲ メニューに戻る</a>
    </div>

    <p class="_a"><a href="https://125naroom.com/web/3476" target="_blank" class="link">View the note</a></p>

  </div>
</section>
              
            
!

CSS

              
                header {
	position: -webkit-sticky;
	position: sticky;
	top: 0;
	z-index: +1;
}
header h1 {
	font-size: 22px;
	line-height: 2.0;
	padding: 1em .5em;
	margin: 3em 0 0;
	text-align: center;
  background: #FFEB3B;
}
section {
	background: #e5dfd3;
	padding: 120px 20px;
}
section .inner {
  max-width: 1024px;
  margin: 0 auto;
}
section .oneArea {
	margin-bottom: 200px;
}
section ul.list {
	display: flex;
	flex-wrap: wrap;
	justify-content: space-between;
	list-style-type: none;
	margin: 0 0 100px;
	padding: 0;
	box-sizing: border-box;
}
section ul.list li {
	width: calc(100% / 6);
	text-align: center;
}
section ul.list li a {
	display: block;
	padding: 10px;
	text-decoration: none;
	color: #fff;
	background: #333;
	border-right: 1px solid #fff;
	transition-duration: 0.3s;
}
section ul.list li:last-child a {
	border-right: none;
}
section ul.list li a:hover {
	opacity: 0.7;
}
section .oneArea .one_title {
	color: #fff;
	text-align: center;
	padding: 10px;
	background: #333;
}
section .oneArea a.button {
	display: block;
	font-size: 12px;
	color: #333333;
	text-align: center;
	background: #f2f2f2;
	padding: 10px;
	text-decoration: none;
	transition-duration: 0.3s;
	margin-top: 40px;
}
section .oneArea a.button:hover {
	opacity: 0.7;
}
/*==============
common
==============*/
body {
  font-family: YuGothic, "游ゴシック体", "Yu Gothic", "ヒラギノ角ゴ Pro", "Hiragino Kaku Gothic Pro", "メイリオ", Meiryo, "MS Pゴシック", "MS PGothic", sans-serif;
  line-height: 1.8;
  margin: 0;
  background: #fff;
}
p._a {
  font-size: 12px;
  font-weight: bold;
  margin: 30px 0 0;
}
p._a .link {
  display: inline-block;
  color: #607D8B;
  padding-left: 1.3em;
  text-indent: -1.3em;
}
p._a .link:before {
  content: '';
  display: inline-block;
  width: 5px;
  height: 5px;
  border-top: 2px solid #607D8B;
  border-right: 2px solid #607D8B;
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
  margin-right: 10px;
}
              
            
!

JS

              
                // ページ内リンク
$(function(){
  var headerHeight = $('header').outerHeight(); // ヘッダーについているID、クラス名など、余白を開けたい場合は + 10を追記する。
  var urlHash = location.hash; // ハッシュ値があればページ内スクロール
  if(urlHash) { // 外部リンクからのクリック時
    $('body,html').stop().scrollTop(0); // スクロールを0に戻す
    setTimeout(function(){ // ロード時の処理を待ち、時間差でスクロール実行
      var target = $(urlHash);
      var position = target.offset().top - headerHeight;
      $('body,html').stop().animate({scrollTop:position}, 500); // スクロール速度ミリ秒
    }, 100);
  }
  $('a[href^=#]').click(function(){ // 通常のクリック時
    var href= $(this).attr("href"); // ページ内リンク先を取得
    var target = $(href);
    var position = target.offset().top - headerHeight;
    $('body,html').stop().animate({scrollTop:position}, 500); // スクロール速度ミリ秒
    return false; // #付与なし、付与したい場合は、true
  });
});

// ヘッダー固定(IEではPolyfillを使う)
$(function() {
  var elements = document.querySelectorAll('header');
  Stickyfill.add(elements);
});
              
            
!
999px

Console