1

I want to go through each of the .clipped-box elements and pass them through the following function:

(genClips = function() {        
    // For easy use
    $t = $('.clipped-box');     
    // Like I said, we're using 5!
    var amount = 5;     
    // Get the width of each clipped rectangle.
    var width = $t.width() / amount;
    var height = $t.height() / amount;      
    // The total is the square of the amount
    var totalSquares = Math.pow(amount, 2);     
    // The HTML of the content
    var html = $t.find('.content').html();  
    var y = 0;

    for(var z = 0; z <= (amount*width); z = z+width) { 

        $('<div class="clipped" style="clip: rect('+y+'px, '+(z+width)+'px, '+(y+height)+'px, '+z+'px)">'+html+'</div>').appendTo($t);

        if(z === (amount*width)-width) {

            y = y + height;
            z = -width;

        }

        if(y === (amount*height)) {
            z = 9999999;
        }

    }

})();

I have several .clipped-box elements in the HTML as follows:

<div class="clipped-box piece1">
        <div class="content">
            <img src="img/piece1.png">
        </div>      
    </div>

    <div class="clipped-box piece2">
        <div class="content">
            <img src="img/piece2.png">
        </div>      
    </div>

How can I adapt it to this?

4 Answers 4

2

you can do:

$('.clipped-box').each(function(){
    var $box = $(this);
    // implement everything here for this single box.
});

that way, you can have multiple boxes all contained in their own scope

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the .each() function in jQuery. Documentation

genClips = function(item) {        
    // For easy use
    $t = item;
    ....
};

$('.clipped-box').each(function(){
    var $box = $(this);
    genClips($box);
});

2 Comments

so why not just: $('.clipped-box').each(genClips); ?
There are definitely a ton of different ways to use the $.each() and his function.
1

Try it like this

(genClips = function () {
    $('.clipped-box').each(function () { <-- use each here
        $t = $(this);
        var amount = 5;
        var width = $t.width() / amount;
        var height = $t.height() / amount;
        var totalSquares = Math.pow(amount, 2);
        var html = $t.find('.content').html();
        var y = 0;
        for (var z = 0; z <= (amount * width); z = z + width) {
            $('<div class="clipped" style="clip: rect(' + y + 'px, ' + (z + width) + 'px, ' + (y + height) + 'px, ' + z + 'px)">' + html + '</div>').appendTo($t);
            if (z === (amount * width) - width) {
                y = y + height;
                z = -width;
            }

            if (y === (amount * height)) {
                z = 9999999;
            }
        }
    });
})();

Demo

Comments

1

Invoking your jQuery Method like this? http://jsbin.com/zafor/2/edit

$('.clipped-box').genClips( 5 );

All you need:

$.fn.genClips = function( amount ){
    return this.each(function(){
        var $t = $(this),
            width = $t.width() / amount,
            height = $t.height() / amount,      
            totalSquares = Math.pow(amount, 2),
            html = $t.find('.content').html(),
            y = 0;
        for(var z = 0; z <= (amount*width); z = z+width) { 
            $('<div class="clipped" style="clip: rect('+y+'px, '+(z+width)+'px, '+(y+height)+'px, '+z+'px)">'+html+'</div>').appendTo($t);
            if(z === (amount*width)-width) {
                y = y + height;
                z = -width;
            }
            if(y === (amount*height)) {
                z = 9999999;
            }
        }
    });
};

any way... what do you use totalSquares for? What should be actually 9999999?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.