Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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 id="page">

  <h1>This is a demo for the isiaToTop plugin<br />
    <span>
    <iframe src="https://ghbtns.com/github-btn.html?user=vurghus-minar&repo=isiaToTop&type=fork&count=false&size=large" frameborder="0" scrolling="0" width="158px" height="30px"></iframe>
    </span>
  </h1>

<p><strong>Pellentesque habitant morbi tristique</strong> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. <a href="#">Donec non enim</a> in turpis pulvinar facilisis. Ut felis.</p>

<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>

<ul>
   <li>Morbi in sem quis dui placerat ornare. Pellentesque odio nisi, euismod in, pharetra a, ultricies in, diam. Sed arcu. Cras consequat.</li>
   <li>Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus.</li>
   <li>Phasellus ultrices nulla quis nibh. Quisque a lectus. Donec consectetuer ligula vulputate sem tristique cursus. Nam nulla quam, gravida non, commodo a, sodales sit amet, nisi.</li>
   <li>Pellentesque fermentum dolor. Aliquam quam lectus, facilisis auctor, ultrices ut, elementum vulputate, nunc.</li>
</ul>

<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>

<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>

<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
</div>
              
            
!

CSS

              
                #isiaToTop {
	position: fixed;
	right: 20px;
	bottom: 20px;
	float: right;
	width: 20px;
	height: 20px;
	padding: 10px;
	cursor: pointer;
	-webkit-animation: toTopFadeIn 2s linear;
	animation: toTopFadeIn 2s linear;
	opacity: 1;
	color: #fff;
	border: 3px solid #fff;
	background: #d13317;
}

#isiaToTop:hover {
	background: #df4624;
}

@-webkit-keyframes toTopFadeIn {

0% {
	opacity: 0;
}

100% {
	opacity: 1;
}

}

@keyframes toTopFadeIn {

0% {
	opacity: 0;
}

100% {
	opacity: 1;
}

}

/**
DEMO STYLES
*/

body{
  background-color: #e23849;
}

#page {
  font-family: "Helvetica Neue", Arial, sans-serif;
  width: 100%;
  max-width: 600px;
	margin: 40px auto;
	padding: 20px;
  background-color: beige;
  box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.75);
}

#page h1 {
	text-align: center;
}

#page h1 span iframe{
  margin-top: 20px;
}
              
            
!

JS

              
                /**
 * Isia Scroll to top plugin.
 * 
 * Plugin is used mainly as back to top click button that shows up once the body has been scrolled a specified height.
 * 
 * @link https://github.com/vurghus-minar/isiaToTop
 * 
 */
