function createTree(data, defaultId, parentIds = '') {
const ul = document.createElement("ul");
data.map((item, index) => {
const li = document.createElement("li");
const id = [parentIds, item.id].join('_')
li.textContent = id;
if (item.children) {
li.appendChild(createTree(item.children, index, id));
}
ul.appendChild(li);
});
return ul;
}
const createTree = (data, index = []) =>
data.reduce((ul, { children: c }, i) => {
index.push(i);
const li = document.createElement('li');
li.innerText = index.join('_');
if (Array.isArray(c) && c.length) {
li.appendChild(createTree(c, index));
}
ul.appendChild(li);
index.pop();
return ul;
}, document.createElement('ul'));
// или
const createTree = (data, index = []) =>
data instanceof Array && data.length
? `<ul>${data.map((n, i) => `
<li>
${(index.push(i), index.join('_'))}
${createTree(n.children, index)}
${(index.pop(), '')}
</li>`).join('')}
</ul>`
: '';
function nextImage(image) {
var imageNames = ["blackbird.jpg", "duck.jpg", "sheep.jpg"];
image.current++;
if (image.current === imageNames.length) image.current = 0;
image.src = "images/" + imageNames[image.current];
}
window.onload = function() {
var img = document.getElementById("main");
img.current = 1;// создаём новое свойство (счётчик)
img.onmousedown = function(event) {
var startX = event.clientX;
img.onmousemove = function(event) {
console.log(startX);
if (startX - event.clientX < -40) {
nextImage(this);
this.onmousemove = null;
}
};
return false; //Позволяет избавиться от стадартного обработчика
}
}
<!DOCTYPE HTML>
<html lang=en>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href=""/>
<script src="type.js"></script>
</head>
<body>
<img id="main" src="images/duck.jpg">
</body>
</html>