Skip to content

Instantly share code, notes, and snippets.

@drifterz28
Created August 10, 2014 18:03
Show Gist options
  • Select an option

  • Save drifterz28/3ca98d73e1b709096ab1 to your computer and use it in GitHub Desktop.

Select an option

Save drifterz28/3ca98d73e1b709096ab1 to your computer and use it in GitHub Desktop.
some js that I would like to make into something wonderful.
$(function () {
function parallax(elm, move) {
window.addEventListener('scroll', function(){
var win_offset = window.pageYOffset;
var bg_scroll = document.querySelectorAll(elm);
var bg_scroll_len = bg_scroll.length;
var parallax = win_offset / parseFloat('1.' + move);
for (var i = bg_scroll_len - 1; i >= 0; i--) {
bg_scroll[i].style.backgroundPosition = '0 -' + parallax + 'px';
}
});
}
function pushHash(hash) {
if(history.pushState) {
history.pushState(null, null, '#' + hash);
} else {
location.hash = hash;
}
}
function scrollToHash(anchorID) {
var anchorPos = $('#' + anchorID).offset();
$('html, body').animate({scrollTop: anchorPos.top}, 400, function () {
pushHash(anchorID);
});
}
function changeChapter(direction) {
var hash = location.hash.slice(1);
var $hash_id = $('#' + hash);
if(direction === 'next') {
if ($hash_id.next().attr('id')) {
scrollToHash($hash_id.next().attr('id'));
}
} else {
if ($hash_id.prev().attr('id')) {
scrollToHash($hash_id.prev().attr('id'));
}
}
}
$(window).on('scroll', function(e){
e.preventDefault();
var win_scroll_top = $(window).scrollTop();
$('.js-chapters').each(function() {
var $this = $(this);
var elm_id = $this.attr('id');
var elm_pos = $this.position().top;
var elm_height = $this.height();
if (elm_pos <= win_scroll_top && (elm_height + elm_pos) > win_scroll_top) {
pushHash(elm_id);
}
});
});
// TODO: IE9 fallback with http://andreruffert.github.io/rangeslider.js/
$('.js-font_slider').on('change', function (){
var font_size = $(this).val();
$('.js-report_interpretation_wrapper').css('font-size', font_size + 'px');
});
$('.js-arrow_up').on('click', changeChapter.bind(this, 'prev'));
$('.js-arrow_down').on('click', changeChapter.bind(this, 'next'));
$('a[href^="#"]').on('click', function (e) {
e.preventDefault();
scrollToHash($(this).attr('href').slice(1));
});
$('.js-table_of_contents').on('click', function (e){
$(this).toggleClass('open');
e.stopPropagation();
$(document).one('click', function() {
$('.js-table_of_contents').toggleClass('open');
});
});
$('.js-more_options').on('click', function (e){
$(this).toggleClass('open');
e.stopPropagation();
//$(document).one('click', function() {
// $('.js-more_options').toggleClass('open');
//});
});
parallax('.js-parallax', 4);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment