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 function(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 bodyScrollFixed = isFixed => {
	//スクロール固定用ベースstyle
  const styles = {
    position: 'fixed',
    top: 0,
    left: 0,
    width: '100%',
    boxSizing: 'border-box',
    paddingRight: 0
  };
  const body = document.body;

	//スクロール固定
  if(isFixed) {
    //style設定
    const scrollY = window.pageYOffset || document.documentElement.scrollTop;
    styles.top = (scrollY * -1) + 'px';
    styles.paddingRight = (window.innerWidth - document.body.clientWidth) + 'px';
    Object.keys(styles).forEach( key => {
      body.style[key] = styles[key];
    });

    body.classList.add('is-fixed');

	} else {
		//スクロール固定解除

		//スクロール位置取得
    const scrollY = parseInt(body.style.top || '0');

    //style解除
    Object.keys(styles).forEach( key => {
      body.style[key] = '';
    });
    body.classList.remove('is-fixed');

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


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

		//スクロール停止解除
		bodyScrollFixed(false);

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

		//スクロール停止
		bodyScrollFixed(true);

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

Console