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>

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

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

相关文章

基于 InternLM 和 LangChain 搭建你的知识库(三)

基于 InternLM 和 LangChain 搭建你的知识库 大模型开发范式 Finetune 在大型语言模型中&#xff0c;Finetune&#xff08;微调&#xff09;是一种技术&#xff0c;用于调整预训练的模型以提高其在特定任务或数据集上的表现。这种方法通常涉及以下步骤&#xff1a; 预训练模…

【MySQL】:DQL查询

&#x1f3a5; 屿小夏 &#xff1a; 个人主页 &#x1f525;个人专栏 &#xff1a; MySQL从入门到进阶 &#x1f304; 莫道桑榆晚&#xff0c;为霞尚满天&#xff01; 文章目录 &#x1f4d1;前言一. DQL1.1 基本语法1.2 基础查询1.3 条件查询1.3 聚合函数 &#x1f324;️ 全篇…

【Linux取经路】文件系统之被打开的文件——文件描述符的引入

文章目录 一、明确基本共识二、C语言文件接口回顾2.1 文件的打开操作2.2 文件的读取写入操作2.3 三个标准输入输出流 三、文件有关的系统调用3.1 open3.1.1 比特位级别的标志位传递方式 3.2 write3.2.1 模拟实现 w 选项3.2.2 模拟实现 a 选项 3.3 read 四、访问文件的本质4.1 再…

黑马程序员java部分笔记(持续更新)九点五:数组的动态初始化与常见问题

为什么有动态初始化呢? 当 不知道数组里几个元素的具体值时用动态初始化 动态初始化&#xff1a;初始化时只指定数组长度&#xff0c;由系统分配初始值 格式&#xff1a;数据类型[]数组名new 数据类型[数组长度]; 特点&#xff1a;在创建的时候有自己指定数组长度&#xff0c;…

Java的集合框架和泛型

文章目录 集合框架什么是集合框架类和接口总览 集合框架的重要性背后所涉及的数据结构以及算法什么是数据结构容器背后对应的数据结构什么是算法 包装类基本数据类型和对应的包装类装箱和拆箱自动装箱和自动拆箱 泛型什么是泛型引出泛型语法泛型类泛型的上界(没有下界)泛型方法…

心理辅导|高校心理教育辅导系统|基于Springboot的高校心理教育辅导系统设计与实现(源码+数据库+文档)

高校心理教育辅导系统目录 目录 基于Springboot的高校心理教育辅导系统设计与实现 一、前言 二、系统功能设计 三、系统实现 1、学生功能模块的实现 &#xff08;1&#xff09;学生登录界面 &#xff08;2&#xff09;留言反馈界面 &#xff08;3&#xff09;试卷列表界…

基于Springboot的社区物资交易互助平台(有报告)。Javaee项目,springboot项目。

演示视频&#xff1a; 基于Springboot的社区物资交易互助平台&#xff08;有报告&#xff09;。Javaee项目&#xff0c;springboot项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系…

【光学】学习记录1-几何光学的近轴理论

课程来源&#xff1a;b站资源-光学-中科大-崔宏滨老师&#xff08;感谢&#xff09;&#xff0c;本系列仅为自学笔记 【光学 中科大 崔宏滨老师 1080p高清修复&#xff08;全集&#xff09;】https://www.bilibili.com/video/BV1NG4y1C7T9?p2&vd_source7ba37b2cff2a1b783…

MATLAB计算极限和微积分

一.函数与极限 计算极限&#xff1a;lim(3*x^2/(2x1))&#xff0c;x分别趋于0和1&#xff0c;代码如下&#xff1a; syms x; limit(3*x*x/(2*x1),x,0) limit(3*x*x/(2*x1),x,1) 结果分别为0和1&#xff1a; 1.计算双侧极限 计算极限&#xff1a;lim(3*x^2/(2x1))&#xff0…

wordpress日主题模版Ripro-v5 6.4开心版

RiPro主题全新V5版本&#xff0c;&#xff08;原RiPro v2旧版已停更&#xff09;是一个优秀且功能强大、速度极快&#xff0c;易于管理、现代化的WordPress虚拟资源商城主题。支持首页模块化布局和WP原生小工具模块化首页可拖拽设置&#xff0c;让您的网站设计体验更加舒适。同…

