Multiple instances of plugin
Hey there,
I'm trying to figger out how to make a simple plugin, and make it work for multiple sections on my current project.
Let's say, I've got two sets of images that I want to horizontally scroll when I click on there own navigation-arrows. The markup could look something like this:
- <div id="container_1" class="container">
- <div class="nav">
- <a href="#" class="prev">previous</a>
- <a href="#" class="prev">next</a>
- </div>
- <div class="item"><img src="" alt="" /></div>
- <div class="item"><img src="" alt="" /></div>
- <div class="item"><img src="" alt="" /></div>
- <div class="item"><img src="" alt="" /></div>
- <div class="item"><img src="" alt="" /></div>
- </div>
- <div id="container_2" class="container">
- <div class="nav">
- <a href="#" class="prev">previous</a>
- <a href="#" class="prev">next</a>
- </div>
- <div class="item"><img src="" alt="" /></div>
- <div class="item"><img src="" alt="" /></div>
- <div class="item"><img src="" alt="" /></div>
- <div class="item"><img src="" alt="" /></div>
- <div class="item"><img src="" alt="" /></div>
- </div>
Now.. Using jQuery, I want to call something like:
- $("#container_1").scroller();
- $("#container_2").scroller();
or even better:
- $(".container").scroller();
I've kinda got this working, but there's defiantly something wrong. Here's what I got (simplified):
- (function($)
- {
- $.fn.scroller = function(options) {
- var defaults = {
- container_width: 900,
- container_height: 400,
- item_width: 900,
- items_per_view: 1,
- duration: 200
- };
-
- if (options) { $settings = $.extend(defaults, options) };
-
- return this.each(function() {
- $inner = $(this).find('.inner');
- $nav = $(this).find('.nav');
- $items = $(this).find('.item');
- $sliding = false;
-
- $(nav).find('.prev').click(function() {
- if(sliding === false && $.fn.scroller.currentItem() > 0) {
- $.fn.scroller.toItem('prev');
- }
- this.blur();
- return false;
- });
-
- $(nav).find('.next').click(function() {
- if(sliding === false && $.fn.scroller.currentItem() < (items.length-options.items_per_view)) {
- $.fn.scroller.toItem('next');
- }
- this.blur();
- return false;
- });
- });
-
- };
-
- $.fn.scroller.currentItem = function() {
- return 0-(parseFloat($($inner).css('left')) / $settings.item_width);
- }
- $.fn.scroller.toItem = function(direction) {
- var target_item;
- if(direction=='next') {
- target_item = $.fn.scroller.currentItem() + 1;
- } else {
- target_item = $.fn.scroller.currentItem() - 1;
- }
-
- var target_margin = 0-(target_item*$settings.item_width);
- $sliding = true;
- $($inner).stop().animate(
- { "left": target_margin },
- {
- duration: $settings.duration,
- complete: function() {
- $sliding = false;
- }
- }
- );
-
- }
-
- })(jQuery);
This kinda works, but the problem is that the click-event are overwritten, so both navigations will control the second container (instead of their own). What should be the right approach to make this work for each instance specifically? And what should be the way to set variables for each instance, for starters? Thanks!