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

              
                 <section>
      <div class="container">
        <div id="ios-notice" class="alert alert-info hidden"><p><strong>Notice:</strong> Hello mobile user. Unfortunately, this parallax effect is not reliably achieved on mobile devices, so all you will see on this demo page are static backgrounds.  Please visit this page on a desktop browser to see its full effect.</p></div>
        <h1>Simple Parallax Scrolling <small>parallax.js</small></h1>
        <p>Parallax.js is a dirt simple parallax scrolling effect inspired by <a href="http://spotify.com">Spotify.com</a> and implemented as a jQuery plugin.</p>
        <a href="https://github.com/pixelcog/parallax.js/archive/v1.4.2.zip" class="btn btn-default download-link">Download Parallax.js v1.4.2</a>
        <a href="https://github.com/pixelcog/parallax.js/" class="btn btn-default">View on GitHub</a>
      </div>
    </section>

    <div class="parallax-container" data-parallax="scroll" data-position="top" data-bleed="10" data-image-src="https://pixelcog.github.io/parallax.js/img/stellar-spire-eagle-nebula-1400x900.jpg" data-natural-width="1400" data-natural-height="900"><div class="container">
        <div id="ios-notice" class="alert alert-info hidden"><p><strong>Notice:</strong> Hello mobile user. Unfortunately, this parallax effect is not reliably achieved on mobile devices, so all you will see on this demo page are static backgrounds.  Please visit this page on a desktop browser to see its full effect.</p></div>
        <h1>Simple Parallax Scrolling <small>parallax.js</small></h1>
        <p>Parallax.js is a dirt simple parallax scrolling effect inspired by <a href="http://spotify.com">Spotify.com</a> and implemented as a jQuery plugin.</p>
        <a href="https://github.com/pixelcog/parallax.js/archive/v1.4.2.zip" class="btn btn-default download-link">Download Parallax.js v1.4.2</a>
        <a href="https://github.com/pixelcog/parallax.js/" class="btn btn-default">View on GitHub</a>
      </div></div>

    <section>
      <div class="container">
        <h2 id="installation">Installation</h2>

        <p>Download and include <code>parallax.min.js</code> in your document after including jQuery.</p>

        <pre><code>&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;/path/to/parallax.js&quot;&gt;&lt;/script&gt;
</code></pre>

        <h2 id="useage">Useage</h2>

        <h3 id="viadataattributes">Via data attributes</h3>

        <p>To easily add a parallax effect behind an element, add <code>data-parallax=&quot;scroll&quot;</code> to the element you want to use, and specify an image with <code>data-image-src=&quot;/path/to/image.jpg&quot;</code>.</p>

        <pre><code>&lt;div class=&quot;parallax-window&quot; data-parallax=&quot;scroll&quot; data-image-src=&quot;/path/to/image.jpg&quot;&gt;&lt;/div&gt;
</code></pre>

        <h3 id="viajavascript">Via JavaScript</h3>

        <p>To call the parallax plugin manually, simply select your target element with jQuery and do the following:</p>

        <pre><code>$('.parallax-window').parallax({imageSrc: '/path/to/image.jpg'});
