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="scrollTop"></div>

<!-- The stuff below is just for the demo --> 

<div class="example-box">
  <h1>Simple scrollTop example</h1>
  <p>Scroll down and press the button in the bottom right corner.</p>
  
  <h2>Nibh Vehicula</h2>
  <p>Donec sed odio dui. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam quis risus eget urna mollis ornare vel eu leo. Maecenas faucibus mollis interdum. Vestibulum id ligula porta felis euismod semper. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
  
  <h2>Nibh Vehicula</h2>
  <p>Donec sed odio dui. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam quis risus eget urna mollis ornare vel eu leo. Maecenas faucibus mollis interdum. Vestibulum id ligula porta felis euismod semper. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
  <h2>Nibh Vehicula</h2>
  
  <p>Donec sed odio dui. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam quis risus eget urna mollis ornare vel eu leo. Maecenas faucibus mollis interdum. Vestibulum id ligula porta felis euismod semper. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
  
  <h2>Nibh Vehicula</h2>
  <p>Donec sed odio dui. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam quis risus eget urna mollis ornare vel eu leo. Maecenas faucibus mollis interdum. Vestibulum id ligula porta felis euismod semper. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
  
  <h2>Nibh Vehicula</h2>
  <p>Donec sed odio dui. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam quis risus eget urna mollis ornare vel eu leo. Maecenas faucibus mollis interdum. Vestibulum id ligula porta felis euismod semper. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
  
  <h2>Nibh Vehicula</h2>
  <p>Donec sed odio dui. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam quis risus eget urna mollis ornare vel eu leo. Maecenas faucibus mollis interdum. Vestibulum id ligula porta felis euismod semper. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
  
  <h2>Nibh Vehicula</h2>
  <p>Donec sed odio dui. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam quis risus eget urna mollis ornare vel eu leo. Maecenas faucibus mollis interdum. Vestibulum id ligula porta felis euismod semper. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
  
  <h2>Nibh Vehicula</h2>
  <p>Donec sed odio dui. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam quis risus eget urna mollis ornare vel eu leo. Maecenas faucibus mollis interdum. Vestibulum id ligula porta felis euismod semper. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
  
  <h2>Nibh Vehicula</h2>
  <p>Donec sed odio dui. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam quis risus eget urna mollis ornare vel eu leo. Maecenas faucibus mollis interdum. Vestibulum id ligula porta felis euismod semper. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
  
  <h2>Nibh Vehicula</h2>
  <p>Donec sed odio dui. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam quis risus eget urna mollis ornare vel eu leo. Maecenas faucibus mollis interdum. Vestibulum id ligula porta felis euismod semper. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
  <h2>Nibh Vehicula</h2>
  <p>Donec sed odio dui. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam quis risus eget urna mollis ornare vel eu leo. Maecenas faucibus mollis interdum. Vestibulum id ligula porta felis euismod semper. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
  
  <h2>Nibh Vehicula</h2>
  <p>Donec sed odio dui. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam quis risus eget urna mollis ornare vel eu leo. Maecenas faucibus mollis interdum. Vestibulum id ligula porta felis euismod semper. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
              
            
!

CSS

              
                #scrollTop {
  
  /* Colors */
  @normal: #353535;
  @active: #FF8A00;
  @hover:  #999999;
  
  /* Size */
  @width: 20px;
  @margin: 10px;
  
  /* Positioning */
  position: fixed;
  right:    @margin;
  bottom:   @margin;
  z-index:  99999;
  display:  none;
  
  /* Make a triangle */
  border-color:   @normal transparent;
	border-style:   solid;
	border-width:   0px @width (@width+(@width/2)) @width;
	height:         0px;
	width:          0px;
  
  /* Awesome CSS3 animations */
  transition:         border-color 0.2s, bottom 0.8s;
  -moz-transition:    border-color 0.2s, bottom 0.8s;
  -webkit-transition: border-color 0.2s, bottom 0.8s;
  -o-transition:      border-color 0.2s, bottom 0.8s;
  
  &:hover {
     cursor:       pointer;
     border-color: @hover transparent; 
  }
  
  &.active {
     border-color: @active transparent;
     bottom:       (@margin+5);
  }
}
              
            
!

JS

              
                /* ==========================================================================
   Options
   ========================================================================== */

var maxWidth = 767,
    minWidth = 0,
    scrollTrigger = 100,
    scrollButton = $('#scrollTop'),
    alwaysVisible = false,
    scrollTriggered = false,
    widthTriggered = false;

/* ==========================================================================
   Events
   ========================================================================== */

$(window).resize(function() {
  if ($(window).width() >= maxWidth && !widthTriggered) {
    widthTriggered = true;
    toggleDisplay();
  } else if ($(window).width() < maxWidth && widthTriggered) {
    widthTriggered = false;
    toggleDisplay();
  }
});

$(window).scroll(function () {
  if ($(window).scrollTop() >= scrollTrigger && !scrollTriggered) {
    scrollTriggered = true;
    toggleDisplay();
  } else if ($(window).scrollTop() < scrollTrigger && scrollTriggered) {
    scrollTriggered = false;
    toggleDisplay();
  }
});

scrollButton.on('click', function() {
  // Boolean to prevent double execution of the complete-property
  var scrollDone = false,
      button = $(this);

  // CSS class for active state
  button.addClass('active');

  $("html, body").animate({
      scrollTop: "0"
    }, {duration: 800, complete : function(){
        if(!scrollDone){
          scrollDone = true;
          button.removeClass('active');
        }
      }
    }
  );
});

/* ==========================================================================
   Functions
   ========================================================================== */

function toggleDisplay() {
  if ($(window).width() < maxWidth && $(window).width() > minWidth && !alwaysVisible && scrollTriggered && !widthTriggered) {
    // If a correct maxWidth and minWidth is set
    scrollButton.fadeIn();
    console.log(1);
  } else if (!maxWidth || minWidth >= maxWidth || alwaysVisible) {
    // If values are wrong or alwaysVisible is true
    scrollButton.fadeIn();
    console.log(2);
  } else {
    // If no matches are found
    scrollButton.fadeOut();
    console.log(3);
  }
}
              
            
!
999px

Console