jQuery-ajax-json实现自动+序号+点击轮播图
发布日期:2021-06-30 11:50:47
浏览次数:2
分类:技术文章
本文共 2136 字,大约阅读时间需要 7 分钟。
轮播图
js
$(function() { //jQuery-Ajax数据请求 ./data/img.json $.get('./data/img.json', function(res) { // console.log(res) $.each(res, function(idx, item) { console.log(idx, item) var $li = $(''); $('ul').append($li); var $span = $('' + (idx + 1) + ''); $('p').append($span); }) //初始默认样式 宽 $('p').children('span:nth-child(1)').addClass('active'); $('ul').width(res.length * $('li').width()); var n = 0; function auto() { if (n >= res.length) { $('ul').animate({left: 0}, 0); n = 0; } n++; //图片移动 var end = -n * $('li').width(); $('ul').animate({left: end}, 2000); //序号移动 $('span').removeClass('active'); if (n >= res.length) { $('span:nth-child(1)').addClass('active'); } else { $('span:nth-child(' + (n + 1) + ')').addClass('active'); } } //无缝轮播 var $fade = $('li:nth-child(1)').clone(true); $('ul').append($fade); $('ul').width($('ul li').length * $('li').width()); //序号切换 $('span').on('mouseover', function(ev) { n = $(ev.target).text()-1; // console.log(this,$(ev.target).text(),end) var end = -n * $('li').width(); $('ul').animate({left: end}, 1500); //序号移动 $('span').removeClass('active'); if (n >= res.length) { $('span:nth-child(1)').addClass('active'); } else { $('span:nth-child(' + (n + 1) + ')').addClass('active'); } }); //箭头轮播 $('.bar').on('click','.left,.right',function(){ if($(this).hasClass('left')){ if(n<=0){ $('ul').animate({left: -(res.length * $('li').width())}, 0); n = res.length; } n--; var end = -n * $('li').width(); $('ul').animate({left: end}, 1500); $('span').removeClass('active'); if (n >= res.length) { $('span:nth-child(1)').addClass('active'); } else { $('span:nth-child(' + (n + 1) + ')').addClass('active'); } }else if($(this).hasClass('right')){ auto(); } }) //开始自动动画 var timer = setInterval(auto, 3000); //鼠标移入 合并 $('#wrap').hover(function() { clearInterval(timer); }, function() { timer = setInterval(auto, 3000); }); })})
转载地址:https://jackiehao.blog.csdn.net/article/details/105757214 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月13日 10时48分19秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
CSS在性能优化方面的实践
2019-04-30
CSS3动画(简单动画的实现,如旋转等)
2019-04-30
base64的原理及优缺点
2019-04-30
几种常见的CSS布局
2019-04-30
Java环境的搭建
2019-04-30
java语法基础
2019-04-30
对面向对象的理解和this static的理解
2019-04-30
自学练习题
2019-04-30
面向对象的三大特性,动态绑定,抽象,接口
2019-04-30
异常和数组
2019-04-30
容器和String、Date、Calendar
2019-04-30
java异常知识点总结
2019-04-30
C++ 虚函数表
2019-04-30
浅谈C++中指针和引用的区别
2019-04-30
C++学习笔记
2019-04-30
XlFileFormat 枚举 (Excel)
2019-04-30
SECP256K1签名
2019-04-30
WINDOWS匿名管道
2019-04-30
非模式对话框的问题
2019-04-30
C++解析XML, TINYXML2的使用
2019-04-30