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 class="header">
        <h1>Animated Scrolling Title</h1>
    </div>

    <div class="content">
        <p>scroll down to see the animation</p>
        <p>created by <a href="http://stevenfabre.com">steven fabre</a></p>
       

    </div>

              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Lato:300,900);




$backgroundBody: #ecf0f1;
$backgroundHeader: #2c3e50;
$primaryFont: 'Lato';

$titleColor: #e67e22;
$titleColorShadow: #D35400;
$titleFontSize: 3.7em;
$titlePadding: ($titleFontSize / 1.4) 0;


$paragraphColor: #34495e;

* {
  box-sizing:border-box;
}

html, body, div, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, ol, ul, li, form, fieldset, legend, label, table, header, footer, nav, section, figure {
  margin: 0;
  padding: 0;
}

body {
  background: $backgroundBody;
  min-height:100%;
  margin:0;
  position:relative;
  font-family:$primaryFont;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.header {
  background: $backgroundHeader;
  position:relative;
  padding:0 10%;
  overflow:hidden;

  h1 {
    text-transform: uppercase;
    font-size:$titleFontSize;
    padding: $titlePadding;
    color:$titleColor;
    text-align: center;
    text-shadow: 0px 2px 1px $titleColorShadow;

    span {
      display:inline-block;
    }
  }
}

.content {
  padding:2em 10% 80em 10%;
  p {
    color:$paragraphColor;
    font-size:2em;
    font-weight:300;
    text-align: center;
    margin:2em 0 0 0;
  }
  a {
    color:$titleColor;
    border-bottom:1px solid $titleColor;
    text-decoration: none;
    display:inline-block;
    position:relative;
    transition: all 0.2s;
    -webkit-transition: all 0.2s;
    -moz-transition: all 0.2s;
    -ms-transition: all 0.2s;
    -o-transition: all 0.2s;

    &:hover, &:focus {
      color:$titleColorShadow;
      border-bottom:1px solid $titleColorShadow;
    }
  }
}






ul.social {
  list-style-type: none;
  margin:0;
  padding:0;
  text-align:center;
  li {

    font-family: "fontello";
    font-style: normal;
    font-weight: normal;
    speak: none;
    font-size:1.5em;
    margin: 1em 0.5em;
    display:inline-block;
    position:relative;

    a {
      text-decoration:none;
      border:0;
      text-indent:9999px;
      overflow:hidden;
      position:relative;
      width:2em;
      height:2em;
      border-radius:50%;
      background:$titleColor;
      color:$backgroundBody;
      &:hover, &:focus {
        border:0;
        color:$backgroundBody;
        background:$titleColorShadow;
        top:2px;
      }
    }
  }
}


/* General Responsive */
@media all and (max-width: 40em) {
  body {
    font-size: 0.5em;
  }
}
@media all and (min-width: 40em) and (max-width: 60em) {
  body {
    font-size: 0.75em;
  }
}
@media all and (min-width: 60em) and (max-width: 80em) {
  body {
    font-size: 1em;
  }
}
@media all and (min-width: 80em) {
  body {
    font-size: 1.25em;
  }
}
              
            
!

JS

              
                
$(document).ready(function() {
    var elementToAnimate = $('.header h1');
    animateScrollingLetters(elementToAnimate);
});


/* Animate Letters of an element e when the user scrolls down */
function animateScrollingLetters(element) {
    wrapCharacters(element);
    animateSpanScrolling(element);
}


/* Wrap each character of an element with spans */
function wrapCharacters(element) {
    $(element).contents().each(function() {
        if(this.nodeType === 1) {
            wrapCharacters(this);
        }
        else if(this.nodeType === 3) {
            $(this).replaceWith($.map(this.nodeValue.split(''), function(c) {
                if (c != ' ') {
                    return '<span>' + c + '</span>';
                }
                else {
                    return ' ';
                }
            }).join(''));
        }
    });
}


function animateSpanScrolling(element) {

    var currentDistance = 0;
    var distanceToEndAnimation = element.parent().height();

    /* *****************     */
    /* Default variables     */
    /* Arrays = orgin to end */
    /* *****************     */

    /* Variable: Rotate [Default: 0] */
    var rotate = [0,0];

    /* Variable: Scale [Default: 1] */
    var scale = [1,1];

    /* Variable: Opacity [Default: 1] */
    var opacity = [1,1];

    /* Variable: Translate Array X,Y,Z axes [Default: 0,0,0] */
    var translate = [[0,0,0],[0,0,0]];


    /* ************************** */
    /* Random Target Variables    */
    /* End                        */
    /* ************************** */
    var minRotate = -90;
    var maxRotate = 90;
    var ratioRotate = (maxRotate - minRotate) / distanceToEndAnimation;

    var minScale = 0;
    var maxScale = 1;
    var ratioScale = (maxScale - minScale) / distanceToEndAnimation;

    var minOpacity = 0;
    var maxOpacity = 1;
    var ratioOpacity = (maxOpacity - minOpacity) / distanceToEndAnimation;


    var minTranslateX = -300;
    var maxTranslateX = 300;
    var ratioTranslateX = (maxTranslateX - minTranslateX) / distanceToEndAnimation;

    var minTranslateY = -200;
    var maxTranslateY =  0;
    var ratioTranslateY = (maxTranslateY - minTranslateY) / distanceToEndAnimation;


    // Generate a random end target value for each letter and save it in letter array
    var letter = new Array();
    element.children('span').each(function(i) {
        letter[i] = new Array();

        letter[i][0] = new Array();

        letter[i][0][0] = generateRandomNumber(minRotate,maxRotate,0);
        ratioRotate = (rotate[0] - letter[i][0][0]) / distanceToEndAnimation;

        letter[i][0][1] = generateRandomNumber(minScale,maxScale,2);
        ratioScale = (scale[0] - letter[i][0][1]) / distanceToEndAnimation;

        letter[i][0][2] = generateRandomNumber(minOpacity,maxOpacity,2);
        ratioOpacity = (opacity[0] - letter[i][0][2]) / distanceToEndAnimation;

        letter[i][0][3] = generateRandomNumber(minTranslateX,maxTranslateX,0);
        ratioTranslateX = (translate[0][0] - letter[i][0][3]) / distanceToEndAnimation;

        letter[i][0][4] = generateRandomNumber(minTranslateY,maxTranslateY,0);
        ratioTranslateY = (translate[0][1] - letter[i][0][4]) / distanceToEndAnimation;

        // If end value is negative, ratio in inverted, so it decreases instead of increases
        var ratioLetterRotate = ratioRotate;
        if (letter[i][0][0] < rotate[0]) {
            ratioLetterRotate = ratioRotate * -1;
        }
        var ratioLetterScale = ratioScale;
        if (letter[i][0][1] > scale[0]) {
            ratioLetterScale = ratioScale * -1;
        }
        var ratioLetterOpacity = ratioOpacity;
        if (letter[i][0][2] > opacity[0]) {
            ratioLetterOpacity = ratioOpacity * -1;
        }
        var ratioLetterTranslateX = ratioTranslateX;
        var ratioLetterTranslateY = ratioTranslateY;



        letter[i][1] = new Array();
        letter[i][1][0] = ratioLetterRotate;
        letter[i][1][1] = ratioLetterScale;
        letter[i][1][2] = ratioLetterOpacity;
        letter[i][1][3] = ratioLetterTranslateX;
        letter[i][1][4] = ratioLetterTranslateY;
    });



    $(document).on('scroll', function() {
        currentDistance = $(document).scrollTop();
        if (currentDistance < distanceToEndAnimation) {

            element.find('span').each(function(i) {

                var cssRotate = currentDistance * letter[i][1][0];
                var cssOpacity = opacity[0] * (opacity[0] - currentDistance * letter[i][1][2]);
                var cssTranslate = 'translate3d('+ currentDistance * letter[i][1][3] +'px,'+ currentDistance * letter[i][1][4] +'px, 0)';
                var cssRotate = 'rotate(' + cssRotate + 'deg)';
                var cssScale = 'scale('+ scale[0] * (scale[0] - currentDistance * letter[i][1][1]) +','+ scale[0] * (scale[0] - currentDistance * letter[i][1][1]) +')';
                var cssTransform = cssTranslate + ' ' + cssRotate + ' ' + cssScale;
                $(this).css({
                    'transform':cssTransform,
                    '-webkit-transform':cssTransform,
                    '-moz-transform':cssTransform,
                    'opacity': cssOpacity
                });
            });
        }

    });
}


/* Generate a Random Number  from a range between min and max, dec defines how many decimals you want */
function generateRandomNumber(min, max, dec) {
    var difference = max-min;
    //this code generates a random number between max and min
    var randomNumber = (difference*Math.random()) + min;
    var result = Math.round(randomNumber*Math.pow(10,dec))/Math.pow(10,dec);
    return result;
}
              
            
!
999px

Console