Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1. Меняем фон по клику
- const bgBtn = document.querySelector('#bg-btn');
- bgBtn.addEventListener('click', function() {
- document.body.style.backgroundColor = 'lightgreen';
- });
- // 2. Текст при наведении и уходе
- const tip = document.querySelector('#tip');
- const originalText = tip.textContent;
- tip.addEventListener('mouseover', function() {
- tip.textContent = 'Спасибо за внимание!';
- });
- tip.addEventListener('mouseout', function() {
- tip.textContent = originalText;
- });
- // 3. Увеличиваем текст двойным щелчком
- const textPara = document.querySelector('#text');
- textPara.addEventListener('dblclick', function() {
- // получаем текущий размер или задаём 16px по умолчанию
- const currSize = parseInt(window.getComputedStyle(textPara).fontSize, 10) || 16;
- textPara.style.fontSize = (currSize + 4) + 'px';
- });
- // 4. Показ вводимого текста
- const liveInput = document.querySelector('#live');
- const outputSpan = document.querySelector('#output');
- liveInput.addEventListener('input', function(event) {
- outputSpan.textContent = event.target.value;
- });
- // 5. Выбор из списка с alert
- const sel = document.querySelector('#sel');
- sel.addEventListener('change', function() {
- alert('Вы выбрали: ' + sel.value);
- });
- // 6. Сообщение при прокрутке
- const scrollBlock = document.querySelector('#scroll');
- scrollBlock.addEventListener('scroll', function() {
- console.log('Прокрутка идёт');
- });
- // 7. Нажатие Enter в поле
- const inp = document.querySelector('#inp');
- inp.addEventListener('keydown', function(event) {
- if (event.key === 'Enter') {
- console.log('Enter нажата');
- }
- });
- // 8. Включить/выключить клик на box2
- const onBtn = document.querySelector('#on');
- const offBtn = document.querySelector('#off');
- const box2 = document.querySelector('#box2');
- function box2ClickHandler() {
- box2.style.backgroundColor = box2.style.backgroundColor === 'yellow' ? '' : 'yellow';
- }
- onBtn.addEventListener('click', function() {
- box2.addEventListener('click', box2ClickHandler);
- });
- offBtn.addEventListener('click', function() {
- box2.removeEventListener('click', box2ClickHandler);
- });
- // 9. Меняем фон только у кликнутого пункта
- const items = document.querySelectorAll('#list li');
- items.forEach(function(li) {
- li.addEventListener('click', function() {
- li.style.backgroundColor = 'lightblue';
- });
- });
- // 10. Мигание кнопки с остановкой через 5 секунд
- const flashBtn = document.querySelector('#flash');
- flashBtn.addEventListener('click', function() {
- let on = false;
- const intervalId = setInterval(function() {
- flashBtn.style.backgroundColor = on ? '' : 'red';
- on = !on;
- }, 1000);
- setTimeout(function() {
- clearInterval(intervalId);
- flashBtn.style.backgroundColor = ''; // возвращаем исходный
- }, 5000);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement