
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>轮播图</title></head><style>.slider {width: 500px;margin: auto;padding: 0 10px;margin-top:20px;}.slider .imgs {/* 图片容器必须要有高度,否则下面图片不能正常显示 */height: 300px;}.slider .imgs img {/* 图片完全充满父级空间显示 */height: 100%;width: 100%;/* 默认图片全部隐藏,只有有active的图片才显示 */display: none;}.slider .imgs img:hover {cursor: pointer;}/* 默认显示第一张 */.slider .imgs img.active {display: block;}/* 轮播图按钮组 */.slider .btns {/* 按钮水平一排显示,用flex,且水平居中 */display: flex;place-content: center;}.slider .btns span {/* 按钮宽高相同,确定显示成一个正圆 */width: 8px;height: 8px;/* 加上红色背景和数字是为了布局时可以看到,一会更去掉 */background-color: rgba(255, 255, 255, 255);/* 50%可确保显示为正圆 */border-radius: 50%;/* 按钮上外边距负值,可将它上移,可移动到图片中下方 */margin: -12px 3px 5px;/* z-index: 99; */}.slider .btns span.active {background-color: red;}</style><body><!-- 1.写个轮播图 --><!-- 2.写个图片盒子 --><!-- 3.写个按钮盒子 --><div class="slider"><div class="imgs"><img src="images/1.jpg" alt="" data-index="1"><img src="images/2.jpg" alt="" data-index="2" class="active"><img src="images/3.jpg" alt="" data-index="3"></div><div class="btns"><span data-index="1" onclick="setActive()" class="active"></span><span data-index="2" onclick="setActive()"></span><span data-index="3" onclick="setActive()"></span></div></div></body><script>//获取图片按钮const imgs = document.querySelectorAll('.slider .imgs img');const btns = document.querySelectorAll('.slider .btns span');//设置激活状态function setActive() {//1.获取到当前点击的按钮console.log(event.target);//2.把图片和按钮的active都清空imgs.forEach((img) => img.classList.remove("active"))btns.forEach(function (btn) {btn.classList.remove('active');})//3.把active显示到当前点击的按钮上,和对应的data-index上event.target.classList.add('active')imgs.forEach(function (key) {if (key.dataset.index === event.target.dataset.index) {key.classList.add('active')}})}//实现自动播放//需要定时器 setInterval()除了回调和时间外,还可传入第三个参数,做为回调的参数let sj = setInterval(function (arr) {//1.头部删除let a = arr.shift();//2.尾部相加arr.push(a);//console.log(arr);//.事件派发btns[a].dispatchEvent(new Event('click'))}, 2000, Object.keys(btns))// console.log('这是按钮',Object.keys(btns))// console.log('这是图片',Object.keys(imgs))//当鼠标移动到图片时候停止轮播//给定时器个变量//获取当前图片盒子const himg = document.querySelector('.slider .imgs');console.log(himg);//鼠标移动到当前盒子是清除定时器 mouseover(移入)himg.addEventListener("mouseover", function () {clearInterval(sj)console.log('停止滚动')});//当鼠标离开是继续轮播//获取当前图片盒子 上面以及获取到了我们直接拿来使用//鼠标离开到当前盒子是继续轮播 mouseout(移除)himg.addEventListener("mouseout", function () {sj = setInterval(function (arr) {let a = arr.shift();arr.push(a);btns[a].dispatchEvent(new Event('click'))}, 2000, Object.keys(btns))console.log('继续滚动')})//向左点击//获取想左点击按钮//</script></html>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>xhr.传参</title></head><body><button onclick="getUser1(this)">用户信息: xhr</button></body><script>// 1. 传统 XHR// 1. 创建对象: new XMLHttpRequest();// 2. 响应类型: xhr.responseType = "json";// 3. 配置参数: xhr.open("GET", url, true);// 4. 请求回调: xhr.onload = () => console.log(xhr.response);// 5. 失败回调: xhr.onerror = () => console.log("Error");// 6. 发起请求: xhr.send(null);// xhr 至少监听2个事件: load,error, 调用2个函数: open,send// post请求,需要设置一下请求头与请求的数据,其它与get请求完全相同function getuser1() {//1.创建对象:const xhr = new XMLHttpRequest();// 2. 响应类型xhr.responseType = "json";// 3. 配置参数let url = "hhttp://xhr411.cc/users.php?id=2";xhr.open("GET",url,)// 4. 请求回调:xhr.onload = function() {console.log(xhr.response)}//5.失败回调xhr.onerror = function () {console.log('请求失败')}//6. 发起请求xhr.send(null);}</script></html>
相关推荐
© 2020 asciim码
人生就是一场修行