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:
- (function( $ ) {
- $.fn.kbg = function() {
-
- var element = $(this);
-
- var kbgS = { count : element.children().length,
- width : element.data('kbg-width'),
- height : element.data('kbg-height'),
- type : element.data('kbg-type'),
- speed : element.data('kbg-speed'),
- effectSpeed : element.data('kbg-effectspeed'),
- curCount : 0
- }
-
- this.css({width:kbgS.width, height:kbgS.height});
-
- if (kbgS.type == "Scrolling") {
- element.children().wrapAll("<div class='kbg_scrolling' />");
- setInterval( kbg_scroll, kbgS.speed);
- element.find('.kbg_scrolling').append(element.find('img:first-child').clone());
-
- } else if (kbgS.type == "Fading") {
- element.children().wrapAll("<div class='kbg_fading' />");
- setInterval( kbg_fade, kbgS.speed);
- element.find('.kbg_fading').append(element.find('img:first-child').clone());
- element.find('img:first-child').fadeIn(0);
- }
-
- function kbg_scroll() {
-
- kbgS.curCount++;
- element.find('.kbg_scrolling').animate({left: '-=' +kbgS.width}, kbgS.effectSpeed, function() {
- if (kbgS.curCount == kbgS.count) {
- element.find('.kbg_scrolling').css({left:0});
- kbgS.curCount = 0;
-
- }
- });
-
- }
-
- function kbg_fade() {
- kbgS.curCount++;
- element.find('.kbg_fading img:nth-child('+ (kbgS.curCount+1) +')').fadeIn(kbgS.effectSpeed, function() {
- if (kbgS.curCount == kbgS.count) {
- element.find('.kbg_fading img').hide();
- element.find('.kbg_fading img:first-child').fadeIn(0);
- kbgS.curCount = 0;
- }
- });
- }
-
-
- };
- })( jQuery )
Here is the html:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Untitled Document</title>
- <script src="jquery-1.9.0.min.js"></script>
- <script src="kbg.jQuery.js"></script>
- <style>
- .kbGallery {
- overflow:hidden;
- width:0;
- height:0;
- position:relative;
- }
- .kbGallery img {
- margin:0;
- padding:0;
-
- }
-
- .kbg_scrolling {
- width:9999px;
- position:absolute;
- }
-
- .kbg_scrolling img {
- float:left;
- }
-
-
- .kbg_fading img {
- position:absolute;
- top:0;
- left:0;
- display:none;
- }
-
- </style>
- <script>
- $(document).ready(function() {
-
- $('.kbGallery').kbg();
-
- });
- </script>
- </head>
- <body>
- <!-- Scrolling or Fading / Speed between fades / width / height -->
- <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 >
- <img src="http://placekitten.com/g/300/200" />
- <img src="http://placehold.it/300x200&text=NO" />
- <img src="http://placehold.it/300x200&text=YES" />
- <img src="http://placekitten.com/300/200" />
- <img src="http://placehold.it/300x200&text=MAYBE" />
- </div>
- <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 >
- <img src="http://placekitten.com/g/300/200" />
- <img src="http://placehold.it/300x200&text=NO" />
- <img src="http://placehold.it/300x200&text=MAYBE" />
- </div>
- </body>
- </html>