</code></pre>

        <h3 id="underthehood">Under The Hood</h3>

        <p>What parallax.js will do is create a fixed-position element for each parallax image at the start of the document&#8217;s body. This mirror element will sit behind the other elements and match the position and dimensions of its target object.</p>

        <p>Due to the nature of this implementation, you must ensure that these parallax objects and any layers below them are transparent so that you can see the parallax effect underneath. Also, if there is no other content in this element, you will need to ensure that it has some fixed dimensions otherwise you won&#8217;t see anything.</p>

        <pre><code>.parallax-window {
    min-height: 400px;
    background: transparent;
}
</code></pre>
      </div>
    </section>

    <div class="parallax-container" data-parallax="scroll" data-bleed="10" data-speed="0.2" data-image-src="https://pixelcog.github.io/parallax.js/img/helix-nebula-1400x1400.jpg" data-natural-width="1400" data-natural-height="1400"></div>

    <section>
      <div class="container">
        <h2 id="options">Options</h2>
        <p>Options can be passed in via data attributes of JavaScript. For data attributes, append the option name to <code>data-</code>, as in <code>data-image-src=&quot;&quot;</code>.</p>

        <div class="alert alert-warning"><p>Note that when specifying these options as html data-attributes, you should convert &#8220;camelCased&#8221; variable names into &#8220;dash-separated&#8221; lower-case names (e.g. <code>zIndex</code> would be <code>data-z-index=&quot;&quot;</code>).</p></div>
        <div class="table-responsive">
          <table class="table table-bordered table-striped">
              <thead>
                  <tr>
                      <th style="width: 100px;">Name</th>
                      <th style="width: 100px;">type</th>
                      <th style="width: 50px;">default</th>
                      <th>description</th>
                  </tr>
              </thead>
              <tbody>
                  <tr>
                      <td>imageSrc</td>
                      <td>path</td>
                      <td>null</td>
                      <td>You must provide a path to the image you wish to apply to the parallax effect.</td>
                  </tr>
                  <tr>
                      <td>naturalWidth</td>
                      <td>number</td>
                      <td>auto</td>
                      <td rowspan="2">You can provide the natural width and natural height of an image to speed up loading and reduce error when determining the correct aspect ratio of the image.</td>
                  </tr>
                  <tr>
                      <td>naturalHeight</td>
                      <td>number</td>
                      <td>auto</td>
                  </tr>
                  <tr>
                      <td>position</td>
                      <td>xPos yPos</td>
                      <td>center center</td>
                      <td rowspan="3">This is analogous to the background-position css property. Specify coordinates as top, bottom, right, left, center, or pixel values (e.g. -10px 0px). The parallax image will be positioned as close to these values as possible while still covering the target element.</td>
                  </tr>
                  <tr>
                      <td>positionX</td>
                      <td>xPos</td>
                      <td>center</td>
                  </tr>
                  <tr>
                      <td>positionY</td>
                      <td>yPos</td>
                      <td>center</td>
                  </tr>
                  <tr>
                      <td>speed</td>
                      <td>float</td>
                      <td>0.2</td>
                      <td>The speed at which the parallax effect runs. 0.0 means the image will appear fixed in place, and 1.0 the image will flow at the same speed as the page content.</td>
                  </tr>
                  <tr>
                      <td>zIndex</td>
                      <td>number</td>
                      <td>-100</td>
                      <td>The z-index value of the fixed-position elements.  By default these will be behind everything else on the page.</td>
                  </tr>
                  <tr>
                      <td>bleed</td>
                      <td>number</td>
                      <td>0</td>
                      <td>You can optionally set the parallax mirror element to extend a few pixels above and below the mirrored element.  This can hide slow or stuttering scroll events in certain browsers.</td>
                  </tr>
                  <tr>
                      <td>iosFix</td>
                      <td>boolean</td>
                      <td>true</td>
                      <td>iOS devices are incompatable with this plugin. If true, this option will set the parallax image as a static, centered background image whenever it detects an iOS user agent. Disable this if you wish to implement your own graceful degradation.</td>
                  </tr>
                  <tr>
                      <td>androidFix</td>
                      <td>boolean</td>
                      <td>true</td>
                      <td>If true, this option will set the parallax image as a static, centered background image whenever it detects an Android user agent. Disable this if you wish to enable the parallax scrolling effect on Android devices.</td>
                  </tr>
              </tbody>
          </table>
        </div>
      </div>
    </section>

    <div class="parallax-container" data-parallax="scroll" data-bleed="10" data-image-src="https://pixelcog.github.io/parallax.js/img/spiral-galaxy-1400x900.jpg" data-natural-width="1400" data-natural-height="900"></div>

    <div class="parallax-container" data-parallax="scroll" data-bleed="10" data-image-src="https://pixelcog.github.io/parallax.js/img/reflection-nebula-750x763.jpg" data-natural-width="750" data-natural-height="763"></div>

              
            
!

CSS

              
                section {
  padding: 25px 0 35px;
  background: #fff;
}

.parallax-container {
  height: 500px;
  width: 100%;
}

pre {
  overflow: auto;
}

pre code {
  word-wrap: normal;
  white-space: pre;
}

footer {
  background: #000;
  color: #ddd;
  padding: 30px 0 30px;
  text-align: center;
}

footer a {
  color: #ddd;
  text-decoration: none;
}

footer a:hover {
  color: #eee;
  text-decoration: underline;
}

              
            
!

JS

              
                $(function(){
        if (navigator.userAgent.match(/(iPod|iPhone|iPad|Android)/)) {
          $('#ios-notice').removeClass('hidden');
          $('.parallax-container').height( $(window).height() * 0.5 | 0 );
        } else {
          $(window).resize(function(){
            var parallaxHeight = Math.max($(window).height() * 0.7, 200) | 0;
            $('.parallax-container').height(parallaxHeight);
          }).trigger('resize');
        }
      });


