$(function () { //鼠标移入显示左右箭头和关闭按钮 var timer = ''; $('.cooldog_container').mouseover(function () { $('.btn_left').show('1000'); $('.btn_right').show('1000'); clearinterval(timer); }).mouseleave(function () { $('.btn_left').hide('1000'); $('.btn_right').hide('1000'); timer = setinterval(btn_right, 4000); }); //点击关闭隐藏轮播图 $('.btn_close').on('click', function () { $('.cooldog_container').hide('1000'); }); var arr = ['p1', 'p2', 'p3', 'p4', 'p5']; var index = 0; //上一张 $('.btn_left').on('click', function () { btn_left(); }); //下一张 $('.btn_right').on('click', function () { btn_right(); }); //图片自动轮播 timer = setinterval(btn_right, 4000); //点击上一张的封装函数 function btn_left() { arr.unshift(arr[4]); arr.pop(); $('.cooldog_content li').each(function (i, e) { $(e).removeclass().addclass(arr[i]); }) index--; if (index < 0) { index = 4; } show(); } //点击下一张的封装函数 function btn_right() { arr.push(arr[0]); arr.shift(); $('.cooldog_content li').each(function (i, e) { $(e).removeclass().addclass(arr[i]); }) index++; if (index > 4) { index = 0; } show(); } //点击底部的按钮切换图片 $('.buttons a').each(function () { $(this).on('click', function () { var myindex = $(this).index(); var mindex = myindex - index; if (mindex == 0) { return; } else if (mindex > 0) { var newarr = arr.splice(0, mindex); //$.merge() 函数用于合并两个数组内容到第一个数组 arr = $.merge(arr, newarr); $('.cooldog_content li').each(function (i, e) { $(e).removeclass().addclass(arr[i]); }) index = myindex; show(); } else if (mindex < 0) { //reverse() 方法用于颠倒数组中元素的顺序。 arr.reverse(); var oldarr = arr.splice(0, -mindex); arr = $.merge(arr, oldarr); arr.reverse(); $('.cooldog_content li').each(function (i, e) { $(e).removeclass().addclass(arr[i]); }) index = myindex; show(); } }) }) //底部按钮高亮 function show() { $('.buttons a').eq(index).addclass('color').siblings().removeclass('color'); } })