function openCity(e, city) {
document.querySelectorAll('.tablink').forEach(n => {
n.classList.toggle('active', city === 'All' || n === e.target);
});
document.querySelectorAll('.tabcontent').forEach(n => {
n.style.display = city === 'All' || n.id === city ? 'block' : 'none';
});
}
h1 {
font-size: var(--h1-font-size);
}
<input v-model="fontSize" type="range">
<h1>hello, world!!</h1>
data: () => ({
fontSize: 24,
}),
mounted() {
this.$watch(
'fontSize',
val => this.$el.style.setProperty('--h1-font-size', `${val}px`),
{ immediate: true }
);
},
document.querySelectorAll('.number').forEach(number => {
const top = number.getBoundingClientRect().top;
window.addEventListener('scroll', function onScroll() {
if (window.pageYOffset > top - window.innerHeight / 2) {
this.removeEventListener('scroll', onScroll);
let start = +number.innerHTML;
const interval = setInterval(function() {
number.innerHTML = ++start;
if (start >= number.dataset.max) {
clearInterval(interval);
}
}, 5);
}
});
});
$(document).wheel(
var item = $('#time'); if (e.deltaY > 0) item.scrollLeft += 100;
$(document).on('wheel', function(e) {
$('#time')[0].scrollLeft += e.originalEvent.deltaY > 0 ? 100 : -100;
// или
$('#time').get(0).scrollLeft += [ -100, 100 ][+(e.originalEvent.deltaY > 0)];
// или
$('#time').prop('scrollLeft', (i, val) => val + 100 * Math.sign(e.originalEvent.deltaY));
});
typeof
:const strings = arr.filter(n => typeof n === 'string');
const numbers = arr.filter(n => typeof n === 'number');
const booleans = arr.filter(n => typeof n === 'boolean');
const strings = arr.filter(n => n === `${n}`);
const numbers = arr.filter(n => n === +n); // в отличие от typeof, отбрасывает NaN
const booleans = arr.filter(n => n === !!n);
const groupedByType = arr.reduce((acc, n) => {
const type = n == null ? `${n}` : n.constructor.name.toLowerCase();
(acc[type] = acc[type] || []).push(n);
return acc;
}, {});
const strings = groupedByType.string;
const containerSelector = 'ul';
const itemSelector = 'li';
const activeClass = 'active';
const $containers = $(containerSelector).on('click', itemSelector, function(e) {
const index = $(itemSelector, e.delegateTarget).index(this);
$containers.find(`${itemSelector}.${activeClass}`).removeClass(activeClass);
$containers.find(`${itemSelector}:eq(${index})`).addClass(activeClass);
});
const containers = document.querySelectorAll(containerSelector);
containers.forEach(n => n.addEventListener('click', onClick));
function onClick(e) {
const item = e.target.closest(itemSelector);
if (item) {
const items = this.querySelectorAll(itemSelector);
const index = Array.prototype.indexOf.call(items, item);
containers.forEach(container => {
container.querySelectorAll(itemSelector).forEach((n, i) => {
n.classList.toggle(activeClass, i === index);
})
});
}
}
const td = [...document.querySelectorAll('table td')];
const first = td[0].offsetLeft;
const last = Math.max(...td.map(n => n.offsetLeft + n.offsetWidth));
td.forEach(n => (
(n.offsetLeft === first) && n.classList.add('first'),
(n.offsetLeft + n.offsetWidth === last) && n.classList.add('last')
));
const newArr = arr.reduce((acc, n) => (
acc[n.month - 1] = n.sum,
acc
), Array(12).fill(''));
const newArr = Array.from({ length: 12 }, function(_, i) {
return this[-~i] || '';
}, Object.fromEntries(arr.map(n => [ n.month, n.sum ])));
const newArr = [];
for (let i = 0, j = 0; i < 12; i++) {
newArr.push((arr[j] || {}).month === i + 1 ? arr[j++].sum : '');
}
<div id="buttons"></div>
<div id="map"></div>
const items = [
{ coord: [ 55.751244, 37.618423 ], title: 'Moscow' },
{ coord: [ 48.864716, 2.349014 ], title: 'Paris' },
{ coord: [ 34.052235, -118.243683 ], title: 'Los Angeles' },
];
ymaps.ready(function () {
const map = new ymaps.Map('map', {
zoom: 9,
center: items[Math.random() * items.length | 0].coord,
});
items.forEach(n => map.geoObjects.add(new ymaps.Placemark(n.coord)));
const buttons = document.querySelector('#buttons');
buttons.innerHTML = items.map((n, i) =>
`<button data-index="${i}">${n.title}</button>`
).join('');
buttons.addEventListener('click', ({ target: { dataset: { index } } }) => {
if (index) {
map.setCenter(items[index].coord);
}
});
});