0

Setting up parallax.js I ran into this:

The html source markup (in the official GIThub project) is:

    <ul id="scene" class="scene">
        <li class="layer" data-depth="1.00"><img src="images/layer1.png"></li>
        <li class="layer" data-depth="0.80"><img src="images/layer2.png"></li>
        <li class="layer" data-depth="0.60"><img src="images/layer3.png"></li>
        <li class="layer" data-depth="0.40"><img src="images/layer4.png"></li>
        <li class="layer" data-depth="0.20"><img src="images/layer5.png"></li>
        <li class="layer" data-depth="0.00"><img src="images/layer6.png"></li>
        </ul>

But if we look at the (really awesome) Wagerfield example it looks like the images are brought in as a background image of a div block inside each .layer class element.

The explanation I can give myself is that in this way the image can be controlled more flexibly,


I'm just starting out with JS so I really can't find a workaround: but couldn't the script transform this

    <img src="images/layer1.png">

into something like:

    <div class="layer1" style="background:url(images/layer1.png) no-repeat 50% 100%"></div>

Can this be achieved easily?

Extending the question it would be cool to entirely style the .layer with inline styles passed through the script.

Script so far:

     jQuery("li.layer > img").replaceWith
    ("<div class=\"" + $((("li.layer > img").src).replace(/\.[^/.]+$/, "")) + "\"</div>" );

this part isn't working (i'm looking for the url output of images..):

    (("li.layer > img").src).replace(/\.[^/.]+$/, "")
maioman
  • 18,154
  • 4
  • 36
  • 42
  • 1
    Have you tried something yourself so far? Can you post the code here? I recommend using http://api.jquery.com/replacewith/ – Spokey Sep 18 '14 at 11:16
  • I can't get the output for the img url ; `jQuery("li.layer > img").replaceWith("li.layer > img").attr("src");` with this code I get every image replaced with `li.layer > img;` and not the url inside it.... – maioman Sep 19 '14 at 08:05

1 Answers1

1

Something like this should do the trick.

$('#scene > .layer > img').replaceWith(function () { // select all images
    var s = this.src.substring(this.src.indexOf('images'), this.src.length);
    // get only the relative path of the url
    
  
    return $('<div/>', { // create a div
        class: s.replace(/\.[a-zA-Z]+/, '').split('/')[1], 
               // get the image name as class
        style: 'background:url(' + s + ') no-repeat 50% 100%' 
               // set style
    });
});

$('div').text(function(){
 return this.outerHTML;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<ul id="scene" class="scene">
    <li class="layer" data-depth="1.00">
        <img src="images/layer1.png">
    </li>
    <li class="layer" data-depth="0.80">
        <img src="images/layer2.png">
    </li>
    <li class="layer" data-depth="0.60">
        <img src="images/layer3.png">
    </li>
    <li class="layer" data-depth="0.40">
        <img src="images/layer4.png">
    </li>
    <li class="layer" data-depth="0.20">
        <img src="images/layer5.png">
    </li>
    <li class="layer" data-depth="0.00">
        <img src="images/layer6.png">
    </li>
</ul>
Spokey
  • 10,974
  • 2
  • 28
  • 44