function chunked(arr, numChunks) {
const chunks = [];
const chunkSize = arr.length / numChunks | 0;
const numLooseItems = arr.length % numChunks;
for (let i = 0, j = 0; j < numChunks; j++) {
chunks.push(arr.slice(i, i += chunkSize + (j < numLooseItems)));
}
return chunks;
}
const [ head, tail ] = chunked(arr, 2);
const itemSelector = '.parent';
const className = 'xxx';
document.querySelectorAll(itemSelector).forEach(n => {
n.addEventListener('mouseenter', onHover);
n.addEventListener('mouseleave', onHover);
});
function onHover(e) {
const state = e.type === 'mouseenter';
for (
let el = this;
(el = el.nextElementSibling) && !el.matches(itemSelector);
el.classList.toggle(className, state)
) ;
}
.parent
, чтобы создать видимость, будто бы стили не применялись:.parent:hover ~ .child {
...
}
.parent:hover ~ .parent ~ .child {
...
}
document.querySelectorAll('.selectit > input').forEach(n => {
n.checked = genres.includes(n.parentNode.innerText.trim());
});
for (const n of document.getElementsByClassName('selectit')) {
n.children[0].checked = genres.indexOf(n.textContent.trim()) > -1;
}
ind.map(((data, n) => data[n]).bind(null, arr.reduce((acc, row) => {
const d = acc[row[0]];
row.forEach((n, i) => i && (d[i] = d[i] || []).push(n));
return acc;
}, ind.reduce((acc, n) => (acc[n] = [ n ], acc), {}))))
как правильно сделать из этого директиву?
Note: This method is obsolete and should not be used. In particular, to interact with the clipboard, consider using the Clipboard API instead.
const clipboardDirective = (() => {
const values = new Map();
const onClick = e => navigator.clipboard.writeText(values.get(e.currentTarget));
return {
bind(el, binding) {
el.addEventListener('click', onClick);
values.set(el, binding.value);
},
update(el, binding) {
values.set(el, binding.value);
},
unbind(el) {
el.removeEventListener('click', onClick);
values.delete(el);
},
};
})();
Vue.directive('clipboard', clipboardDirective);
data: () => ({
scroll: 0,
...
}),
created() {
window.addEventListener('scroll', () => this.scroll = window.scrollY);
},
computed: {
scrollClasses() {
return что-то, в зависимости от значения this.scroll;
},
...
},
<div :class="scrollClasses"></div>
array.reduce((acc, el) => {
el.forEach((n, i) => {
acc[i] = acc[i] || [];
n.forEach((m, j) => (acc[i][j] = acc[i][j] || []).push(...[].concat(m)));
});
return acc;
}, [])
const result = ind.reduce((acc, col) => {
arr.forEach((n, i) => {
acc[i].push(i ? n.filter((m, j) => arr[0][j] === col) : col);
});
return acc;
}, arr.map(() => []));
const indObj = Object.fromEntries(ind.map((n, i) => [ n, i ]));
const result = arr.reduce((acc, n, i) => (
i && n.forEach((m, j) => acc[i][indObj[arr[0][j]]].push(m)),
acc
), arr.map((_, i) => ind.map(n => i ? [] : n)));
const min = 666;
const inputSelector = 'input';
const buttonSelector = 'button';
const input = document.querySelector(inputSelector);
const button = document.querySelector(buttonSelector);
input.addEventListener('input', e => button.disabled = e.target.value < min);
input.dispatchEvent(new Event('input'));
// или
const $input = $(inputSelector);
const $button = $(buttonSelector);
$input.on('input', () => $button.prop('disabled', $input.val() < min)).trigger('input');
const Modal = (props) => {
const onClick = e => {
if (e.target.classList.contains('close') || !e.target.closest('.modal-content')) {
props.close();
}
}
return (
<div className="modal" onClick={onClick}>
<div className="modal-content">
<span className="close">×</span>
{props.children}
</div>
</div>
);
};
const App = () => {
const [ opened, setOpened ] = React.useState(false);
const open = () => setOpened(true);
const close = () => setOpened(false);
return (
<div>
<h2>Modal Example</h2>
<button onClick={open}>Open Modal</button>
{opened && <Modal close={close}>hello, world!!</Modal>}
</div>
);
}
slick-slide
у предков целевого элемента, номер слайда можно определить через атрибут data-slick-index
(или можете назначать слайдам какие-то свои классы/атрибуты для выяснения, где был произведён клик).const className = 'qq-upload-file';
.const elements = document.querySelectorAll(`.${className}`);
// или
const elements = document.getElementsByClassName(className);
const getText = el => el.textContent;
// или
const getText = el => el.innerText;
// или (т.к., вложенных элементов нет)
const getText = el => el.innerHTML;
const texts = Array.from(elements, getText);
// или
const texts = Array.prototype.map.call(elements, getText);
// или
const texts = [];
for (const n of elements) {
texts.push(getText(n));
}
// или
const texts = [];
for (let i = 0; i < elements.length; i++) {
texts[i] = getText(elements[i]);
}
const uniqueCount = new Set(texts).size;
// или
const { size: uniqueCount } = new Map(texts.map(n => [ n, n ]));
// или
const [ uniqueCount ] = texts.reduce((acc, n) => {
acc[0] += !(acc[1][n] = acc[1].hasOwnProperty(n));
return acc;
}, [ 0, {} ]);
// или
const uniqueCount = texts.filter((n, i, a) => i === a.indexOf(n)).length;