jquery.slide.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. (function($, window) {
  2. function Slide(obj, prefix) {
  3. this.pics = obj.find('li');
  4. var nav = obj.find('.' + prefix + '-nav');
  5. nav.append(new Array(this.pics.length + 1).join('<a></a>'));
  6. this.dots = nav.find('a');
  7. this.currCls = prefix + '-curr';
  8. var slide = this;
  9. this.dots.mouseover(function() {
  10. slide.change($(this).index());
  11. });
  12. }
  13. Slide.prototype = {
  14. change: function(i, speed) {
  15. this.i = i;
  16. this.dots.eq(i).addClass(this.currCls).siblings('a').removeClass(this.currCls);
  17. this.pics.eq(i).stop(true, true).fadeIn(speed).siblings('li').fadeOut(speed);
  18. return this;
  19. },
  20. next: function() {
  21. if (++this.i >= this.pics.length) {
  22. this.i = 0;
  23. }
  24. return this.change(this.i, 600);
  25. },
  26. start: function(speed) {
  27. var slide = this;
  28. this.timer = window.setInterval(function() {
  29. slide.next();
  30. }, speed);
  31. return this;
  32. },
  33. pause: function() {
  34. window.clearInterval(this.timer);
  35. return this;
  36. }
  37. };
  38. var defaults = {
  39. speed: 3000,
  40. width: '670px',
  41. height: '240px',
  42. prefix: 'slide'
  43. };
  44. $.fn.slide = function(options) {
  45. options = $.extend({}, defaults, options);
  46. this.css({width: options.width, height: options.height});
  47. var slide = new Slide(this, options.prefix);
  48. this.hover(function() {
  49. slide.pause();
  50. }, function() {
  51. slide.start(options.speed);
  52. });
  53. return slide.change(0, 0).start(options.speed);
  54. };
  55. $.fn.slide.defaults = defaults;
  56. })(jQuery, this);