/*!
 * parallax.js v1.4.2 (https://pixelcog.github.io/parallax.js/)
 * @copyright 2016 PixelCog, Inc.
 * @license MIT (https://github.com/pixelcog/parallax.js/blob/master/LICENSE)
 */
!function(t,i,e,s){function o(i,e){var h=this;"object"==typeof e&&(delete e.refresh,delete e.render,t.extend(this,e)),this.$element=t(i),!this.imageSrc&&this.$element.is("img")&&(this.imageSrc=this.$element.attr("src"));var r=(this.position+"").toLowerCase().match(/\S+/g)||[];if(r.length<1&&r.push("center"),1==r.length&&r.push(r[0]),("top"==r[0]||"bottom"==r[0]||"left"==r[1]||"right"==r[1])&&(r=[r[1],r[0]]),this.positionX!=s&&(r[0]=this.positionX.toLowerCase()),this.positionY!=s&&(r[1]=this.positionY.toLowerCase()),h.positionX=r[0],h.positionY=r[1],"left"!=this.positionX&&"right"!=this.positionX&&(this.positionX=isNaN(parseInt(this.positionX))?"center":parseInt(this.positionX)),"top"!=this.positionY&&"bottom"!=this.positionY&&(this.positionY=isNaN(parseInt(this.positionY))?"center":parseInt(this.positionY)),this.position=this.positionX+(isNaN(this.positionX)?"":"px")+" "+this.positionY+(isNaN(this.positionY)?"":"px"),navigator.userAgent.match(/(iPod|iPhone|iPad)/))return this.imageSrc&&this.iosFix&&!this.$element.is("img")&&this.$element.css({backgroundImage:"url("+this.imageSrc+")",backgroundSize:"cover",backgroundPosition:this.position}),this;if(navigator.userAgent.match(/(Android)/))return this.imageSrc&&this.androidFix&&!this.$element.is("img")&&this.$element.css({backgroundImage:"url("+this.imageSrc+")",backgroundSize:"cover",backgroundPosition:this.position}),this;this.$mirror=t("<div />").prependTo("body");var a=this.$element.find(">.parallax-slider"),n=!1;0==a.length?this.$slider=t("<img />").prependTo(this.$mirror):(this.$slider=a.prependTo(this.$mirror),n=!0),this.$mirror.addClass("parallax-mirror").css({visibility:"hidden",zIndex:this.zIndex,position:"fixed",top:0,left:0,overflow:"hidden"}),this.$slider.addClass("parallax-slider").one("load",function(){h.naturalHeight&&h.naturalWidth||(h.naturalHeight=this.naturalHeight||this.height||1,h.naturalWidth=this.naturalWidth||this.width||1),h.aspectRatio=h.naturalWidth/h.naturalHeight,o.isSetup||o.setup(),o.sliders.push(h),o.isFresh=!1,o.requestRender()}),n||(this.$slider[0].src=this.imageSrc),(this.naturalHeight&&this.naturalWidth||this.$slider[0].complete||a.length>0)&&this.$slider.trigger("load")}function h(s){return this.each(function(){var h=t(this),r="object"==typeof s&&s;this==i||this==e||h.is("body")?o.configure(r):h.data("px.parallax")?"object"==typeof s&&t.extend(h.data("px.parallax"),r):(r=t.extend({},h.data(),r),h.data("px.parallax",new o(this,r))),"string"==typeof s&&("destroy"==s?o.destroy(this):o[s]())})}!function(){for(var t=0,e=["ms","moz","webkit","o"],s=0;s<e.length&&!i.requestAnimationFrame;++s)i.requestAnimationFrame=i[e[s]+"RequestAnimationFrame"],i.cancelAnimationFrame=i[e[s]+"CancelAnimationFrame"]||i[e[s]+"CancelRequestAnimationFrame"];i.requestAnimationFrame||(i.requestAnimationFrame=function(e){var s=(new Date).getTime(),o=Math.max(0,16-(s-t)),h=i.setTimeout(function(){e(s+o)},o);return t=s+o,h}),i.cancelAnimationFrame||(i.cancelAnimationFrame=function(t){clearTimeout(t)})}(),t.extend(o.prototype,{speed:.2,bleed:0,zIndex:-100,iosFix:!0,androidFix:!0,position:"center",overScrollFix:!1,refresh:function(){this.boxWidth=this.$element.outerWidth(),this.boxHeight=this.$element.outerHeight()+2*this.bleed,this.boxOffsetTop=this.$element.offset().top-this.bleed,this.boxOffsetLeft=this.$element.offset().left,this.boxOffsetBottom=this.boxOffsetTop+this.boxHeight;var t=o.winHeight,i=o.docHeight,e=Math.min(this.boxOffsetTop,i-t),s=Math.max(this.boxOffsetTop+this.boxHeight-t,0),h=this.boxHeight+(e-s)*(1-this.speed)|0,r=(this.boxOffsetTop-e)*(1-this.speed)|0;if(h*this.aspectRatio>=this.boxWidth){this.imageWidth=h*this.aspectRatio|0,this.imageHeight=h,this.offsetBaseTop=r;var a=this.imageWidth-this.boxWidth;this.offsetLeft="left"==this.positionX?0:"right"==this.positionX?-a:isNaN(this.positionX)?-a/2|0:Math.max(this.positionX,-a)}else{this.imageWidth=this.boxWidth,this.imageHeight=this.boxWidth/this.aspectRatio|0,this.offsetLeft=0;var a=this.imageHeight-h;this.offsetBaseTop="top"==this.positionY?r:"bottom"==this.positionY?r-a:isNaN(this.positionY)?r-a/2|0:r+Math.max(this.positionY,-a)}},render:function(){var t=o.scrollTop,i=o.scrollLeft,e=this.overScrollFix?o.overScroll:0,s=t+o.winHeight;this.boxOffsetBottom>t&&this.boxOffsetTop<=s?(this.visibility="visible",this.mirrorTop=this.boxOffsetTop-t,this.mirrorLeft=this.boxOffsetLeft-i,this.offsetTop=this.offsetBaseTop-this.mirrorTop*(1-this.speed)):this.visibility="hidden",this.$mirror.css({transform:"translate3d(0px, 0px, 0px)",visibility:this.visibility,top:this.mirrorTop-e,left:this.mirrorLeft,height:this.boxHeight,width:this.boxWidth}),this.$slider.css({transform:"translate3d(0px, 0px, 0px)",position:"absolute",top:this.offsetTop,left:this.offsetLeft,height:this.imageHeight,width:this.imageWidth,maxWidth:"none"})}}),t.extend(o,{scrollTop:0,scrollLeft:0,winHeight:0,winWidth:0,docHeight:1<<30,docWidth:1<<30,sliders:[],isReady:!1,isFresh:!1,isBusy:!1,setup:function(){if(!this.isReady){var s=t(e),h=t(i),r=function(){o.winHeight=h.height(),o.winWidth=h.width(),o.docHeight=s.height(),o.docWidth=s.width()},a=function(){var t=h.scrollTop(),i=o.docHeight-o.winHeight,e=o.docWidth-o.winWidth;o.scrollTop=Math.max(0,Math.min(i,t)),o.scrollLeft=Math.max(0,Math.min(e,h.scrollLeft())),o.overScroll=Math.max(t-i,Math.min(t,0))};h.on("resize.px.parallax load.px.parallax",function(){r(),o.isFresh=!1,o.requestRender()}).on("scroll.px.parallax load.px.parallax",function(){a(),o.requestRender()}),r(),a(),this.isReady=!0}},configure:function(i){"object"==typeof i&&(delete i.refresh,delete i.render,t.extend(this.prototype,i))},refresh:function(){t.each(this.sliders,function(){this.refresh()}),this.isFresh=!0},render:function(){this.isFresh||this.refresh(),t.each(this.sliders,function(){this.render()})},requestRender:function(){var t=this;this.isBusy||(this.isBusy=!0,i.requestAnimationFrame(function(){t.render(),t.isBusy=!1}))},destroy:function(e){var s,h=t(e).data("px.parallax");for(h.$mirror.remove(),s=0;s<this.sliders.length;s+=1)this.sliders[s]==h&&this.sliders.splice(s,1);t(e).data("px.parallax",!1),0===this.sliders.length&&(t(i).off("scroll.px.parallax resize.px.parallax load.px.parallax"),this.isReady=!1,o.isSetup=!1)}});var r=t.fn.parallax;t.fn.parallax=h,t.fn.parallax.Constructor=o,t.fn.parallax.noConflict=function(){return t.fn.parallax=r,this},t(e).on("ready.px.parallax.data-api",function(){t('[data-parallax="scroll"]').parallax()})}(jQuery,window,document);
              
            
!
999px

Console