(function (root, factory) {

  if (typeof define === 'function' && define.amd) {
      // AMD. Register as an anonymous module.
      define([], factory());
  } else if (typeof module === 'object' && module.exports) {
      // Node. Does not work with strict CommonJS, but
      // only CommonJS-like environments that support module.exports,
      // like Node.
      module.exports = factory();
  } else {
      // Browser globals (root is window)
      root.isiaToTop = factory();
  }
}(typeof self !== 'undefined' ? self : this, function () {

  'use strict'
  const body = document.body
  const html = document.documentElement

  const windowHeight = window.innerHeight || html.clientHeight || body.clientHeight
  const documentHeight = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight)

  const defaults = {
    toTopElementTemplate: `<div id="isiaToTop" style="display: none">
                              <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAMAAAC6V+0/AAAAVFBMVEUAAAD///////////////////////////////////////////////////////////////////////////////////////////////////////////8wXzyWAAAAG3RSTlMAAQIgJic0N0BHUVVZW1xdYm9wcXeFlZ2rrf2L0Jb5AAAAXElEQVQYV7XNRw6AMBBDURN673Xuf09IoiGjwBL+xtLbGPimYOmethH1nqnLfLVGNLyYVLUT+WrtiCqhbEB9qzPWEVidAY3RApO2mD9bjSkwCzOa6y1DiLIEv3QCJfQLDOsEaRoAAAAASUVORK5CYII=" alt="Scroll to top">
                          </div>`,
    scrollStartPosition: 50,
    toTopElement: 'isiaToTop',
    scrollAnimationSpeed: 500,
    onClick: null, // callback overrides _onClick method
    onScroll: null // callback appends to the _onScroll method
  }

  let settings, toTopElement

  function _extend (target, defaults) {
    for (let i = 1; i < arguments.length; i++) {
      for (let key in arguments[i]) {
        if (arguments[i].hasOwnProperty(key)) {
          arguments[0][key] = arguments[i][key]
        }
      }
    }
    return arguments[0]
  }

  /**
   * Initiate the plugin
   */
  function _init (options) {
    settings = _extend({}, defaults, options)
    _bindEvents() 
  }

  /**
   * Bind all events
   */
  function _bindEvents () {
    _onPageLoad()
    _onScroll()
    _onClick()
  }

  /**
   * Triggered during DOM load
   */
  function _onPageLoad(){
    const toTopElementTemplate = settings.toTopElementTemplate
    if(typeof toTopElementTemplate === 'function'){
      toTopElementTemplate.call(this)
    } else if (typeof toTopElementTemplate === 'string') {
      body.insertAdjacentHTML('beforeend', toTopElementTemplate)
    }
    toTopElement = document.getElementById(settings.toTopElement)
  }

  /**
   * _onScroll method runs on page scroll.
   */
  function _onScroll () {
    window.addEventListener('scroll', function () {
      const scrollPos = window.scrollY || window.scrollTop || document.getElementsByTagName('html')[0].scrollTop

      if (documentHeight >= windowHeight) {
        scrollPos > settings.scrollStartPosition ? _showToTopElement() : _hideToTopElement()
      }

      if (typeof settings.onScroll === 'function'){
        settings.onScroll.call(this)
      }

    })
  }

  /**
   * _onClick method runs when element is clicked.
   */
  function _onClick () {
    toTopElement.addEventListener('click', function () {
      if (typeof settings.onClick === 'function') {
        settings.onClick.call(this)
      } else {
        _onToTop(html, 0, settings.scrollAnimationSpeed)
      }
    }) 
  }

  /**
   *  Function triggered on scroll event.
   */
  function _scroll(){
    if(document.getElementById(settings.toTopElement).length !== 0){
      _onToTop(html, 0, settings.scrollAnimationSpeed)
    }
  }

  /**
   * Activates when the page is scrolled or element is clicked.
   * 
   * @param {object} element - DOM element to scroll.
   * @param {integer} to - starting point of animation in milliseconds.
   * @param {integer} duration - speed of animation.
   */
  function _onToTop (element, to, duration) {
    let start = element.scrollTop
    let change = to - start
    let currentTime = 0
    let increment = 20

    // Scrolling animation
    const __animateScroll = function () {
      currentTime += increment
      const val = __easeInOutQuad(currentTime, start, change, duration)
      element.scrollTop = val
      if (currentTime < duration) {
        setTimeout(__animateScroll, increment)
      }
    }

    // t = current time
    // b = start value
    // c = change in value
    // d = duration
    const __easeInOutQuad = function (t, b, c, d) {
      t /= d / 2
      if (t < 1) return c / 2 * t * t + b
      t--
      return -c / 2 * (t * (t - 2) - 1) + b
    }

    __animateScroll()
  }

  /**
   * Display the ToTopElement
   */
  function _showToTopElement () {
    toTopElement.setAttribute('style', 'display:block')
  }

  /**
   * Hide the ToTopElement
   */
  function _hideToTopElement () {
    toTopElement.setAttribute('style', 'display: none;')
  }

  /**
   * Plugin methods to expose publicly.
   */
  return {
    active: _init,
    scroll: _scroll
  }

}))


////////////DEMO SET UP/////////////////
const options = {
  scrollStartPosition: 20,
  scrollAnimationSpeed: 1000,
  onClick: overrideClick,
  onScroll: function () {
    console.log('scrolled')
  }
}

isiaToTop.active(options)

function overrideClick(){
  console.log('bla')
  isiaToTop.scroll()  
}
              
            
!
999px

Console