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

              
                <!DOCTYPE html>
<html>
  <head>
    <title>Scroll Header View</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
    <meta name="mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <style>
      html, body {
        font-family: sans-serif;
        margin: 0;
        overflow: hidden;
      }
      scroll-header-view {
        display: block;
        width: 100%;
        height: 100vh;
        --header-height-px: 192;
        --header-background-image: url("//gyazo.com/a779a158fce29e44b73b2af5a598d27a/raw");
        --header-background-color: #312319;
        --title-height-px: 64;
        --menus-height-px: 40;
        --sticky-y-px: 50;
      }
    </style>
    <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" />
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <style>
      .menus {
        position: absolute;
        right: 10px;
        height: 40px;
      }
      .material-icons {
        cursor: pointer;
        color: #eadfc6;
        margin: 8px 16px 0 6px;
      }
      .title {
        font-size: 32px;
        font-family: "roboto";
        margin-left: 70px;
        padding-top: 14px;
        color: #eadfc6;
      }
      .icon {
        width: 40px;
        height: 40px;
        left: 18px;
        background-size: cover;
        background-image: url("//gyazo.com/26f70a79063b1f8e642403eda2f3aefd/raw");
      }
    </style>
  </head>
  <body>
    <scroll-header-view gradation>
      <div slot="menus" class="menus">
        <i class="material-icons">fastfood</i>
        <i class="material-icons">account_box</i>
        <i class="material-icons">casino</i>
      </div>
      <div slot="icon" class="icon"></div>
      <div slot="title" class="title">Demo</div>
      <div slot="contents">
        <h1>SECTION A</h1>
        <h2>CONTENTS 0</h2>
        <h2>CONTENTS 1</h2>
        <h2>CONTENTS 2</h2>
        <h2>CONTENTS 3</h2>
        <h2>CONTENTS 4</h2>

        <h1>SECTION B</h1>
        <h2>CONTENTS 0</h2>
        <h2>CONTENTS 1</h2>
        <h2>CONTENTS 2</h2>
        <h2>CONTENTS 3</h2>
        <h2>CONTENTS 4</h2>

        <h1>SECTION C</h1>
        <h2>CONTENTS 0</h2>
        <h2>CONTENTS 1</h2>
        <h2>CONTENTS 2</h2>
        <h2>CONTENTS 3</h2>
        <h2>CONTENTS 4</h2>

        <h1>SECTION D</h1>
        <h2>CONTENTS 0</h2>
        <h2>CONTENTS 1</h2>
        <h2>CONTENTS 2</h2>
        <h2>CONTENTS 3</h2>
        <h2>CONTENTS 4</h2>

        <h1>SECTION E</h1>
        <h2>CONTENTS 0</h2>
        <h2>CONTENTS 1</h2>
        <h2>CONTENTS 2</h2>
        <h2>CONTENTS 3</h2>
        <h2>CONTENTS 4</h2>

        <h1>SECTION F</h1>
        <h2>CONTENTS 0</h2>
        <h2>CONTENTS 1</h2>
        <h2>CONTENTS 2</h2>
        <h2>CONTENTS 3</h2>
        <h2>CONTENTS 4</h2>

        <h1>SECTION G</h1>
        <h1>CONTENTS 7</h1>
        <h1>CONTENTS 8</h1>
        <h1>CONTENTS 9</h1>
      </div>
    </scroll-header-view>
    
    <script>
      const scrollHeaderView = document.querySelector('scroll-header-view')
      scrollHeaderView.addEventListener('click-icon', event => {
        const target = event.target
        target.scrollContainerTo(0)
        requestAnimationFrame(() => {
          if (target.solid) {
            target.removeAttribute('solid')
            target.setAttribute('gradation', '')
          } else {
            target.setAttribute('solid', '')
            target.removeAttribute('gradation')
          }
        })
      })
    </script>
  </body>
</html>

              
            
!

CSS

              
                
              
            
!