linux进程控制【程序替换】

目录 前言&#xff1a; 1.替换原理 ​编辑 2.替换函数 2.1函数 execl 2.2函数 execv 2.3函数 execlp 2.4函数 execvp 2.5函数 execle 2.6函数 execve 2.7函数 execvpe 前言&#xff1a; 前面我们介绍了进程控制中的创建&#xff0c;退出等待&#xff0c;本章节我们将…

8.JS中的== 操作符的强制类型转换规则

对于 来说&#xff0c;如果对比双方的类型不一样&#xff0c;就会进行类型转换。假如对比 x 和 y 是否相同&#xff0c;就会进行如下判断流程&#xff1a; 首先会判断两者类型是否相同&#xff0c;类型相同的话就比较两者的大小&#xff1b;类型不相同的话&#xff0c;就会进…

核心篇-OSPF技术之序(下)

文章目录 一. 实验专题1.1. 实验1&#xff1a;配置OSPF特殊区域1.1.1. 实验目的1.1.2. 实验拓扑图1.1.3. 实验步骤&#xff08;1&#xff09;配置IP地址&#xff08;2&#xff09;创建环回口&#xff08;3&#xff09;查看路由表&#xff08;4&#xff09;设置Stub区域&#xf…

2024/02/13

21 、C 22 、D 23、B 如果5先出栈那么1&#xff0c;2&#xff0c;3&#xff0c;4就已经入栈了&#xff0c;5出后4出&#xff0c;1要出栈必须先让3&#xff0c;2出栈&#xff0c;所以 不可能输出B 24、10&#xff0c;12&#xff0c;120 25、2&#xff0c;5 26、段错…

selenium定位元素报错:‘WebDriver‘ object has no attribute ‘find_element_by_id‘

Selenium更新到 4.x版本后&#xff0c;以前的一些常用的代码的语法发生了改变 from selenium import webdriver browser webdriver.Chrome() browser.get(https://www.baidu.com) input browser.find_element_by_id(By.ID,kw) input.send_keys(Python)目标&#xff1a;希望通…

【Python--网络编程之TCP三次握手】

&#x1f680; 作者 &#xff1a;“码上有前” &#x1f680; 文章简介 &#xff1a;Python开发技术 &#x1f680; 欢迎小伙伴们 点赞&#x1f44d;、收藏⭐、留言&#x1f4ac; Python网络编程之[TCP三次握手] 往期内容代码见资源&#xff0c;效果图如下一、实验要求二、协…

嵌入式内核链表list_head,如何管理不同类型节点的实现

在Linux内核中&#xff0c;提供了一个用来创建双向循环链表的结构 list_head。虽然linux内核是用C语言写的&#xff0c;但是list_head的引入&#xff0c;使得内核数据结构也可以拥有面向对象的特性&#xff0c;通过使用操作list_head 的通用接口很容易实现代码的重用&#xff0…

P1219 八皇后 (dfs 表格坐标关系)

一个正常的dfs&#xff08;数据范围1-13&#xff09;&#xff0c;发现一条对角线上&#xff0c;分别符合和与差相等。因为有负数&#xff0c;这里我最开始开的是map&#xff0c;发现卡了最后一个点TLE&#xff0c;记录一下时间复杂度&#xff08; map&#xff0c;set的时间复杂…

Mysql——update更新数据的方式

注&#xff1a;文章参考&#xff1a; MySQL 更新数据 不同条件(批量)更新不同值_update批量更新同一列不同值-CSDN博客文章浏览阅读2w次&#xff0c;点赞20次&#xff0c;收藏70次。一般在更新时会遇到以下场景&#xff1a;1.全部更新&#xff1b;2.根据条件更新字段中的某部分…

同学,请实现一个扫码登录

马上要到春节了&#xff0c;小伙伴们的公司是不是已经可以申请请假调休呢&#xff1f;虽然今年刚入职没有年假(好像国家不是这么规定的&#xff0c;但也不好跟公司硬杠)&#xff0c;大小周的我已经攒了 7 天调休&#xff0c;也可以提前回家过年啦&#xff01; 即使是年底&…