JS冒泡排序(div)
更生动的排序动画。
通过改变div的高度来实现排序,通过闭包来实现for循环的睡眠时间。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
button
{
width:70px;
height:30px;
background:#005AA8;
color:white;
font-size:15px;
font-family:Microsoft YaHei;
border:0;
outline:0;
border-radius:5px;
cursor:pointer;
opacity:.8;
} button:hover
{
opacity:1;
} #box
{
width:300px;
height:400px;
outline:1px solid black;
margin-top:10px;
} #box div
{
width:3px;
height:200px;
margin-left:1px;
background:blue;
display:inline-block; } #boxboxbox
{
width:0;
height:100%;
display:inline-block;
margin-left:0px;
} </style>
</head>
<body> <button onclick="fnnn()">排序</button> <div id="box">
<span id="boxboxbox"></span>
</div> <script> // div 以及 div 的宽高
var box = document.getElementById("box");
var boxW = box.offsetWidth;
var boxH = box.offsetHeight; var len = boxW/4; for(var i = 0;i<len;i++){
var crtDiv = document.createElement("div");
var divH = parseInt(Math.random()*boxH);
crtDiv.style.height = divH + "px";
crtDiv.index = i;
box.insertBefore(crtDiv,document.getElementById("boxboxbox"));
} function fnnn(){
setInterval(fn,300);
} function fn(){
var childDiv = box.children; for(var j = 0,leng = childDiv.length-1;j<leng;j++){ for(var n = 0,nleng = childDiv.length-1;n<nleng-j;n++){ (function fun(n){
setTimeout(function(){
if(parseInt(childDiv[n].style.height) > parseInt(childDiv[n+1].style.height)){ var oneH = parseInt(childDiv[n].style.height); childDiv[n].style.height = parseInt(childDiv[n+1].style.height) + "px"; childDiv[n+1].style.height = oneH + "px"; } },n*100);
})(n); }
}
}
</script>
</body>
</html>
JS冒泡排序(div)的更多相关文章
- js控制div滚动条,滚动滚动条使div中的元素可见并居中
1.html代码如下 <div id="panel"> <div id="div1"></div> <div id=& ...
- js实现div居中
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- js 控制Div循环显示 非插件版
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JS控制DIV隐藏显示
转载自:http://blog.sina.com.cn/s/blog_6c3a67be0100ldbe.html JS控制DIV隐藏显示 一,需求描述: 现在有3个DIV块,3个超链接,需要点击一个链 ...
- 功能整合(一):滚动条的变相隐藏、js控制div的渐变显示、滚动条监听
1.滚动条的变相隐藏 思路: 1. 把body的横向,纵向的超出部分隐藏,宽设置100%:高设置100%.就没有body的滚动条了, 2. 然后把最外层的div的宽设置的比body的宽宽一点,把d ...
- JS 实现DIV 滚动至顶部后固定
JS 实现DIV 滚动至顶部后固定 <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8" ...
- JS控制div跳转到指定的位置的几种解决方案总结
原文:http://www.jb51.net/article/96574.htm 这篇文章主要介绍了JS控制div跳转到指定的位置的几种解决方案总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考 ...
- js控制div样式显示与隐藏,JS通过点击超链接右边(指定位置)显示一个图标
原文出自:https://blog.csdn.net/seesun2012 javascript基础篇,老土的方法解决js控制div样式,便于新手理解,粗暴的不能再粗暴,如果你是高手,请忽略! 思路: ...
- JS获取div高度的方法
有时在写页面时,需要获取一个div的高度.怎么才能获取呢?哈哈,先上结论.有两种方法. offsetHeight .clientHeight getComputedStyle offsetHeight ...
- js获取div基础元素
1.js获取div元素 clientHeight 获取对象的高度,不计算任何边距.边框.滚动条,但包括该对象的补白. clientLeft 获取 offsetLeft 属性和客户区域的实际左边之间的距 ...
随机推荐
- Redis实战阅读笔记——第二章
在性能的要求下,如何获取重构之前的构件
- EKF的理解
若已知参考点(landmarks)的坐标,则状态向量中不必含有xL, 从而实现的仅为机器人在已知环境中的定位,求解大大减少(状态向量维度仅为运动状态).若想实现完整SLAM,必须将xL加入状态向量中. ...
- 解决svn问题:Wrong committed revision number: -1
参考:http://my.oschina.net/luckyi/blog/291007
- 利用box-shadow实现伪边框透明到图片
前阵子突然看到了一个效果,一张图片,有一个边框,但是边框可以透明到图片.直接上图 贴代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...
- Eclipse使用Maven tomcat:run命令启动web项目时修改默认端口
- java后台对前端输入的特殊字符进行转义
转自:http://www.cnblogs.com/yangzhilong/p/5667165.html java后台对前端输入的特殊字符进行转义 HTML: 常见的帮助类有2个:一个是spring的 ...
- YII2之 Scenario
使用方法 // scenario is set as a property $model = new User; $model->scenario = User::SCENARIO_SHOW; ...
- XCode8目录整理后的几个警告消除,Missing file
Git目录没有及时更新导致 终端进入目录运行如下命令 git rm main.m git rm Info.plist git rm AppDelegate.h git rm AppDelegate.m ...
- ContextMenuStrip 类
表示快捷菜单 命名空间: System.Windows.Forms程序集: System.Windows.Forms(位于 System.Windows.Forms.dll) 继承层次结构 Sy ...
- ExecutorService线程池
ExecutorService 建立多线程的步骤: 1.定义线程类 class Handler implements Runnable{} 2.建立ExecutorService线程池 Executo ...