$(document).ready(function () { $(".banner").duzhanbanner({ showclass: ".banner", showspanindex: ".banner_span", leftbtn: ".ban_left", rightbtn: ".ban_right"}); $(".banner ul li").eq(0).find("img").addclass("addcur"); $(".banner ul li .banwenzi").eq(0).addclass("wzaddcur"); }); (function ($) { $.fn.duzhanbanner = function (options) { var defaults = { showtime: "8000", showclass: "showbanner", showspanindex: "", }; var showindex = 0; var options = $.extend(defaults, options); var showlist = $(options.showclass + " ul li"); showlist.width($(".banner ul li").width()); var showfirst = showlist.eq(0); var swidth = showfirst.width(); if (showfirst.height() > 0) { $(options.showclass).height(showfirst.height()); } var widthbl = showfirst.height() / showfirst.width(); var len = showlist.length; var index = 0; var pictimer; var showtimeline; var shownowclass = options.showclass; var showcur = "cur"; showlist.show(); $(options.showclass + " ul").css("position", "absolute"); $(options.showclass + " ul").width(showlist.width() * (len + 1)); $(options.showspanindex).html(""); for (var i = 0; i < len; i++) { $(options.showspanindex).append(""); } for (var i = 0; i < len; i++) { showlist.eq(i).find("img").attr("src", showlist.eq(i).find("img").attr("rel")); } var showspan = $(options.showspanindex + " span"); showspan.first().addclass(showcur); pictimer = setinterval(automove, options.showtime); $(window).resize(function () { clearinterval(pictimer); //swidth = $(window).width(); if( $(window).width() <1270 && $(window).width() > 750 ){ swidth=750; }else{ swidth=$(window).width(); } showlist.width(swidth); $(options.showclass + " ul").width(showlist.width() * (len + 1)); $(options.showclass).height($(".banner ul li").height()); //alert($(".banner ul li").height()); $(options.showclass + " ul").height($(".banner ul li").height()); showfirpic(); pictimer = setinterval(automove, options.showtime); }); $(showspan).mouseenter(function () { clearinterval(pictimer); index = $(showspan).index(this); showmove(index); pictimer = setinterval(automove, options.showtime); }) function automove() { index++; if (index >= len) { showfirpic(); index = 0; } else { showmove(index); } } function showmove(index) { $(".banner ul li img").removeclass("addcur"); $(".banner ul li .banwenzi").removeclass("wzaddcur"); $(".bannershuzi span").html("0"+(index+1)); if (index < len) { var nowleft = -index * swidth; $(options.showclass + " ul").stop(true, false).animate({ "left": nowleft },800, function () { $(".banner ul li").eq(index).find("img").addclass("addcur"); $(".banner ul li .banwenzi").eq(index).addclass("wzaddcur"); }); showspan.removeclass(showcur).eq(index).addclass(showcur); } else { showfirpic(); index = 0; } } $(".ban_left").click(function(){ clearinterval(pictimer); index--; if(index>=0){ } else{ index=len-1; } showmove(index); }) $(".ban_right").click(function(){ clearinterval(pictimer); index++; if(index>=len){ index=0; } else{ } showmove(index); }) function showfirpic() { $(shownowclass + " ul").append(showfirst.clone()); var nowleft = -len * swidth; $(shownowclass + " ul").stop(true, false).animate({ "left": nowleft }, 500, function () { showspan.removeclass(showcur).eq(0).addclass(showcur); $(this).css("left", "0px"); $(shownowclass + " ul li:last").remove(); }); } } })(jquery)