Скрипт кастомизации формы поиска в стиле Github
Популярные запросы: Open Graph, плагин Reading Time, тема Quebec, плагин Telegram, набор иконок Analogue
Скрипт кастомизации формы поиска в стиле Github
Используем клавиатурные нажатия для переброса курсора, отправки формы или ее очистки
const topSearch = document.forms.topsearch;
if (topSearch) {
const topSearchInput = topSearch.sq;
const topSearchInputPlaceholder = topSearchInput.getAttribute('data-placeholder');
const topSearchButton = topSearch.querySelector('button');
function hastext() {
if (topSearchInput.value === '') {
topSearchButton.disabled = true;
} else {
topSearchButton.disabled = false;
}
}
function noActive() {
return document.activeElement === document.body;
}
topSearchInput.addEventListener("keyup", function() {
hastext();
});
window.addEventListener('keydown', (event) => {
if (event.code === "Slash" && noActive()) {
event.preventDefault();
topSearchInput.value = topSearchInput.placeholder = '';
topSearchInput.focus();
} else if (event.key === "Escape" && document.activeElement === topSearchInput) {
event.preventDefault();
topSearchInput.value = '';
topSearchInput.blur();
topSearchInput.placeholder = topSearchInputPlaceholder;
topSearchButton.disabled = true;
} else if (event.key === "Enter" && document.activeElement === topSearchInput) {
return;
}
// console.log(event.key);
});
window.addEventListener('click', function(event) {
if (topSearchInput.contains(event.target)) {
// console.log("Clicked on form input!");
topSearchInput.value = topSearchInput.placeholder = '';
} else if (topSearchButton.contains(event.target)) {
// console.log("Clicked on form button!");
return;
} else {
// console.log("Clicked on empty space!");
topSearchInput.value = '';
topSearchInput.placeholder = topSearchInputPlaceholder;
topSearchButton.disabled = true;
}
});
}
- Создан: 18.01.26 @ 20:01
- Будет удален: никогда
- Просмотры сниппета: 25
- Тип кода: JavaScript
- Владелец: admin
Новые сниппеты:
-
Скрипт анимированной текстуры для элемента canvas
Просмотры: 10
Дата: 10.03.26 @ 16:05 -
Шаблон редактирования страницы для Cotonti Siena (grid-модель)
Просмотры: 11
Дата: 05.03.26 @ 12:42 -
Шаблон добавления страницы для Cotonti Siena (grid-модель)
Просмотры: 10
Дата: 05.03.26 @ 11:50 -
Отображаем шапку сайта при скролле вверх в мобильной версии
Просмотры: 9
Дата: 18.02.26 @ 11:30 -
Блокиратор ботов в User-Agent
Просмотры: 12
Дата: 04.02.26 @ 17:55 -
Адаптивный шаблон редактирования страницы
Просмотры: 53
Дата: 22.01.26 @ 10:47 -
IP-диапазоны Biterika Grupp для блокировки
Просмотры: 26
Дата: 20.01.26 @ 13:07 -
LESS mixin для верстки моделью grid с обводкой
Просмотры: 24
Дата: 16.01.26 @ 20:47 -
Блок вывода статистики страницы с помощью плагина Reading Time
Просмотры: 27
Дата: 16.01.26 @ 13:50 -
Функция минификации CSS и JS ресурсов
Просмотры: 43
Дата: 14.01.26 @ 20:08