Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

              
                <button type="button" class="menu" id="fix">
  <span class="menu-bar"></span>
</button>
<nav class="nav" id="nav">
  <p>メニュー</p>
</nav>

<main class="contents">

  <h1>Menu Open Scroll stop(body fixed) Vanilla JS</h1>

  <p><img src="https://picsum.photos/id/1003/500/600" alt=""></p>
  <p><img src="https://picsum.photos/id/1062/1600/500" alt=""></p>
  <p><img src="https://picsum.photos/id/275/400/500" alt=""></p>

</main>
              
            
!

CSS

              
                @charset "UTF-8";
/* テスト用style */
html {
  min-height: 100vh;
}
body {
  margin: 0;
  padding: 100px 0;
  text-align: center;
}
h1 {
  text-align: center;
  margin-bottom: 10vh;
}
p {
  margin: 100px 0;
}
img {
  max-width: 100%;
  vertical-align: top;
}
.menu {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 100;
  height: 50px;
  width: 50px;
}
.menu-bar {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 2px;
  width: 20px;
  margin: 0 auto;
  background-color: #000;
  margin-top: -1px;
}
.menu-bar:before,
.menu-bar:after {
  content: "";
  position: absolute;
  left: 0;
  height: 2px;
  width: 20px;
  background-color: #000;
  transition: transform .5s, top .5s;
}
.menu-bar:before {
  top: -7px;
}
.menu-bar:after {
  top: 7px;
}
.menu.is-active .menu-bar {
  background-color: transparent;
}
.menu.is-active .menu-bar:before {
  top: 0;
  transform: rotate(45deg);
}
.menu.is-active .menu-bar:after {
  top: 0;
  transform: rotate(-45deg);
}
.nav {
  visibility: hidden;
  opacity: 0;
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  z-index: 50;
  background-color: rgba(0,0,0,.5);
  transition: opacity .5s, visibility .5s;
  color: #fff;
  font-size: 40px;
}
.nav p {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  margin: 0;
}
.nav.is-active {
  visibility: visible;
  opacity: 1;
}
              
            
!

JS

              
                //スクロール固定オブジェクトセット
const bodyScroll = {
	//スクロール位置保存用プロパティ
	scrollY: 0,

	//スクロール固定用ベースstyle
	styles: {
		position: 'fixed',
		left: 0,
		width: '100%',
		boxSizing: 'border-box',
	},

	//スクロール固定・解除メソッド
	fixed: function(isFixed) {
		//スクロール固定
		if(isFixed) {
			//style追加・更新
			//現在の位置を取得
			this.scrollY = window.pageYOffset || document.documentElement.scrollTop;
			//現在の位置をstyleに設定
			this.styles.top = (this.scrollY * -1) + 'px';

			//スクロールバーの幅取得・設定(ガタツキ防止)
			this.styles.paddingRight = (window.innerWidth - document.body.clientWidth) + 'px'

			//その他のstyleをセット
			Object.keys(this.styles).forEach(function(key) {
				document.body.style[key] = this.styles[key];
			}.bind(this));


		//スクロール固定解除
		} else {
			//styleリセット
			Object.keys(this.styles).forEach(function(key) {
				document.body.style[key] = '';
			});

			//スクロール位置設定
			window.scrollTo(0, this.scrollY);
		}
	}
};

//スクロール固定テストコード
const fixBtn = document.getElementById('fix');
const nav = document.getElementById('nav');
fixBtn.addEventListener('click',function() {
	if(fixBtn.classList.contains('is-active')) {

		//スクロール停止解除
		bodyScroll.fixed(false);

		//メニュー非表示
		fixBtn.classList.remove('is-active');
		nav.classList.remove('is-active');
	} else {

		//スクロール停止
		bodyScroll.fixed(true);

		//メニュー表示
		fixBtn.classList.add('is-active');
		nav.classList.add('is-active');
	}
});
              
            
!
999px

Console