<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<script src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"></script>
<body>
<button onclick="refresh()" class="refresh">刷新数据</button>
<div class="ls">
<table cellpadding="0" cellspacing="10"></table>
</div>
</body>
</html>
// 字体大小
var fontSize = 16
// 父容器宽度
var containerWidth = 400
// 间距
var spaceWidth = 10
/**
* 生成随机文件名
*/
var generateFileNameas = function() {
var randomFilename = function(randomFlag, min, max) {
var str = "",
range = min,
arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
];
// 随机产生
if (randomFlag) {
range = Math.round(Math.random() * (max - min)) + min;
}
for (var i = 0; i < range; i++) {
pos = Math.round(Math.random() * (arr.length - 1));
str += arr[pos];
}
return str;
}
var data = []
for (var i = 0; i < 10; i++) {
var filename = randomFilename(true, 1, 10)
data.push(filename)
}
return data
}
/**
* 文件名分栏
*/
var partition = function(files, fontSize, containerWidth, spaceWidth) {
var maxFile = files[0]
var minFile = files[0]
for (var i = 0; i < files.length; i++) {
var filename = files[i]
if (filename.length > maxFile.length) maxFile = filename
if (filename.length < minFile.length) minFile = filename
}
var subfield = function(list, colnum, size, width, spaceWidth) {
var result = _.chunk(list, Math.min(colnum, list.length))
var rowWidth = 0
for(var col=0;col<result[0].length;col++) {
colWidth = 0
for(var row=0; row<result.length; row++){
var item = result[row][col] || ''
colWidth = Math.max(colWidth, item.length * size / 2 + spaceWidth)
}
rowWidth += colWidth
}
return rowWidth < width ? result : []
}
// 英文数字只占半个字符宽度
var colnum = Math.max(1, Math.floor((containerWidth - maxFile.length * fontSize / 2 - spaceWidth) / (spaceWidth + fontSize /
2 * minFile.length)))
var result = []
while (colnum > 0 && result.length===0) {
if (colnum === 1) {
result = []
for(var i=files.length;i>0;i--) {
result.push([files.shift()])
}
} else {
result = subfield(files, colnum, fontSize, containerWidth, spaceWidth)
}
colnum--
}
return result
}
/**
* 结果渲染到页面
*/
var render = function(list) {
var str = ''
list.forEach(function(row) {
str += '<tr><td>'+ row.join('</td><td>') + '</td></tr>'
})
document.querySelector('.ls table').innerHTML = str
}
function refresh() {
var files = generateFileNameas()
var groups = partition(files, fontSize, containerWidth, spaceWidth)
render(groups)
}
refresh()
Output
300px
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |