GÅ‚upi git!
[redakcja.git] / project / static / js / jquery.autoscroll.js
1 (function($) {
2     $.fn.autoscroll = function(synchronizeWith, options) {
3         var $this = $(this);
4         var self = $this;
5         var selfContainer = self.parent();
6         var synchronizeWith = $(synchronizeWith);
7         var synchronizeWithContainer = synchronizeWith.parent();
8         var eventContainer = synchronizeWithContainer;
9
10         // Hack for iframes
11         if (self.is('iframe')) {
12             selfContainer = $('body', $('iframe').contents());
13             self = selfContainer;
14         }
15         
16         if (synchronizeWith.is('iframe')) {
17             eventContainer = synchronizeWith.contents();
18             synchronizeWithContainer = $('body', eventContainer);
19             synchronizeWith = synchronizeWithContainer;
20         }
21         
22         $this.data('autoscroll:enabled', true);
23         synchronizeWithContainer.data('autoscroll:lastCheckedScrollTop', synchronizeWithContainer.scrollTop());
24         
25         eventContainer.scroll(function() {
26             if ($this.data('autoscroll:enabled')) {
27                 var distanceScrolled = synchronizeWithContainer.scrollTop() - synchronizeWithContainer.data('autoscroll:lastCheckedScrollTop');
28                 var percentScrolled = distanceScrolled / synchronizeWith.height();
29                 selfContainer.scrollTop(selfContainer.scrollTop() + percentScrolled * self.height());
30             }
31             synchronizeWithContainer.data('autoscroll:lastCheckedScrollTop', synchronizeWithContainer.scrollTop());
32         });
33     },
34     
35     $.fn.enableAutoscroll = function() {
36         $(this).data('autoscroll:enabled', true);
37     },
38         
39     $.fn.disableAutoscroll = function() {
40         $(this).data('autoscroll:enabled', false);
41     },
42     
43     $.fn.toggleAutoscroll = function() {
44         $(this).data('autoscroll:enabled', !$(this).data('autoscroll:enabled'));
45     }
46 })(jQuery);
47