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

              
                 <div class="wrapper">
    <section>
      <h1>絶対位置の取得</h1>
      <p>任意の要素の絶対位置を取得するデモ。<br>
      
        絶対位置は右上の赤字部分に表示。
      </p>
      <p>
        位置を取得した要素はページの下部。<br>
        スクロールして確認してください。<br>
      </p>
    </section>

    <section>
      <h2>下にスクロールしてください</h2>
      <p>Scroll down to the bottom.</p>
      <p>下にスクロールしてください。<br>
        Scroll down to the bottom.<br>
        下にスクロールしてください。<br>
        Scroll down to the bottom.<br>
        下にスクロールしてください。<br>
        Scroll down to the bottom.<br>
        下にスクロールしてください。<br>
        Scroll down to the bottom.<br>
        下にスクロールしてください。<br>
        Scroll down to the bottom.<br>
      </p>
    </section>

    <section>
      <h2>下にスクロールしてください</h2>
      <p>Scroll down to the bottom.</p>
      <p>下にスクロールしてください。<br>
        Scroll down to the bottom.<br>
        下にスクロールしてください。<br>
        Scroll down to the bottom.<br>
        下にスクロールしてください。<br>
        Scroll down to the bottom.<br>
        下にスクロールしてください。<br>
        Scroll down to the bottom.<br>
        下にスクロールしてください。<br>
        Scroll down to the bottom.<br>
      </p>
    </section>

    <div class="target">
      ターゲット<br>
      <span>
      赤い上線の位置が絶対位置です。<br>
      現在の位置と等しくなります。<span>
    </div>

    <section>
      <h2>上にスクロールしてください</h2>
      <p>Scroll up</p>
      <p>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
      </p>
    </section>
    <section>
      <h2>上にスクロールしてください</h2>
      <p>Scroll up</p>
      <p>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
      </p>
    </section>
    <section>
      <h2>上にスクロールしてください</h2>
      <p>Scroll up</p>
      <p>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
        Scroll up<br>
        上にスクロールしてください。<br>
      </p>
    </section>

  </div>

  <div class="position">
    <div class="position-abs">
      絶対位置:<span></span>
    </div>
    <div class="position-scrolled">
      現在の位置:<span> 0px</span>
    </div>
    <div class="position-desc">
       ※上の青線が<br>現在の位置
    </div>
  </div>

  <div class="currentPosition"></div>
              
            
!

CSS

              
                @charset "utf-8";

:root {
  font-size: 1vw;
}

body {
  background-color: #ffffeb;
}

.wrapper {
  width: 82%;
  margin: auto;
}

section {
  &:not(:last-child) {
    margin-bottom: 8rem;
  }
}

h1 {
  font-size: 3.4rem;
  padding-left: 0.5em;
  border-left: 1.2rem solid red;
  margin-top: 5rem;
  
  + p {
    font-size: 1.7rem;
    font-weight: bold;
  }
  
}

h2 {
  font-size: 2rem;
  margin-bottom: 1.3rem;
  
  padding-left: 0.5rem;
  border-left: 0.6rem solid red;
  + p {
    font-size: 1.4rem;
    font-weight: bold;
  }
}

p {
  font-size: 1.4rem;
  line-height: 1.8;
  max-width: 60%;
}

.target {
  width: 28%;
  margin-top: 3rem;
  background-color: orange;
  font-size: 2.4rem;
  text-align: center;
  font-weight: bold;
  padding: 5vw;
  border-top: 2rem solid red;
  margin-bottom: 5rem;
  
  > span {
    font-weight: normal;
    font-size: 1.4rem;
  }
}

.position {
  text-align: right;
  position: fixed;
  top: 3rem;
  right: 10%;
  padding: 1rem;
  font-size: 2.5rem;
  
  span {
    font-weight: bold;
  }

  &-abs {
    color: red;
  }
  
  &-desc {
    font-size: 1.6rem;
  }

}

.currentPosition {
  position: fixed;
  width: 100%;
  height: 2rem;
  background-color: blue;
  opacity: .6;
  top: 0;
  left: 0;
}
              
            
!

JS

              
                window.addEventListener('DOMContentLoaded', () => {
  //target
  const target = document.querySelector<HTMLDivElement>('.target')
  
  //main process
  const height = target!.getBoundingClientRect().top + window.scrollY
  
  //for display
  document.querySelector<HTMLSpanElement>('.position-abs > span')!.textContent = Math.round(height) + "px"
  const scrolled = document.querySelector<HTMLSpanElement>('.position-scrolled > span')

  //current position
  window.addEventListener('scroll', () => {
    scrolled!.textContent = String(Math.round(window.scrollY)) + "px"
  })
})
              
            
!
999px

Console