WEB APIs(2)

应用定时器可以写一个定时轮播图,如下

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport"content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"><title>~</title><link rel="shortcut icon" href="https://www.bilibili.com/favicon.ico"><link rel="stylesheet" href="css/初始化表.css"><link rel="stylesheet" href="css/index.css"><meta name="keywords" content="..." /><style>/*写代码时始终要考虑权重问题!*/@font-face {font-family: 'icomoon';src: url('fonts/icomoon.eot?au9n7q');src: url('fonts/icomoon.eot?au9n7q#iefix') format('embedded-opentype'),url('fonts/icomoon.ttf?au9n7q') format('truetype'),url('fonts/icomoon.woff?au9n7q') format('woff'),url('fonts/icomoon.svg?au9n7q#icomoon') format('svg');font-weight: normal;font-style: normal;font-display: block;}.slider {width: 560px;height: 400px;overflow: hidden;}.slider-wrapper {width: 100%;height: 320px;}.slider-wrapper img {width: 100%;height: 100%;display: block;}.slider-footer p {margin: 0;color: #fff;font-size: 18px;margin-bottom: 10px;}.slider-footer {height: 80px;background-color: rgb(72, 131, 213);padding: 12px 12px 0 12px;position: relative;}ul {display: flex;align-items: center;}.toggle {position: absolute;right: 0;top: 12px;display: flex;}ul li {width: 8px;height: 8px;margin: 4px;border-radius: 50%;background-color: #fff;opacity: 0.4;cursor: pointer;}ul .active {width: 12px;height: 12px;opacity: 1;}</style></head><body><div class="slider"><div class="slider-wrapper"><img src="img/1.jpg" alt=""></div><div class="slider-footer"><p>哆啦A梦1</p><ul><li></li><li></li><li></li><li></li></ul><div class="toggle"><button class="prev">&lt;</button><button class="next">&gt;</button></div></div></div><script>const sliderData = [{ url: 'img/1.jpg', title: '哆啦A梦1', color: 'rgb(72, 131, 213)' },{ url: 'img/2.jpg', title: '哆啦A梦2', color: 'rgb(43,35,26)' },{ url: 'img/3.jpg', title: '哆啦A梦3', color: 'rgb(36,3,31)' },{ url: 'img/4.jpg', title: '哆啦A梦4', color: 'rgb(166,131,143)' }]function getRandom(m, n) {return Math.floor(Math.random() * (n - m + 1)) + m}const random = getRandom(0, 3)const img = document.querySelector('.slider-wrapper img')const p = document.querySelector('.slider-footer p')const footer = document.querySelector('.slider-footer')document.querySelector(`ul li:nth-child(${1})`).classList.add('active')let i = 0setInterval(function () {i++img.src = sliderData[i % 4].urlp.innerHTML = sliderData[i % 4].titlefooter.style.backgroundColor = sliderData[i % 4].colordocument.querySelector(`ul li:nth-child(${((i - 1) % 4) + 1})`).classList.remove('active')document.querySelector(`ul li:nth-child(${(i % 4) + 1})`).classList.add('active')}, 1000)</script>
</body></html>

效果:

此案例有一个缺陷,点击页面无法与用户交互,这就用到了事件监听

事件监听

什么是事件

编程时系统内发生的动作,比如单机一个按钮

监听即一旦有事件触发立即调用一个函数做出响应也称绑定事件

语法:

元素对象.addEventListener('事件类型',要执行的函数)

事件源:哪个DOM元素被触发,就获取这个元素

事件类型:用什么方式触发,鼠标点击click,鼠标经过mouseover等

调用函数:要做什么事

点击即可弹出对话框

事件类型

鼠标事件(click鼠标经过,mouseenter点击和mouseleave离开)

焦点事件(focus获得焦点,blur失去焦点)

键盘事件(Keydown键盘按下和Keyup抬起)

文本事件(input用户输入事件)

由此,可以得到完整轮播图

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport"content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"><title>~</title><link rel="shortcut icon" href="https://www.bilibili.com/favicon.ico"><link rel="stylesheet" href="css/初始化表.css"><link rel="stylesheet" href="css/index.css"><meta name="keywords" content="..." /><style>/*写代码时始终要考虑权重问题!*/@font-face {font-family: 'icomoon';src: url('fonts/icomoon.eot?au9n7q');src: url('fonts/icomoon.eot?au9n7q#iefix') format('embedded-opentype'),url('fonts/icomoon.ttf?au9n7q') format('truetype'),url('fonts/icomoon.woff?au9n7q') format('woff'),url('fonts/icomoon.svg?au9n7q#icomoon') format('svg');font-weight: normal;font-style: normal;font-display: block;}.slider {width: 560px;height: 400px;overflow: hidden;}.slider-wrapper {width: 100%;height: 320px;}.slider-wrapper img {width: 100%;height: 100%;display: block;}.slider-footer p {margin: 0;color: #fff;font-size: 18px;margin-bottom: 10px;}.slider-footer {height: 80px;background-color: rgb(72, 131, 213);padding: 12px 12px 0 12px;position: relative;}ul {display: flex;align-items: center;}.toggle {position: absolute;right: 0;top: 12px;display: flex;}ul li {width: 8px;height: 8px;margin: 4px;border-radius: 50%;background-color: #fff;opacity: 0.4;cursor: pointer;}ul .active {width: 12px;height: 12px;opacity: 1;}.toggle {right: 0;top: 12px;}.toggle button {margin-right: 10px;width: 28px;height: 28px;border: none;background: rgba(255,255,255,0.1);color: #fff;border-radius: 4px;cursor: pointer;appearance: none;}.toggle button:hover {background: rgba(255,255,255,0.2);}</style></head><body><div class="slider"><div class="slider-wrapper"><img src="img/1.jpg" alt=""></div><div class="slider-footer"><p>哆啦A梦1</p><ul><li></li><li></li><li></li><li></li></ul><div class="toggle"><button class="prev">&lt;</button><button class="next">&gt;</button></div></div></div><script>const sliderData = [{ url: 'img/1.jpg', title: '哆啦A梦1', color: 'rgb(72, 131, 213)' },{ url: 'img/2.jpg', title: '哆啦A梦2', color: 'rgb(43,35,26)' },{ url: 'img/3.jpg', title: '哆啦A梦3', color: 'rgb(36,3,31)' },{ url: 'img/4.jpg', title: '哆啦A梦4', color: 'rgb(166,131,143)' }]const img = document.querySelector('.slider-wrapper img')const p = document.querySelector('.slider-footer p')const footer = document.querySelector('.slider-footer')const next = document.querySelector('.next')const prev = document.querySelector('.prev')const slider = document.querySelector('.slider')document.querySelector(`ul li:nth-child(${1})`).classList.add('active')let n=setInterval(function () {next.click()}, 900)let i = 0prev.addEventListener('click',function(){i--i=i<0?sliderData.length-1:iimg.src = sliderData[i % 4].urlp.innerHTML = sliderData[i % 4].titlefooter.style.backgroundColor = sliderData[i % 4].colordocument.querySelector(`ul li:nth-child(${((i + 1) % 4) + 1})`).classList.remove('active')document.querySelector(`ul li:nth-child(${(i % 4) + 1})`).classList.add('active')})next.addEventListener('click',function(){i++img.src = sliderData[i % 4].urlp.innerHTML = sliderData[i % 4].titlefooter.style.backgroundColor = sliderData[i % 4].colordocument.querySelector(`ul li:nth-child(${((i - 1) % 4) + 1})`).classList.remove('active')document.querySelector(`ul li:nth-child(${(i % 4) + 1})`).classList.add('active')})slider.addEventListener('mouseenter',function(){clearInterval(n)})slider.addEventListener('mouseleave',function(){n=setInterval(function () {next.click()}, 900)})</script>
</body></html>

焦点事件

案例:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport"content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"><title>~</title><link rel="shortcut icon" href="https://www.bilibili.com/favicon.ico"><link rel="stylesheet" href="css/初始化表.css"><link rel="stylesheet" href="css/index.css"><meta name="keywords" content="..." /><style>/*写代码时始终要考虑权重问题!*/@font-face {font-family: 'icomoon';src: url('fonts/icomoon.eot?au9n7q');src: url('fonts/icomoon.eot?au9n7q#iefix') format('embedded-opentype'),url('fonts/icomoon.ttf?au9n7q') format('truetype'),url('fonts/icomoon.woff?au9n7q') format('woff'),url('fonts/icomoon.svg?au9n7q#icomoon') format('svg');font-weight: normal;font-style: normal;font-display: block;}ul {list-style: none;}.mi {position: relative;width: 223px;margin: 100px auto;}.mi input {width: 223px;height: 48px;padding: 0 10px;font-size: 14px;line-height: 48px;border: 1px solid #e0e0e0;outline: none;}.mi .search {border: 1px solid #ff6700;}.list {display: none;position: absolute;border: 1px solid #e0e0e0;left: 0;top: 48px;width: 223px;border-top: 0;background-color: #fff;}.list a {display: block;padding: 6px 15px;font-size: 12px;color: #424242;}.list a:hover {background-color: #eee;}</style></head><body><div class="mi"><input type="search"><ul class="list"><li><a href="#">0</a></li><li><a href="#">1</a></li><li><a href="#">2</a></li><li><a href="#">3</a></li><li><a href="#">4</a></li><li><a href="#">5</a></li><li><a href="#">6</a></li><li><a href="#">7</a></li></ul></div><script>const input=document.querySelector('[type=search]')//属性选择器const ul=document.querySelector('.list')input.addEventListener('focus',function(){ul.style.display='block'input.classList.add('search')})input.addEventListener('blur',function(){ul.style.display='none'input.classList.remove('search')})</script>
</body></html>

效果:

键盘事件

文本事件

 表单输入触发

发布评论案例

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport"content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"><title>~</title><link rel="shortcut icon" href="https://www.bilibili.com/favicon.ico"><link rel="stylesheet" href="css/初始化表.css"><link rel="stylesheet" href="css/index.css"><meta name="keywords" content="..." /><style>/*写代码时始终要考虑权重问题!*/@font-face {font-family: 'icomoon';src: url('fonts/icomoon.eot?au9n7q');src: url('fonts/icomoon.eot?au9n7q#iefix') format('embedded-opentype'),url('fonts/icomoon.ttf?au9n7q') format('truetype'),url('fonts/icomoon.woff?au9n7q') format('woff'),url('fonts/icomoon.svg?au9n7q#icomoon') format('svg');font-weight: normal;font-style: normal;font-display: block;}.wrapper {min-width: 400px;max-width: 800px;display: flex;justify-content: flex-end;}.avatar {width: 48px;height: 48px;border-radius: 50%;overflow: hidden;background: url(img/哔站头像.gif) no-repeat center/cover;margin-right: 20px;}.wrapper textarea {outline: none;border-color: transparent;resize: none;background-color: #f5f5f5;border-radius: 4px;flex: 1;padding: 10px;transition: all 0.5s;height: 50px;}.wrapper textarea:focus {border-color: #f5f5f5;background-color: #fff;height: 60px;}.wrapper button {background-color: #00aeec;color: #fff;border: none;border-radius: 4px;margin-left: 10px;width: 70px;cursor: pointer;}.wrapper .total {margin-right: 80px;color: #999;margin-top: 5px;opacity: 0;transition: all 0.5s;}</style>
</head><body><div class="wrapper"><i class="avatar"></i><textarea id="tx" placeholder="发布友善评论" rows="2" maxlength="200"></textarea><button>发布</button></div><div class="wrapper"><span class="total">0/200字</span></div><script>const tx=document.querySelector('#tx')const total=document.querySelector('.total')tx.addEventListener('focus',function(){total.style.opacity=1})tx.addEventListener('blur',function(){total.style.opacity=0})tx.addEventListener('input',function(){total.innerHTML=`${tx.value.length}/200字`})</script>
</body></html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/473703.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【MySQL进阶之路】MySQL中到底为什么会出现幻读?

欢迎关注公众号&#xff08;通过文章导读关注&#xff1a;【11来了】&#xff09;&#xff0c;及时收到 AI 前沿项目工具及新技术的推送&#xff01; 在我后台回复 「资料」 可领取编程高频电子书&#xff01; 在我后台回复「面试」可领取硬核面试笔记&#xff01; 文章导读地址…

精通C语言:打造高效便捷的通讯录管理系统

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ &#x1f388;&#x1f388;养成好习惯&#xff0c;先赞后看哦~&#x1f388;&#x1f388; 所属专栏&#xff1a;C语言项目 贝蒂的主页&#xff1a;Betty‘s blog 引言 在我们大致学习完C语言之后&#xff0c;我们就可以利用目前所学的知识去…

【python】网络爬虫与信息提取--正则表达式

一、正则表达式 正则表达式是用来简洁表达一组字符串的表达式。是通用的字符串表达框架&#xff0c;简洁表达一组字符串的表达式&#xff0c;针对字符串表达“简洁”和“特征”思想的工具&#xff0c;判断某字符串的特征归属。 用处&#xff1a;表达文本类型的特征&#xff1b;…

AGV命名要求

命名规则&#xff1a; 组代号负载代号导引代号负载方式代号轮系代号升级换代代号(初代可隐藏)变形代号

面试题-01

1、JDK 和 JRE 和 JVM 分别是什么&#xff0c;有什么区别&#xff1f; JDK&#xff08;Java Development Kit&#xff0c;Java 软件开发工具包&#xff09; JDK&#xff08;Java Development Kit&#xff09;&#xff1a;JDK 是 Java 开发⼯具包&#xff0c;包含了编写、编译…

Python爬虫之自动化测试Selenium#7

爬虫专栏&#xff1a;http://t.csdnimg.cn/WfCSx 前言 在前一章中&#xff0c;我们了解了 Ajax 的分析和抓取方式&#xff0c;这其实也是 JavaScript 动态渲染的页面的一种情形&#xff0c;通过直接分析 Ajax&#xff0c;我们仍然可以借助 requests 或 urllib 来实现数据爬取…

数学建模【非线性规划】

一、非线性规划简介 通过分析问题判断是用线性规划还是非线性规划 线性规划&#xff1a;模型中所有的变量都是一次方非线性规划&#xff1a;模型中至少一个变量是非线性 非线性规划在形式上与线性规划非常类似&#xff0c;但在数学上求解却困难很多 线性规划有通用的求解准…

h5和微信小程序实现拍照功能(其中h5暂时无法调用闪光灯)

代码如下 <template><view class"camera"><!-- #ifdef MP --><camera ref"myCamera" id"myCamera" device-position"back" :flash"flash" error"error" style"display: block;"&…

Vue2:组件间通信框架Vuex

一、原理图及作用 功能介绍&#xff1a; 简单说就是Vue项目中&#xff0c;各个组件间通信的一种框架 相较于之前的全局事件总线&#xff0c;该框架更实用&#xff01; 提高了代码的复用率&#xff0c;把核心业务代码&#xff0c;集中到store中&#xff0c;这样&#xff0c;一处…

Java Lambda表达式:简化编程,提高效率

Java Lambda表达式&#xff1a;简化编程&#xff0c;提高效率 1. 使用Lambda表达式进行集合遍历1.1 未使用Lambda表达式&#xff1a;1.2 使用Lambda表达式&#xff1a; 2. 使用Lambda表达式进行排序2.1 未使用Lambda表达式&#xff1a;2.2 使用Lambda表达式&#xff1a; 3. 使用…

市场复盘总结 20240208

仅用于记录当天的市场情况&#xff0c;用于统计交易策略的适用情况&#xff0c;以便程序回测 短线核心&#xff1a;不参与任何级别的调整&#xff0c;采用龙空龙模式 一支股票 10%的时候可以操作&#xff0c; 90%的时间适合空仓等待 二进三&#xff1a; 进级率中 25% 最常用的…

Practical User Research for Enterprise UX

2.1 Why It’s Hard to Get Support for Research in Enterprises 2.1.1 Time and Budget Instead of answering the question “What dowe gain if we do this research?”, ask instead “What do we stand to lose if we don’t do the research?” 2.1.2 Legacy Thinkin…