2018-06-14 admin


什么是parallax.js,Simple parallax scrolling effect inspired by implemented as a jQuery plugin


= 1.7).

<script src=""></script>
<script src="/path/to/parallax.min.js"></script>

Use these CDN links, provided by

<script src=""></script>
<script src=""></script>


Please note, that <!DOCTYPE html> on top of your document is required!

Simple version via data attributes

Note: for more complex requirements we recommend using the inner HTML markup below! With that it’s possible to use the parallax effect with almost any HTML content

To easily add a parallax effect behind an element, add data-parallax="scroll" to the element you want to use, and specify an image with data-image-src="/path/to/image.jpg".

<div class="parallax-window" data-parallax="scroll" data-image-src="/path/to/image.jpg"></div>

Via JavaScript

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

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


What parallax.js will do is create a fixed-position element for each parallax image at the start of the document’s body (or another configurable container). This mirror element will sit behind the other elements and match the position and dimensions of its target object.

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’t see anything.

.parallax-window {
	min-height: 400px;
	background: transparent;

Also, keep in mind that once initialized, the parallax plugin presumes a fixed page layout unless it encounters a scroll or resize event. If you have a dynamic page in which another javascript method may alter the DOM, you must manually refresh the parallax effect with the following commands:


Using inner HTML for complex content

You can use the following syntax to enable complex content for the parallax:

<div class="parallax-window">
  <div class="parallax-slider">
    <span style="position:absolute; top: 400px; left: 400px;">Some Text</span>
	<p>Some other Content</p>

Please note, that the div with class “parallax-slider” is essential here.

You then need to initialize it through JS and provide the naturalWidth and naturalHeight options in order to be rendered correctly.

    naturalWidth: 600,
    naturalHeight: 400

This also makes it possible to use responsive images in the slider:

<div class="parallax-window">
  <div class="parallax-slider">
    <img src="/path/to/image.jpg" srcset="/path/to/image-400px.jpg 400w, /path/to/image-800px.jpg 800w, /path/to/image-1200px.jpg 1200w" sizes="100vw">


转载请注明:文章转载自 JavaScript中文网 []