Hi, I'm attempting to use a mixture of jQuery and CSS to make the sand in an hourglass appear to flow as you scroll down the page. I basically have two 'sand' images, and use jQuery to change the height of their containers as the user scrolls.
<!-- Load jQuery -->
<script src="jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<!-- Handle Hourglass Scrolling -->
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
// Set value of topStart and bottomStart to equal to y-position of the equivilent divs
var topStart = parseInt($('#top-sand').css('top'));
var bottomStart = parseInt($('#bottom-sand').css('top'));
// Calculate the maximum height of the sand relative to the window size
var sandHeight = $(window).height()*0.22;
// setValues sets positions based on window size
function setValues() {
topStart = parseInt($('#top-sand').css('top'));
bottomStart = parseInt($('#bottom-sand').css('top'));
sandHeight = $(window).height()*0.22;
var hourglassWidth = $('#hourglass #outline img').css('width');
var leftMargin = $(window).width()*0.5+ 320;
$('#top-sand').height(22+"%");
$('#top-sand img').height(sandHeight)
$('#bottom-sand img').height(sandHeight)
$('#hourglass').css({left:leftMargin+"px"})
$('#trace').text(hourglassWidth)
// contentMarginLeft = $('#hourglass #outline').width();
// $('#content').text(contentMarginLeft);
// css({"margin-left": contentMarginLeft + "px"});
};
setValues();
// This listens for a window scroll event and then handles the height and position of the sand in the Hourglass
$(window).scroll(function () {
// Calculating the position of the scrollbar
var doc = $("body"),
scrollPosition = $("body").scrollTop(),
pageSize = $("body").height(),
windowSize = $(window).height(),
fullScroll = pageSize - windowSize;
percentageScrolled = (scrollPosition / fullScroll);
// Calculating the Y-positions of the two sand piles
var topPosition = topStart+(22*percentageScrolled);
var bottomPosition = bottomStart-(22*percentageScrolled);
// Updating the sand piles
$('#top-sand').height(22-(22*percentageScrolled)+"%")
.css({top: topPosition+"%"});
$('#bottom-sand').height(22*percentageScrolled+"%")
.css({top:bottomPosition+"%"});
});
// This listens for a window resize event and then reconfigures the layout
$(window).bind('resize', function() {
// Reconfigure layout
});
});
</script>
It's been working fine in Safari (although every so often there's a glitch where the height of the top image changes, but it's y position doesn't), but it hasn't been working in Firefox at all. You can see the site here http://www.chris-armstrong.com/ticktalk
I'm pretty new to jQuery, so apologies if it's a complete noob error.
Thanks in advance.