- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有几个带有随机数的 div
,我后来使用一些简单的冒泡排序代码按升序排列,我想逐渐排列并为它们添加样式,而不是对它们进行样式设置并立即安排。
这感觉很简单,但我无法找到 sleep
for 循环
或正确使用setTimeout
函数的方法... 如果突出显示当前正在比较的两个 div
,也会加分。
这是我到目前为止所得到的一个工作示例:
function bubbleSort(input) {
var swapped;
do {
swapped = false;
for (var i=0; i < input.length-1; i++) {
var div1 = $('.divRandom').eq(i);
var div2 = $('.divRandom').eq(i+1);
if (input[i] > input[i+1]) {
var temp = input[i];
input[i] = input[i+1];
input[i+1] = temp;
arrangeDivs(div1, div2);
swapped = true;
}
}
} while (swapped);
}
function arrangeDivs(div1, div2){
div1.before(div2);
div1.removeClass('divUnsorted');
div1.addClass('divSorted');
div2.removeClass('divUnsorted');
div2.addClass('divSorted');
}
$('.bubbleBtn').click(function() {
var divArray = new Array();
divArray = createArray(divArray);
//console.log(divArray);
bubbleSort(divArray);
//console.log(divArray);
});
function createArray(divArray) {
var divLength = $('.divUnsorted').length;
for (var i = 0; i < divLength; i++){
var divNumber = parseInt($('.divUnsorted').eq(i).text());
divArray.push(divNumber)
}
return divArray;
}
$('.addDivBtn').click(function(){
$('.divRandom').removeClass('divSorted');
$('.divRandom').addClass('divUnsorted');
var randomNumber = Math.floor((Math.random() * 1000) + 1);
$('<div/>', {
'class':'divRandom divUnsorted',
'text':randomNumber,
}).appendTo('.addDivRandom');
$('.divRandom').addClass('divUnsorted');
});
.divRandom {
display: inline-block;
text-align: center;
margin: 5px;
padding: 10px;
width: 100px;
font-size: 20px;
}
.addDivRandom {
text-align: center;
margin: auto;
}
.divUnsorted {
border: 2px solid green;
background-color: #9db;
}
.divSorting {
border: 2px solid darkred;
background-color: #db9;
}
.divSorted {
border: 2px solid darkblue;
background-color: #9bd;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="addDivRandom">
</div>
<button class="addDivBtn" style="display: block;">Add</button>
<button class="bubbleBtn" style="display: block;">Bubble</button>
最佳答案
当你做一个循环时,整个页面将不得不等到它完成后再重新获得控制权。这包括您在 DOM 中所做的任何更改,因此浏览器将等待整个执行过程以向您显示结果。
如果改为运行排序的每个步骤,安排下一步,然后将控制权交还给浏览器并重复,则可以避免这种情况。通过这种方式,您可以在排序时更新 UI。为此,您可以使用 setTimeout
- 你会递归地安排你的函数,直到排序操作完成。
您的排序函数需要稍作改动 - 不会有循环,这反过来会使 swapped
变量过时。您可以通过将当前索引传递给 bubbleSort
的每次执行来跟踪当前索引。当没有东西可以排序时,你就完成了排序。我已经注释掉了不需要的代码以明确更改:
function bubbleSort(input, i = 0) {
//nothing to sort
if (input.length == 0) {
console.log("sorting complete");
return;
}
//var swapped; -> not needed
//do { -> not needed
//swapped = false; -> not needed
//for (var i=0; i < input.length-1; i++) { -> not needed
var div1 = $('.divRandom').eq(i);
var div2 = $('.divRandom').eq(i+1);
if (input[i] > input[i+1]) {
var temp = input[i];
input[i] = input[i+1];
input[i+1] = temp;
arrangeDivs(div1, div2);
//swapped = true; -> not needed
}
//} -> not needed - closes the for-loop
var next;
if (i == input.length) {
//loop back to the start
next = 0;
//exclude the last element - it's already sorted
input = input.slice(0, -1)
} else {
//move the index
next = i + 1
}
//schedule next step - you can change the delay to control the speed of the updates
setTimeout(bubbleSort, 450, input, next)
//} while (swapped); -> not needed
}
function arrangeDivs(div1, div2){
div1.before(div2);
div1.removeClass('divUnsorted');
div1.addClass('divSorted');
div2.removeClass('divUnsorted');
div2.addClass('divSorted');
}
$('.bubbleBtn').click(function() {
var divArray = new Array();
divArray = createArray(divArray);
//console.log(divArray);
bubbleSort(divArray);
//console.log(divArray);
});
function createArray(divArray) {
var divLength = $('.divUnsorted').length;
for (var i = 0; i < divLength; i++){
var divNumber = parseInt($('.divUnsorted').eq(i).text());
divArray.push(divNumber)
}
return divArray;
}
$('.addDivBtn').click(function(){
$('.divRandom').removeClass('divSorted');
$('.divRandom').addClass('divUnsorted');
var randomNumber = Math.floor((Math.random() * 1000) + 1);
$('<div/>', {
'class':'divRandom divUnsorted',
'text':randomNumber,
}).appendTo('.addDivRandom');
$('.divRandom').addClass('divUnsorted');
});
.divRandom {
display: inline-block;
text-align: center;
margin: 5px;
padding: 10px;
width: 100px;
font-size: 20px;
}
.addDivRandom {
text-align: center;
margin: auto;
}
.divUnsorted {
border: 2px solid green;
background-color: #9db;
}
.divSorting {
border: 2px solid darkred;
background-color: #db9;
}
.divSorted {
border: 2px solid darkblue;
background-color: #9bd;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="addDivRandom">
</div>
<button class="addDivBtn" style="display: block;">Add</button>
<button class="bubbleBtn" style="display: block;">Bubble</button>
因此,除此之外 - 如果您想突出显示正在排序的 div,您可以执行以下操作 - 当您确定现在正在检查哪两个 div 时,向它们添加样式 divSorting
.下次执行该函数时,删除所有 divSorting
类并突出显示接下来的两个。当您返回以重新启动计数器时,您可以使最终元素 divSorted
- 它已经就位。
function bubbleSort(input, i = 0) {
//nothing to sort
if (input.length == 0) {
return;
}
var div1 = $('.divRandom').eq(i);
var div2 = $('.divRandom').eq(i+1);
//reset all styles - we are soring different divs now
$("div").removeClass("divSorting");
//highlight the ones that are being examined now
div1.addClass('divSorting');
div2.addClass('divSorting');
if (input[i] > input[i+1]) {
var temp = input[i];
input[i] = input[i+1];
input[i+1] = temp;
arrangeDivs(div1, div2);
}
var next;
if (i == input.length-1) {
//loop back to the start
next = 0;
//exclude the last element - it's already sorted
input = input.slice(0, -1);
//mark the last div as sorted
$('.divRandom').eq(i).addClass("divSorted");
} else {
//move the index
next = i + 1
}
//schedule next step - you can change the delay to control the speed of the updates
setTimeout(bubbleSort, 450, input, next)
}
function arrangeDivs(div1, div2){
div1.before(div2);
}
$('.bubbleBtn').click(function() {
var divArray = new Array();
divArray = createArray(divArray);
//console.log(divArray);
bubbleSort(divArray);
//console.log(divArray);
});
function createArray(divArray) {
var divLength = $('.divUnsorted').length;
for (var i = 0; i < divLength; i++){
var divNumber = parseInt($('.divUnsorted').eq(i).text());
divArray.push(divNumber)
}
return divArray;
}
$('.addDivBtn').click(function(){
$('.divRandom').removeClass('divSorted');
$('.divRandom').addClass('divUnsorted');
var randomNumber = Math.floor((Math.random() * 1000) + 1);
$('<div/>', {
'class':'divRandom divUnsorted',
'text':randomNumber,
}).appendTo('.addDivRandom');
$('.divRandom').addClass('divUnsorted');
});
.divRandom {
display: inline-block;
text-align: center;
margin: 5px;
padding: 10px;
width: 100px;
font-size: 20px;
}
.addDivRandom {
text-align: center;
margin: auto;
}
.divUnsorted {
border: 2px solid green;
background-color: #9db;
}
.divSorting {
border: 2px solid darkred;
background-color: #db9;
}
.divSorted {
border: 2px solid darkblue;
background-color: #9bd;
}nd-color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="addDivRandom">
</div>
<button class="addDivBtn" style="display: block;">Add</button>
<button class="bubbleBtn" style="display: block;">Bubble</button>
关于javascript - 如何逐渐设置正在(冒泡)排序的元素的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56157185/
我想要一个可以逐渐将页面底部的不透明度从 0 更改为 1 的函数。 我在元素的顶部使用了类似的功能,但我使用的不是淡入,而是淡出。创建它相当容易,但在页面底部使用设置的阈值淡入是一场噩梦。 经过研究,
li 状态是关闭和打开。 闭合:颜色为黑色(没有光照,相互折叠)。 打开:颜色为红色(有光,全视野)。 我希望颜色从两个元素之间的点开始淡化,以表明阴影从轴心开始,直到它们关闭。 $('button'
我的问题很简单: 1)我有一个扩展 JFrame 的主类。 2)该类内部有一个 JPanel,其顶部有一个 BufferedImage。 3)最后还有一个 JButton,我称之为“Fire”..这就
我有这个立方体,我想在延迟 3000 后将其转换为不同的 X 和 Y 点。我无法理解如何在 jQuery 的帮助下做到这一点。这是一个JS Fiddle .下面是代码。 JS // code for
我们如何向背景图像添加黑色阴影,阴影从不透明度 1 开始逐渐降低到不透明度 0,在图像的所有 4 个边上? (至少 50 像素值的“降低阴影不透明度”。box-shadow 仅提供少量不透明度逐渐下降
我是一名优秀的程序员,十分优秀!