Jquery文件不会为元素分配随机高度和宽度

时间:2012-09-24 17:08:45

标签: jquery

正如标题所说:这不起作用。该脚本会移动我想要移动的文章,如果我将选项放到选择器上,它也会动画它,但它不会随机化 heightwidth的元素。所有元素的随机宽度为127px,我无法理解。提到脚本的上述php文件如下:

更新:

    <script type="text/javascript" src="<? bloginfo('stylesheet_directory'); ?>/js/jquery.freetile.min.js"></script>
    <script>
    jQuery(document).ready(function() {

    for (i=0;i<40;i++) {
        var w = 128 * (parseInt(Math.random() * 3) + 1) - 1,h = 128 * (parseInt(Math.random() * 3) + 1) - 1;
    jQuery('article').width(w).height(h);
    }

        jQuery('#content').freetile();
         selector: 'article';
         animate: true;
         elementDelay: 10
        });
    </script>

这部分有问题吗?这很奇怪,因为我从github存储库中复制并粘贴它。

    for (i=0;i<40;i++) {
        var w = 128 * (parseInt(Math.random() * 3) + 1) - 1,h = 128 * (parseInt(Math.random() * 3) + 1) - 1;
    jQuery('article').width(w).height(h);
    }

以下是文件的链接: https://github.com/yconst/Freetile/blob/master/js/init.js

感谢ComputerArts,我接受了你的回答:)

2 个答案:

答案 0 :(得分:1)

随机化哪些元素的宽度和高度?您的问题可能是您没有将任何内容传递给jQuery()

// What are you trying to set the width/height of?
jQuery('').width(w).height(h)

可能应该是

之类的东西
jQuery('#content .selector-for-tiled-divs').width(w).height(h)...

答案 1 :(得分:1)

让我们解释你的代码

for (i=0;i<40;i++) {
    var w = 64 * (parseInt(Math.random() * 3) + 1) - 1,
    h = 48 * (parseInt(Math.random() * 3) + 1) - 1;

    //this part here is where the error happens.
    //you're asking to set the width and height to every element <content> on each iteration.
    //so on the last iteration, all the elements have the same width/height
    jQuery('content').width(w).height(h).appendTo('article');
}

我相信this就是你想要的。

jQuery(document).ready(function() {
    for (i=0;i<40;i++) {
        var w = 64 * (parseInt(Math.random() * 3) + 1) - 1,
            h = 48 * (parseInt(Math.random() * 3) + 1) - 1;
        $('<div class="content" />').width(w).height(h).appendTo('body');
    }
});