JS

              
                class ScrollHeaderView extends HTMLElement {
  constructor () {
    super()
    this.render()
    this.lastPosY = 0
    this.solid = false
    this.gradation = false
  }

  static get observedAttributes () {
    return ['solid', 'gradation']
  }

  attributeChangedCallback (attr, oldVal, newVal) {
    if (oldVal === newVal) return
    switch (attr) {
      case 'solid':
      case 'gradation':
        this[attr] = newVal === ''
        this.renderStyle()
        break
    }
  }

  getTemplate () {
    const t = document.createElement('template')
    t.innerHTML = `
      <div id='scroll-header-view'>
        <div id='header'>
          <div id='image'>
            <div class='background-image'></div>
          </div>
          <div id='grad'></div>
          <div id='menus'>
            <div id="icon">
              <slot name='icon'></slot>
            </div>
            <slot name='menus'></slot>
          </div>
          <div id='titlebar'>
            <slot name='title'></slot>
          </div>
        </div>
        <div id='container'>
          <slot name='contents'></slot>
          <div class='tail'></div>
        </div>
      </div>
    `
    return t
  }

  clearStyle () {
    if (this.header) this.header.style = ''
    if (this.titlebar) this.titlebar.style = ''
    if (this.grad) this.grad.style = ''
  }

  getStyle () {
    const t = document.createElement('template')
    let headerHeight = 'var(--header-height-px)'
    let imagePaddingBottom = 0
    if (this.solid) {
      headerHeight = '(var(--header-height-px) + var(--title-height-px))'
      imagePaddingBottom = 'var(--title-height-px)'
    }
    t.innerHTML = `
      <style>
        #header {
          position: absolute;
          background: #eee;
          top: 0;
          width: 100%;
          height: calc(${headerHeight} * 1px);
          z-index: 700;
        }
        #grad, #image {
          position: absolute;
          top: 0;
          width: 100%;
          height: 100%;
          background-color: var(--header-background-color, blue);
          opacity: 0;
        }
        #image {
          opacity: 1;
          padding-bottom: calc(${imagePaddingBottom} * 1px);
          box-sizing: border-box;
        }
        #image > .background-image {
          height: 100%;
          background-image: var(--header-background-image, none);
          background-size: cover;
          background-position: center center;
        }
        #menus {
          position: absolute;
          margin-top: 12px;
          width: 100%;
          top: 0;
          height: calc(var(--menus-height-px) * 1px);
          z-index: 900;
          user-select: none;
        }
        #icon {
          position: absolute;
          cursor: pointer;
          left: 18px;
        }
        #titlebar {
          background-color: var(--header-backgroind-color-default, transparent);
          position: absolute;
          bottom: 0;
          width: 100%;
          height: calc(var(--title-height-px) * 1px);
          z-index: 800;
        }
        #container {
          position: absolute;
          width: 100%;
          height: calc(100% - (${headerHeight} * 1px));
          padding-top: calc(${headerHeight} * 1px);
          top: 0;
          overflow: auto;
          z-index: 500;
        }
        .tail {
          visibility: hidden;
          width: 100%;
          height: 2px;
          background-color: #555;
        }
      </style>
    `
    this.clearStyle()
    return t
  }

  getCssVar (name, defaultValue) {
    return getComputedStyle(this.shadowRoot.firstElementChild).getPropertyValue(name).trim() || defaultValue
  }

  get titleHeight () {
    return parseFloat(this.getCssVar('--title-height-px'))
  }

  get headerHeight () {
    const headerHeight = parseFloat(this.getCssVar('--header-height-px'))
    return this.solid ? headerHeight + this.titleHeight : headerHeight
  }

  get headerBackgroundImageHeight () {
    return parseFloat(this.getCssVar('--header-background-image-height-px', this.headerHeight))
  }

  get headerBackgroundColor () {
    return this.getCssVar('--header-background-color')
  }

  get headerBackgroundColorDefault () {
    if (!this.solid) return 'transparent'
    return this.getCssVar('--header-background-color-default')
  }

  get stickyY () {
    return parseFloat(this.getCssVar('--sticky-y-px', 0))
  }

  onScroll () {
    const header = this.header
    const posY = this.container.scrollTop

    const diffY = posY - Math.min(this.lastPosY, this.headerHeight)
    const h = Math.min(Math.max(this.titleHeight, header.offsetHeight - diffY), this.headerHeight)
    header.style.height = `${h}px`

    if (h <= this.titleHeight) {
      this.titlebar.style.backgroundColor = this.headerBackgroundColor
    } else {
      this.titlebar.style.backgroundColor = this.headerBackgroundColorDefault
      if (this.headerHeight <= h) {
        this.grad.style.opacity = 0
      } else {
        if (this.gradation) {
          this.grad.style.opacity = (this.titleHeight / h) - 0.01 * (this.titleHeight / h)
        }
      }
    }

    // 追加した処理
    if (posY <= this.headerHeight + this.stickyY) {
      header.style.top = '0px'
    } else {
      const diffY = posY - this.lastPosY
      header.style.top = `${Math.min(Math.max(-this.titleHeight, header.offsetTop - diffY), 0)}px`
    }
    
    this.lastPosY = Math.max(0, posY)
  }

  onScrollEnd () {
    if (this.container.scrollTop <= 0) return
    this.dispatchEvent(new Event('scrollend'))
  }

  renderStyle () {
    const oldStyle = this.shadowRoot.querySelector('style')
    if (oldStyle) oldStyle.remove()
    this.shadowRoot.appendChild(this.getStyle().content.cloneNode(true))
  }

  render () {
    const shadowRoot = this.attachShadow({ mode: 'open' })
    this.renderStyle()
    shadowRoot.appendChild(this.getTemplate().content.cloneNode(true))

    this.container = shadowRoot.querySelector('#container')
    this.container.addEventListener('scroll', this.onScroll.bind(this))
    this.header = shadowRoot.querySelector('#header')
    this.titlebar = shadowRoot.querySelector('#titlebar')
    this.grad = shadowRoot.querySelector('#grad')

    const scrollObserver = new IntersectionObserver(this.onScrollEnd.bind(this), { threshold: 0.5 })
    scrollObserver.observe(shadowRoot.querySelector('.tail'))

    const icon = shadowRoot.querySelector('#icon')
    icon.addEventListener('click', () => {
      this.dispatchEvent(new Event('click-icon'))
    }, false)
  }

  scrollContainerTo (y) {
    this.container.scrollTo({
      top: y,
      behavior: 'smooth'
    })
  }

  setHeaderBackgroungImage (srcUrl) {
    requestAnimationFrame(() => {
      this.shadowRoot.getElementById('scroll-header-view').style.setProperty('--header-background-image', `url("${srcUrl}")`)
    })
  }
}

window.customElements.define("scroll-header-view", ScrollHeaderView)
              
            
!
999px

Console