ajax自动轮播图-可点击-鼠标悬停止等-完善版
发布日期:2021-06-30 11:50:38
浏览次数:2
分类:技术文章
本文共 2421 字,大约阅读时间需要 8 分钟。
html文件
轮播图
js文件
ajax('./data/img.json', 'get', '', function(res) { res = JSON.parse(res); // console.log(res) for (var i = 0; i < res.length; i++) { //创建li var li = document.createElement('li'); li.innerHTML = ''; imgUl.appendChild(li); //序号 var span = document.createElement('span'); span.innerHTML = i + 1; p.appendChild(span); } //初始默认样式 设置ul宽度 p.children[0].className = 'active'; imgUl.style.width = res.length * imgUl.children[0].offsetWidth + 'px'; //自动轮播 var n = 0; //动画 function auto() { if (n >= res.length) { imgUl.style.left = 0; n = 0; } n++; var end = -n * imgUl.children[0].offsetWidth; bufferMove(imgUl, { left: end }); //序号 for (var i = 0; i < p.children.length; i++) { p.children[i].className = ''; } if (n >= res.length) { p.children[0].className = 'active'; } else { p.children[n].className = 'active'; } } //无缝轮播 var fade = imgUl.children[0].cloneNode(true); imgUl.appendChild(fade); imgUl.style.width = (res.length + 1) * imgUl.children[0].offsetWidth + 'px'; //序号切换 for (var i = 0; i < p.children.length; i++) { p.children[i].idx = i; p.children[i].onmouseover = function() { var end = -this.idx * imgUl.children[0].offsetWidth; bufferMove(imgUl, { left: end }); //序号 for (var i = 0; i < p.children.length; i++) { p.children[i].className = ''; } p.children[this.idx].className = 'active'; n = this.idx; } } //箭头切换 barS[0].onclick = function() { if (n <= 0) { imgUl.style.left = -res.length * imgUl.children[0].offsetWidth + 'px'; n = res.length; } n--; var end = -n * imgUl.children[0].offsetWidth; bufferMove(imgUl, { left: end }); //序号 for (var i = 0; i < p.children.length; i++) { p.children[i].className = ''; } if (n >= res.length) { p.children[0].className = 'active'; } else { p.children[n].className = 'active'; } } barS[1].onclick = function() { auto(); } //开始 var timer = setInterval(auto, 3000); //鼠标移入移出 var wrap = imgUl.parentNode; wrap.onmouseover = function() { clearInterval(timer); } wrap.onmouseout = function() { timer = setInterval(auto, 3000); }})//获取ulvar imgUl = document.querySelector('#wrap>ul');//获取pvar p = document.querySelector('#wrap>p');//箭头var barS = document.querySelectorAll(".bar>div");
json文件
[ "img/1.jpg", "img/2.jpg", "img/3.jpg", "img/4.jpg"]
转载地址:https://jackiehao.blog.csdn.net/article/details/105588275 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年04月27日 23时00分18秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
决策树 Decision tree
2019-04-30
nominal和ordinal & 数据处理中四种基本数据类型
2019-04-30
Python 实现 Cross-validation
2019-04-30
Grid SearchCV(网格搜索)& Python实现
2019-04-30
ROS相关知识
2019-04-30
单目深度估计 monodepth2模型 代码
2019-04-30
位图索引Bitmap indexes
2019-04-30
YOLO算法(二)—— Yolov2 & yolo9000
2019-04-30
YOLO算法(三)—— Yolov3 & Yolo系列网络优缺点
2019-04-30
Python的__future__模块
2019-04-30
计算机视觉中的cost-volume的概念具体指什么(代价体积)
2019-04-30
启发函数heuristic 与 A*
2019-04-30
Image Pyramid(图像金字塔)
2019-04-30
Oracle 作业记录
2019-04-30