不错的滚动视差效果不起作用

时间:2016-05-24 00:26:55

标签: javascript html css

这是指向该网站的链接:http://codepen.io/zabielski/pen/MyoBaY/

我按照本网站的说明和代码尝试获得类似的滚动视差效果。下面是我的HTML,CSS和JavaScript代码(基本上只是复制,但有我自己的图像和东西)。

<!DOCTYPE html>
<html lang="en">
<!--Meta equiv for the whole web page to be emulated into IE straight from the chrome styling, If this is not done, then the whole webpage is green instead of the gray and orange and the text on the opage is shoved onto the far left side of the page.-->
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7"/>
<!--Link to Favicon-->
<link rel="shortcut icon" type="image/x-icon" href="Favicon/Ulearn.ico">
<!--Links to CSS style sheet-->
<link rel="stylesheet" type="text/css" href="CSS style sheets/Main style sheet.css">
<link rel="stylesheet" type="text/css" href="CSS style sheets/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="CSS style sheets/animate.min.css">
<!--Links to JavaScript external sheets-->
<script type="text/javascript" src="JavaScript sheets/Main javascript.js"></script>
<script type="text/javascript" src="JavaScript sheets/jquery-2.2.3.js"></script>
<head>
<span class="header">Physics Level 3</span>
<!--Meta and links to make sure the webpage is dynamic-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href='https://fonts.googleapis.com/css?family=Playfair+Display' rel='stylesheet' type='text/css'>
</head>
<body>
<div id='hero'>
  <div class='layer-1 layer' data-depth='0.20' data-type='parallax'></div>
  <div class='layer-2 layer' data-depth='0.50' data-type='parallax'></div>
  <div class='layer-3 layer' data-depth='0.80' data-type='parallax'></div>
  <div class='layer-4 layer' data-depth='1.00' data-type='parallax'></div>
</div>
<div id="content">




</div>
</body>
</html>

#hero {
  height: 1000px;
  overflow: hidden;
  position: relative;
}
.header {
    background-color: white;
    border-style: solid;
    border-width: 0px;
    position: absolute;
    text-align: left;
    left: 1%;
    top: 2.4%;
    width: 97%;
    padding-top: 1%;
    padding-bottom: 1%;
    padding-left: 0.7%;
    padding-right: 0.2%;
    z-index: 1;
}
.layer {
    background-position: center;
    background-size: auto;
    width: 100%;
    height: 800px;
    position: fixed;
    z-index: -1;
}
.layer-1 {
    background-image: url("background-last.jpg");
}
.layer-2 {
    background-image: url("background-middle.png");
    background-repeat: no-repeat;
    background-position: center;
}
.layer-3 {
    background-image: url("background-top.png");
    background-repeat: no-repeat;
}
.layer-4 {
    background-image: url("background-verytop.png");
}


(function () {
    window.addEventListener('scroll', function (event) {
        var depth, i, layer, layers, len, movement, topDistance, translate3d;
        topDistance = this.pageYOffset;
        layers = document.querySelectorAll('[data-type=\'parallax\']');
        for (i = 0, len = layers.length; i < len; i++) {
            if (window.CP.shouldStopExecution(1)) {
                break;
            }
            layer = layers[i];
            depth = layer.getAttribute('data-depth');
            movement = -(topDistance * depth);
            translate3d = 'translate3d(0, ' + movement + 'px, 0)';
            layer.style['-webkit-transform'] = translate3d;
            layer.style['-moz-transform'] = translate3d;
            layer.style['-ms-transform'] = translate3d;
            layer.style['-o-transform'] = translate3d;
            layer.style.transform = translate3d;
        }
        window.CP.exitedLoop(1);
    });
}.call(this));

1 个答案:

答案 0 :(得分:1)

你不能复制并粘贴所有内容,你必须格式化 - 代码是专门编译的。

这是格式化的RAW页面:

<html class="animated fadeIn">
<head>
    <script src="//assets.codepen.io/assets/editor/live/console_runner-d0a557e5cb67f9cd9bbb9673355c7e8e.js"></script><script src="//assets.codepen.io/assets/editor/live/events_runner-21174b4c7273cfddc124acb0876792e0.js"></script><script src="//assets.codepen.io/assets/editor/live/css_live_reload_init-7618a0de08795409d8f6c9ef6805f7b2.js"></script>
    <meta charset="UTF-8">
    <meta name="robots" content="noindex">
    <link rel="canonical" href="http://codepen.io/zabielski/pen/MyoBaY/">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://fonts.googleapis.com/css?family=Playfair+Display" rel="stylesheet" type="text/css">
    <link rel="stylesheet prefetch" href="//codepen.io/assets/reset/normalize.css">
    <link rel="stylesheet prefetch" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet prefetch" href="//cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.3/animate.min.css">
    <style class="cp-pen-styles">body {
        padding: 0;
        margin: 0;
        background-color: #130d0a;
        font-family: 'Playfair Display', serif;
        color: #fff;
        }
        #hero {
        height: 800px;
        overflow: hidden;
        position: relative;
        }
        #content {
        background-color: #130d0a;
        }
        .layer {
        background-position: bottom center;
        background-size: auto;
        background-repeat: no-repeat;
        width: 100%;
        height: 800px;
        position: fixed;
        z-index: -1;
        }
        #hero-mobile {
        display: none;
        background: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/272781/full_illustration.png") no-repeat center bottom/cover;
        height: 320px;
        }
        .first-section {
        padding: 50px 0 20px 0;
        }
        .text-header {
        font-size: 50px;
        text-align: center;
        }
        h1 {
        line-height: 120%;
        margin-bottom: 30px;
        }
        p {
        color: #ede0d5;
        font-size: 18px;
        line-height: 150%;
        }
        #hero, .layer {
        min-height: 800px;
        }
        .layer-bg {
        background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/272781/ilu_bg.jpg");
        }
        .layer-1 {
        background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/272781/ilu_03.png\a         ");
        background-position: left bottom;
        }
        .layer-2 {
        background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/272781/ilu_02.png");
        }
        .layer-3 {
        background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/272781/ilu_man.png\a        ");
        background-position: right bottom;
        }
        .layer-4 {
        background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/272781/ilu_01.png\a         ");
        }
        .layer-overlay {
        background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/272781/ilu_overlay.png\a        ");
        }
        @media only screen and (max-width: 768px) {
        #hero {
        display: none;
        }
        #hero-mobile {
        display: block;
        }
        }
        .tutorial-link {
        color: #fff;
        font-size: 18px;
        text-decoration: underline;
        }
        .tutorial-link:hover {
        color: #ede0d5;
        }
    </style>
