Running the plugin on a class selector works on the first element, but not the rest.

Running the plugin on a class selector works on the first element, but not the rest.

So basically, I'm making a little personal plugin, the idea is to have little galleries, divs with images in them. These divs have custom data attibutes for width, height, speed of transition, etc. I want to make set up super simple so anyone can just have a div with images in them, set a few data attributes and they're good to go. 

There is a problem however that I cannot work out - I'm going like this 

(where kbg is the name of my plugin)

$('.kbGallery').kbg();

This works fine for the first element with the class of '.kbGallery'. Not so much for the others. 

If I give all the individual elements a class of '.kbGallery' and an unique id, they work fine :?

$('#one').kbg();
$('#two').kbg();

What am I doing wrong? Can you see anything bad about my code too? Any suggestions? 

Here is the code for the plugin: 

  1. (function( $ ) {

  2. $.fn.kbg = function() {
  3. var element = $(this);
  4. var kbgS = { count : element.children().length,
  5. width : element.data('kbg-width'),
  6. height : element.data('kbg-height'),
  7. type : element.data('kbg-type'),
  8. speed : element.data('kbg-speed'),
  9. effectSpeed : element.data('kbg-effectspeed'),
  10. curCount : 0
  11. }
  12. this.css({width:kbgS.width, height:kbgS.height});
  13. if (kbgS.type == "Scrolling") { 
  14. element.children().wrapAll("<div class='kbg_scrolling' />");
  15. setInterval( kbg_scroll, kbgS.speed);
  16. element.find('.kbg_scrolling').append(element.find('img:first-child').clone());
  17. } else if (kbgS.type == "Fading") { 
  18. element.children().wrapAll("<div class='kbg_fading' />");
  19. setInterval( kbg_fade, kbgS.speed);
  20. element.find('.kbg_fading').append(element.find('img:first-child').clone());
  21. element.find('img:first-child').fadeIn(0);
  22. }
  23. function kbg_scroll() {
  24. kbgS.curCount++;
  25. element.find('.kbg_scrolling').animate({left: '-=' +kbgS.width}, kbgS.effectSpeed, function() {
  26. if (kbgS.curCount == kbgS.count) {
  27. element.find('.kbg_scrolling').css({left:0});
  28. kbgS.curCount = 0;
  29. }
  30. });
  31. }
  32. function kbg_fade() {
  33. kbgS.curCount++;
  34. element.find('.kbg_fading img:nth-child('+ (kbgS.curCount+1) +')').fadeIn(kbgS.effectSpeed, function() {
  35. if (kbgS.curCount == kbgS.count) {
  36. element.find('.kbg_fading img').hide();
  37. element.find('.kbg_fading img:first-child').fadeIn(0);
  38. kbgS.curCount = 0;
  39. }
  40. });
  41. }
  42. };

  43. })( jQuery )

Here is the html: 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5. <title>Untitled Document</title>
  6. <script src="jquery-1.9.0.min.js"></script>
  7. <script src="kbg.jQuery.js"></script>


  8. <style>
  9. .kbGallery {
  10. overflow:hidden;
  11. width:0;
  12. height:0;
  13. position:relative;
  14. }
  15. .kbGallery img {
  16. margin:0;
  17. padding:0;
  18. }
  19. .kbg_scrolling { 
  20. width:9999px;
  21. position:absolute;
  22. }
  23. .kbg_scrolling img {
  24. float:left;
  25. }
  26. .kbg_fading img {
  27. position:absolute;
  28. top:0;
  29. left:0;
  30. display:none;
  31. }
  32. </style>

  33. <script>
  34. $(document).ready(function() {
  35. $('.kbGallery').kbg(); 
  36. });
  37. </script>


  38. </head>

  39. <body>

  40. <!-- Scrolling or Fading / Speed between fades / width / height --> 

  41. <div id="one" class="kbGallery" data-kbg-type="Scrolling" data-kbg-speed=4000 data-kbg-effectspeed=500 data-kbg-width=300 data-kbg-height=200 >
  42. <img src="http://placekitten.com/g/300/200" />
  43. <img src="http://placehold.it/300x200&text=NO" />
  44. <img src="http://placehold.it/300x200&text=YES" />
  45. <img src="http://placekitten.com/300/200" />
  46. <img src="http://placehold.it/300x200&text=MAYBE" />
  47. </div>


  48. <div id="two" class="kbGallery" data-kbg-type="Fading" data-kbg-speed=2000 data-kbg-effectspeed=200 data-kbg-width=200 data-kbg-height=100 >
  49. <img src="http://placekitten.com/g/300/200" />
  50. <img src="http://placehold.it/300x200&text=NO" />

  51. <img src="http://placehold.it/300x200&text=MAYBE" />
  52. </div>

  53. </body>
  54. </html>