正如标题所说:这行不通。该脚本确实移动了我要移动的文章,如果我将选项放在选择器上,它也会对其进行动画处理,但它不会随机化元素的height
和width
。所有元素的随机宽度均为 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
谢谢计算机科学,我已经接受了你的回答:)
让我们解释一下你的代码
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');
}
我相信这就是你想要的。
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');
}
});
随机化哪些元素的宽度和高度?您的问题可能是您没有将任何东西传递给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)...