</head>
<body>
    <div id="hero">
        <div class="layer-bg layer" data-depth="0.10" data-type="parallax"></div>
        <div class="layer-1 layer" data-depth="0.20" data-type="parallax"></div>
        <div class="layer-2 layer" data-depth="0.50" data-type="parallax"></div>
        <div class="layer-3 layer" data-depth="0.80" data-type="parallax"></div>
        <div class="layer-overlay layer" data-depth="0.85" data-type="parallax"></div>
        <div class="layer-4 layer" data-depth="1.00" data-type="parallax"></div>
    </div>
    <div id="hero-mobile"></div>
    <div id="content">
        <div class="container">
            <section class="first-section">
                <div class="row">
                    <div class="col-sm-6">
                        <h1>You cannot hide the soul. Through all his unearthly tattooings, I thought I saw the traces of a simple honest heart.</h1>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <p>And besides all this, there was a certain lofty bearing about the Pagan, which even his uncouthness could not altogether maim. He looked like a man who had never cringed and never had had a creditor. Whether it was, too, that his head being shaved, his forehead was drawn out in freer and brighter relief, and looked more expansive than it otherwise would, this I will not venture to decide; but certain it was his head was phrenologically an excellent one.</p>
                        <p>It may seem ridiculous, but it reminded me of General Washington's head, as seen in the popular busts of him. It had the same long regularly graded retreating slope from above the brows, which were likewise very projecting, like two long promontories thickly wooded on top. Queequeg was George Washington cannibalistically developed.</p>
                        <p>Whilst I was thus closely scanning him, half-pretending meanwhile to be looking out at the storm from the casement, he never heeded my presence, never troubled himself with so much as a single glance; but appeared wholly occupied with counting the pages of the marvellous book. Considering how sociably we had been sleeping together the night previous, and especially considering the affectionate arm I had found thrown over me upon waking in the morning, I thought this indifference of his very strange. But savages are strange beings; at times you do not know exactly how to take them.</p>
                    </div>
                    <div class="col-sm-6">
                        <p>At first they are overawing; their calm self-collectedness of simplicity seems a Socratic wisdom. I had noticed also that Queequeg never consorted at all, or but very little, with the other seamen in the inn. He made no advances whatever; appeared to have no desire to enlarge the circle of his acquaintances. All this struck me as mighty singular; yet, upon second thoughts, there was something almost sublime in it. Here was a man some twenty thousand miles from home, by the way of Cape Horn, that is—which was the only way he could get there—thrown among people as strange to him as though he were in the planet Jupiter; and yet he seemed entirely at his ease; preserving the utmost serenity; content with his own companionship; always equal to himself.</p>
                        <p>Here was a man some twenty thousand miles from home, by the way of Cape Horn, that is—which was the only way he could get there—thrown among people as strange to him as though he were in the planet Jupiter; and yet he seemed entirely at his ease; preserving the utmost serenity; content with his own companionship; always equal to himself. Surely this was a touch of fine philosophy; though no doubt he had never heard there was such a thing as that.</p>
                        <a class="tutorial-link" href="https://medium.com/@PatrykZabielski/how-to-make-multi-layered-parallax-illustration-with-css-javascript-2b56883c3f27">
                        Learn how to create this parallax effect
                        </a>
                    </div>
                </div>
            </section>
        </div>
    </div>
    <script src="//assets.codepen.io/assets/common/stopExecutionOnTimeout.js?t=1"></script>
    <script>(function () {
        window.addEventListener('scroll', function (event) {
            var depth, i, layer, layers, len, movement, topDistance, translate3d;
            topDistance = this.pageYOffset;
            layers = document.querySelectorAll('[data-type=\'parallax\']');
            for (i = 0, len = layers.length; i < len; i++) {
                if (window.CP.shouldStopExecution(1)) {
                    break;
                }
                layer = layers[i];
                depth = layer.getAttribute('data-depth');
                movement = -(topDistance * depth);
                translate3d = 'translate3d(0, ' + movement + 'px, 0)';
                layer.style['-webkit-transform'] = translate3d;
                layer.style['-moz-transform'] = translate3d;
                layer.style['-ms-transform'] = translate3d;
                layer.style['-o-transform'] = translate3d;
                layer.style.transform = translate3d;
            }
            window.CP.exitedLoop(1);
        });
        }.call(this));
        //# sourceURL=pen.js
    </script>
</body>
</html>