Created
January 22, 2014 10:37
-
-
Save antixrist/8556648 to your computer and use it in GitHub Desktop.
Bookmarklet бесконечной прокрутки для photosight.ru
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(window, document, $, undefined){ | |
(function(b,c){var $=b.jQuery||b.Cowboy||(b.Cowboy={}),a;$.throttle=a=function(e,f,j,i){var h,d=0;if(typeof f!=="boolean"){i=j;j=f;f=c}function g(){var o=this,m=+new Date()-d,n=arguments;function l(){d=+new Date();j.apply(o,n)}function k(){h=c}if(i&&!h){l()}h&&clearTimeout(h);if(i===c&&m>e){l()}else{if(f!==true){h=setTimeout(i?k:l,i===c?e-m:e)}}}if($.guid){g.guid=j.guid=j.guid||$.guid++}return g};$.debounce=function(d,e,f){return f===c?a(d,e,false):a(d,f,e!==false)}})(this); | |
var container = '.photolist'; | |
var pageNext = '.g-paginator-next'; | |
var pageList = '.pageslist'; | |
var currentPageClass = 'current'; | |
var $container = $(container); | |
var $pageNext = $(pageNext); | |
var $pageList = $(pageList); | |
var $w = $(window); | |
var $loadingItem = $('<li>').css({ | |
display: 'block', | |
textAlign: 'center', | |
color: 'black', | |
width: '100%', | |
fontSize: '20px', | |
margin: '20px 0' | |
}).html('Загрузка...').addClass('loading'); | |
var loading, containerHeight, containerOffset, windowHeight, scrollDir, scrollPos, oldScrollPos; | |
var calcScrollDir = function () { | |
if (!oldScrollPos) oldScrollPos = scrollPos; | |
scrollPos = $w.scrollTop(); | |
if (scrollPos > oldScrollPos) { | |
scrollDir = 1; | |
} else if (scrollPos < oldScrollPos) { | |
scrollDir = -1; | |
} else { | |
scrollDir = 0; | |
} | |
oldScrollPos = scrollPos; | |
} | |
var calc = function (e) { | |
containerHeight = $container.height(); | |
containerOffset = $container.offset().top; | |
windowHeight = $w.height(); | |
} | |
var onscroll = function (e) { | |
console.log(e); | |
calcScrollDir(); | |
console.log(scrollPos, windowHeight, containerHeight, containerOffset); | |
if ( | |
scrollDir > -1 && | |
(scrollPos + windowHeight) > (containerHeight + containerOffset) && | |
!loading | |
) { | |
console.log('load!'); | |
loadData(); | |
} | |
}; | |
var loadData = function () { | |
if (loading) return; | |
loading = true; | |
$loadingItem.appendTo($container); | |
$.get($pageNext.attr('href'), function (data, status) { | |
$(container, data).find('li').appendTo($container); | |
$pageNext.attr('href', $(pageNext, data).attr('href')); | |
$container.children('.loading').detach(); | |
defineCurrentPage(); | |
loading = false; | |
calc(); | |
}); | |
}; | |
var defineCurrentPage = function () { | |
var $current = $pageList.children('.'+ currentPageClass); | |
var $next = $current.next('a'); | |
if ($next.length) { | |
$current.removeClass(currentPageClass); | |
$next.addClass(currentPageClass); | |
} else { | |
$current.removeClass(currentPageClass) | |
.clone().html(parseInt($current.html(), 10) + 1).insertAfter($current).addClass(currentPageClass); | |
$pageList.children(':first-child').remove(); | |
} | |
} | |
$w | |
.on('resize', function (e) { | |
calc(e); | |
onscroll(e); | |
}) | |
.on('scroll', $.throttle(250, onscroll)); | |
calc(); | |
$w.trigger('scroll'); | |
})(window, document, jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment