【发布时间】:2022-02-01 21:50:44
【问题描述】:
我真的不知道自己在做什么,但一直在玩 jQuery 来实现这一点。除了偶尔连续两次显示相同的推荐之外,我的功能运行良好,这是我试图解决的问题。这是我尝试的无法计算的代码,我很确定这是我不理解的明显内容!任何帮助表示赞赏。
您可以在推荐页脚中看到预期的功能:https://blue-coast-f4d2bc.webflow.io/contact
function getNumber(previous) {
if (previous === undefined) {
i = Math.floor(Math.random() * 14) + 1;
return i;
}
else {
while {
i = previous;
i = Math.floor(Math.random() * 14) + 1;
}
return i;
}
var quoteNumb = getNumber();
$('.footer-testimonial-quote:nth-of-type('+quoteNumb+')').addClass('show');
var footHeight = $('.testimonial-container').height();
$('.testimonial-container').css({'min-height': footHeight + 'px'});
$('.testimonial-container').css({'max-height': footHeight + 'px'});
$(document).ready(function() {
$(".refresh-button").click(function() {
$('.footer-testimonial-quote:nth-of-type('+quoteNumb+')').fadeOut(1000, function() {
$('.footer-testimonial-quote:nth-of-type('+quoteNumb+')').removeClass('show');
quoteNumb = getNumber(quoteNumb);
$('.footer-testimonial-quote:nth-of-type('+quoteNumb+')').addClass('show');
$('.footer-testimonial-quote:nth-of-type('+quoteNumb+')').fadeIn(1000);
});
});
});
【问题讨论】:
-
这个
while { i = previous; i = Math.floor(Math.random() * 14) + 1; }没有意义。我猜您打算比较i == previous并将其作为while循环的条件。此外,getNumber()在您的代码中只被调用一次,并且没有previous参数。 -
目的是在单击刷新按钮时生成一个新数字,该数字与前一个数字不同。因此,只要 i==previous 这个条件不满足,我必须从 14 的列表中再次生成一个新数字。尽管我不断破坏代码,但无法弄清楚如何实现它。
-
do { i = Math.floor(Math.random() * 14) + 1; } while (i === previous); -
好极了,似乎奏效了!非常感谢:)
标签: javascript jquery css variables random