- Wydzielenie synchronizacji przewijania do pluginu jQuery: jquery.autoscroll.js.
[redakcja.git] / project / static / js / jquery.autoscroll.js
1 (function($) {
2     $.fn.autoscroll = function(synchronizeWith, options) {
3         var self = $(this);
4         var selfContainer = self.parent();
5         var synchronizeWith = $(synchronizeWith);
6         var synchronizeWithContainer = synchronizeWith.parent();
7         var eventContainer = synchronizeWithContainer;
8
9         // Hack for iframes
10         if (self.is('iframe')) {
11             selfContainer = $('body', $('iframe').contents());
12             self = selfContainer;
13         }
14         
15         if (synchronizeWith.is('iframe')) {
16             eventContainer = synchronizeWith.contents();
17             synchronizeWithContainer = $('body', eventContainer);
18             synchronizeWith = synchronizeWithContainer;
19         }
20
21         synchronizeWithContainer.data('autoscroll.lastCheckedScrollTop', synchronizeWithContainer.scrollTop());
22         
23         eventContainer.scroll(function() {
24             var distanceScrolled = synchronizeWithContainer.scrollTop() - synchronizeWithContainer.data('autoscroll:lastCheckedScrollTop');
25             var percentScrolled = distanceScrolled / synchronizeWith.height();
26             selfContainer.scrollTop(selfContainer.scrollTop() + percentScrolled * self.height());
27             synchronizeWithContainer.data('autoscroll:lastCheckedScrollTop', synchronizeWithContainer.scrollTop());
28         });
29     };
30 })(jQuery);
31