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

              
                <body>
  <header id="top">
    <p>header</p>
  </header>
  <main>
    <div class="top-visual">
      <h1>top-visual</h1>
    </div>
    <div class="sec"></div>
  </main>
  <a class="btn" href="#top">to top</a>
  <footer>
    <p>footer</p>
  </footer>
</body>
              
            
!

CSS

              
                *{
  margin: 0;
}
body{
  position: relative;
}
header{
  width: 100%;
  height: 80px;
  background-color: #333;
}
header p{
  line-height: 80px;
  text-align: center;
  color: #fff;
}
.top-visual{
  width: 100%;
  height: 400px;
  background-color: cadetblue;
  position: relative;
}
.top-visual h1{
  font-size: 30px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.sec{
  background-color: aquamarine;
  height: 2000px;
}
.btn{
  position: fixed;
  bottom: 0;
  right: 10%;
  text-decoration: none;
  background-color: red;
  color: #fff;
  padding: 20px 10px;
  border-radius: 50%;
  opacity: 0;
  transition: opacity .5s;
}
.btn.active{
  opacity: 1;
}
/* 追記 */
.btn.stop{
  position: absolute;
  bottom: 200px;
}
footer{
  height: 200px;
  background-color: blue;
}
footer p{
  line-height: 200px;
  text-align: center;
  color: #fff;
}
              
            
!

JS

              
                //スクロールした時に処理を実行
window.addEventListener('scroll', function(){
  //トップへ戻るボタンを取得
  let topBtn = document.querySelector('.btn');
  
  //画面上部からトップビジュアル下の位置取得
  const topVisual = document.querySelector('.top-visual').getBoundingClientRect().bottom;

  //トップビジュアル下の位置より下にスクロールされたらactiveクラウを付与
  if(topVisual <= 0){
    topBtn.classList.add('active');
  } else {
    //スクロールが200px未満のときactiveクラスを外す
    topBtn.classList.remove('active');
  }
  //ドキュメントの高さ
  const scrollHeight = document.body.clientHeight;

  //スクロール位置
  const scrollPosition = window.pageYOffset;

  //windowの高さ
  const windowHeignt = window.innerHeight;

  //footer取得
  const footer = document.querySelector('footer');
  console.log(footer.getBoundingClientRect().top)
  //footerの高さ
  const footerHeight = footer.offsetHeight;
  if(scrollHeight - scrollPosition - windowHeignt <= footerHeight){
    topBtn.classList.add('stop');
  } else {
    topBtn.classList.remove('stop');
  }
});
              
            
!
999px

Console