Skip to content

Instantly share code, notes, and snippets.

@0x46616c6b
Created March 15, 2012 21:17
Show Gist options
  • Save 0x46616c6b/2047019 to your computer and use it in GitHub Desktop.
Save 0x46616c6b/2047019 to your computer and use it in GitHub Desktop.
function getAjaxUrl() {
return '/wp-admin/admin-ajax.php';
}
function isExternalLink(link) {
var linkurl, baseurl, href;
href = $(link).attr('href');
if (href === '#') return false;
baseurl = $('base').attr('url').split('//')[1].split('/')[0];
linkurl = href.split('//')[1].split('/')[0];
return baseurl !== linkurl;
}
function isPage(link) {
var href = $(link).attr('href');
return href.indexOf('?page_id=') !== -1;
}
function isPost(link) {
var href = $(link).attr('href');
return href.indexOf('?p=') !== -1;
}
function isHome(link) {
return $(link).parent().hasClass('menu-item-home');
}
function isArrow(link) {
return $(link).hasClass('arrow');
}
function getNumberOfArticles() {
return parseInt($('.articles').children().length);
}
function getArticlesWidth() {
return $('.post').width() * getNumberOfArticles();
}
function getPostId(dom) {
return $(dom).length !== 0 ? parseInt($(dom).attr('id').split('-')[1]) : false;
}
function hasPreviousPosting(dom) {
var postId, ajaxUrl;
postId = getPostId(dom);
if (postId === false) return false;
ajaxUrl = getAjaxUrl();
return $.ajax({
url: ajaxUrl,
type: 'GET',
data: {"action": "hasprevious", "id": postId},
success: function(data) {
return parseInt(data) === 1 ? true : false;
}
});
}
function scrollLeft(callback) {
$('.articles').animate({
left: '+=280'
}, 500);
return callback();
}
function scrollRight(callback) {
$('.articles').animate({
left: '-=280'
}, 500);
return callback();
}
function getVisibleArticlesWidth() {
return Math.floor($(window).width() / 280) * 280;
}
$(document).ready(function() {
var bodyHeight;
POS = 0;
POSTCOUNT = $('#postcount').val();
$('body.home .visible').width(getVisibleArticlesWidth());
$('body.home .articles').width(getArticlesWidth());
$.Placeholder.init({ color:'#d71d7e' });
$(window).keydown(function (e) {
switch (e.keyCode) {
case 37:
$('.previous').trigger('click');
break;
case 39:
$('.next').trigger('click');
break;
}
});
$(window).resize(function() {
$('.visible').width(getVisibleArticlesWidth());
});
$(document).bind('mousemove', function (e) {
bodyHeight = $('body').height();
$('.arrow').css('top', (e.pageY - 35) + 'px');
if (e.pageY < 80 || e.pageY > bodyHeight - 50) {
$('.arrow.next').css('display', 'none');
} else {
$('.arrow.next').css('display', 'block');
}
if (e.pageY < 250 || e.pageY > bodyHeight - 100) {
$('.arrow.previous').css('display', 'none');
} else {
$('.arrow.previous').css('display', 'block');
}
});
$('a').click(function (e) {
var link;
e.preventDefault();
link = $(this);
if (!isExternalLink(link)) {
if (isPost(link)) {
$.get($(link).attr('href'),function(data) {
console.log(data);
});
}
if (isPage(link) || isHome(link)) {
window.location = $(link).attr('href');
}
if (isArrow(link)) {
console.log(POS);
console.log(POSTCOUNT);
if (link.hasClass('next')) {
if (POS < POSTCOUNT) {
$('.arrow.previous').animate({
opacity: 1
}, 400);
scrollRight(function() {
POS += 1;
});
} else {
$('.arrow.next').animate({
opacity: 0
}, 400);
}
}
if (link.hasClass('previous')) {
if (POS > 0) {
$('.arrow.next').animate({
opacity: 1
}, 400);
scrollLeft(function() {
POS -= 1;
});
} else {
$('.arrow.previous').animate({
opacity: 0
}, 400);
}
}
}
} else {
window.location = $(link).attr('href');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment