/* * jquery图片轮播(焦点图)插件 * add.jena.201206291027 * edit.jena.201206300904 * edit.jena.201207051027 * edit.jena.201208090849 * edit.jena.201303141312 * version: 1.2.0314 * author: jena * demo: http://ishere.cn/demo/jquery.slidebox/ */ (function($) { $.fn.slidebox = function(options) { //默认参数 var defaults = { direction : 'left',//left,top duration : 0.6,//unit:seconds easing : 'swing',//swing,linear delay : 3,//unit:seconds startindex : 0, hideclickbar : true, clickbarradius : 5,//unit:px hidebottombar : false, width : null, height : null }; var settings = $.extend(defaults, options || {}); //计算相关数据 var wrapper = $(this), ul = wrapper.children('ul.items'), lis = ul.find('li'), firstpic = lis.first().find('img'); var li_num = lis.size(), li_height = 0, li_width = 0; //定义滚动顺序:asc/desc.add.jena.201208081718 var order_by = 'asc'; //初始化 var init = function(){ if(!wrapper.size()) return false; //手动设定值优先.add.jena.201303141309 li_height = settings.height ? settings.height : lis.first().height(); li_width = settings.width ? settings.width : lis.first().width(); wrapper.css({width: li_width+'px', height:li_height+'px'}); lis.css({width: li_width+'px', height:li_height+'px'});//add.jena.201207051027 if (settings.direction == 'left') { ul.css('width', li_num * li_width + 'px'); } else { ul.css('height', li_num * li_height + 'px'); } ul.find('li:eq('+settings.startindex+')').addclass('active'); if(!settings.hidebottombar){//add.jena.201208090859 var tips = $('
').css('opacity', 0.6).appendto(wrapper); var title = $('').html(function(){ var active = ul.find('li.active').find('a'), text = active.attr('title'), href = active.attr('href'); return $('').attr('href', href).text(text); }).appendto(tips); var nums = $('').hide().appendto(tips); lis.each(function(i, n) { var a = $(n).find('a'), text = a.attr('title'), href = a.attr('href'), css = ''; i == settings.startindex && (css = 'active'); $('').attr('href', href).text(text).addclass(css).css('borderradius', settings.clickbarradius+'px').mouseover(function(){ $(this).addclass('active').siblings().removeclass('active'); ul.find('li:eq('+$(this).index()+')').addclass('active').siblings().removeclass('active'); start(); stop(); }).appendto(nums); }); if(settings.hideclickbar){//add.jena.201206300847 tips.hover(function(){ nums.animate({top: '0px'}, 'fast'); }, function(){ nums.animate({top: tips.height()+'px'}, 'fast'); }); nums.show().delay(2000).animate({top: tips.height()+'px'}, 'fast'); }else{ nums.show(); } } lis.size()>1 && start(); } //开始轮播 var start = function() { var active = ul.find('li.active'), active_a = active.find('a'); var index = active.index(); if(settings.direction == 'left'){ offset = index * li_width * -1; param = {'left':offset + 'px' }; }else{ offset = index * li_height * -1; param = {'top':offset + 'px' }; } wrapper.find('.nums').find('a:eq('+index+')').addclass('active').siblings().removeclass('active'); wrapper.find('.title').find('a').attr('href', active_a.attr('href')).text(active_a.attr('title')); ul.stop().animate(param, settings.duration*1000, settings.easing, function() { active.removeclass('active'); if(order_by=='asc'){ if (active.next().size()){ active.next().addclass('active'); }else{ order_by = 'desc'; active.prev().addclass('active'); } }else if(order_by=='desc'){ if (active.prev().size()){ active.prev().addclass('active'); }else{ order_by = 'asc'; active.next().addclass('active'); } } }); wrapper.data('timeid', window.settimeout(start, settings.delay*1000)); }; //停止轮播 var stop = function() { window.cleartimeout(wrapper.data('timeid')); }; //鼠标经过事件 wrapper.hover(function(){ stop(); }, function(){ wrapper.data('timeid', window.settimeout(start, settings.delay*1000));//add.jena.201303141309 }); //首张图片加载完毕后执行初始化 var imgloader = new image(); imgloader.onload = function(){ imgloader.onload = null; init(); } imgloader.src = firstpic.attr('src'); }; })(jquery);