首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >元素通过函数jquery

元素通过函数jquery
EN

Stack Overflow用户
提问于 2014-08-12 15:05:22
回答 3查看 49关注 0票数 1

我想遍历每一个..clipped box元素,并通过以下函数传递它们:

代码语言:javascript
复制
(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;
        }

    }

})();

HTML中有几个.clipped-box元素,如下所示:

代码语言:javascript
复制
<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>

我怎么才能适应这种情况呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-08-12 15:07:42

你可以:

代码语言:javascript
复制
$('.clipped-box').each(function(){
    var $box = $(this);
    // implement everything here for this single box.
});

这样,您就可以将多个框全部包含在它们自己的作用域中。

票数 2
EN

Stack Overflow用户

发布于 2014-08-12 15:09:40

您可以在.each()函数中使用jQuery。文档

代码语言:javascript
复制
genClips = function(item) {        
    // For easy use
    $t = item;
    ....
};

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

Stack Overflow用户

发布于 2014-08-12 15:13:16

像这样试试

代码语言:javascript
复制
(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;
            }
        }
    });
})();

演示

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25267811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档