wonderful helpers!
I'm trying to create a timeline element that animates the CSS width property on #marker based on scroll position. I'm pretty new at jQuery and I know I probably have a few errors... Sorry. On the up side, I did make helpful comments explaining what I'm trying to do. Please take a look, and thanks in advance!
<!DOCTYPE html>
<head>
<!-- CSS Styles Link -->
<link rel='stylesheet' type='text/css' href='styles.css'>
<!-- jQuery Library Link -->
<script type='text/javascript' src='jquery-awesomeness.js'></script>
<script>
var marker = $('#marker') // Converts CSS #marker into a jQuery variable named $marker
var height = $(window).scrollTop(); // Uses jQuery scrollTop method to detect scroll position and sets it as a variable named height
// Jquery for Timeline
$(document).ready(function(){ //Prevents jQuery from performing instructions until after the page has loaded
if(height > height + 100 ) { // Creates a conditional statement - if the scroll position increases by 100, do the following instructions
marker.css('width', +10% // Increase CSS width setting on #marker by 10%.
}); // Closes first conditional statement
else if (height < height + 100 ) { // Creates another conditional statement - if the scroll position increases by 100, do the following instructions
marker.css('width', -10% // Decrease CSS width setting on #marker by 10%
}); // Closes second conditional statement
}); // Closes document dot ready function
</script>
</head>
<body>
<!-- HTML Code for Timeline elements -->
<div id='timeline'> <!-- Timeline's main container -->
<div id='path'></div><!-- Timeline's inner path -->
<div id='marker'></div><!-- Timeline's animatable marker -->
</div>
<div id='content1'>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum dolor turpis, consectetur vel libero a, mollis dapibus odio. Nunc lobortis, eros non tincidunt porttitor, purus tellus porta lectus, non luctus arcu augue eu est. Nunc malesuada ex elit, vel bibendum erat dictum nec. Proin imperdiet efficitur elit eu vestibulum. Nullam id augue tincidunt, efficitur nunc non, efficitur urna. Proin sit amet tincidunt purus. Sed interdum, felis quis facilisis malesuada, felis felis viverra lectus, eget euismod elit risus et mi. Mauris eu enim tellus. Aenean tincidunt orci nec tincidunt facilisis. Phasellus dignissim ante ut maximus feugiat. Mauris sollicitudin venenatis neque nec luctus. Quisque vel tempus urna, sed feugiat leo. Sed tellus magna, molestie non purus in, viverra condimentum orci. Nulla a nunc mauris. Aliquam convallis interdum velit, ac luctus massa rutrum at.
Sed volutpat purus enim, eget accumsan odio ullamcorper sit amet. Sed vitae imperdiet metus. Aenean mollis mauris odio, eget finibus ligula ornare eu. Aliquam id velit quis tellus cursus imperdiet. Mauris eget laoreet ipsum, sed ultricies magna. Aliquam vel neque ut risus dapibus suscipit nec nec ipsum. Nullam vulputate, elit sit amet finibus sagittis, nunc nisl molestie sem, et tincidunt neque magna in nunc. Nullam aliquam mi et ante dapibus posuere. Vivamus aliquet ipsum sit amet tellus lacinia, vitae convallis odio aliquam. Mauris rhoncus, leo sit amet vulputate placerat, sem tortor fermentum ex, quis luctus nulla sapien consequat nunc.
Phasellus a pellentesque urna. Integer eget mattis nulla. Nullam sed diam ipsum. Fusce interdum scelerisque arcu, quis ultricies risus accumsan vel. Quisque luctus nisi a nisl porttitor, at finibus elit porttitor. Fusce eget mauris a magna sollicitudin volutpat. Sed nisl magna, volutpat quis mauris a, sagittis mollis nibh. Ut consectetur justo et sem convallis, at elementum purus hendrerit. Curabitur eros odio, ullamcorper vitae facilisis vel, congue sed mauris. Aliquam egestas augue nec scelerisque ornare. Integer et neque metus. Suspendisse aliquet scelerisque odio et ornare. Donec ac mi sed nunc interdum aliquet nec at lectus. Praesent eu tortor pretium, euismod tellus ut, scelerisque massa. Nullam eleifend malesuada magna rutrum gravida.
Phasellus ultricies elit ac turpis consequat, a suscipit tellus cursus. Aenean in consectetur turpis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non dui non ipsum pretium sagittis. Donec dictum, enim sit amet venenatis venenatis, ante purus malesuada neque, ac pellentesque eros turpis eu velit. Vestibulum ligula leo, aliquet et dignissim et, dapibus nec orci. Nullam ultrices sapien id imperdiet aliquam. Vestibulum id lorem finibus, condimentum magna mollis, interdum magna. Sed vel lorem feugiat, aliquam mauris non, consequat ante. Nunc eu sem et dui porta auctor. Nam tincidunt orci sed egestas tempor. Suspendisse euismod, arcu vitae porta sollicitudin, justo erat porta sem, vel suscipit odio lorem sit amet mi. Maecenas quis finibus elit.
Praesent eu ante eu est rutrum suscipit. Maecenas dictum massa eros, blandit fermentum dui ultrices et. Duis in orci eget nulla tincidunt rhoncus at et purus. Nunc porta ipsum vitae lacinia ornare. Vivamus laoreet odio quis urna porta blandit. In viverra molestie ornare. Nam lorem turpis, lacinia at lacus non, pharetra efficitur ipsum. Donec ultrices lacinia velit id eleifend. Integer pellentesque lectus tincidunt est porta, et elementum ex euismod. Vivamus lobortis at odio at interdum. Vestibulum posuere quam dolor, at rhoncus elit cursus nec. Proin mollis tincidunt libero quis convallis. Donec vel placerat dui, at cursus urna. Donec cursus eleifend dapibus.
</p>
</div>
</body>
</html>
And the CSS:
#timeline {
width: 98%;
height: 1%;
margin: 1%;
padding-top: 1%;
padding-bottom: 1%;
position: fixed;
bottom: 0;
left: 0;
background-color: #590C13;
border-radius: .5em;
}
#path {
width: 98%;
position: fixed;
border-top: 3px solid #424343;
}
#marker {
width: 0%; /* This is defaulted to zero. jQuery resets value based on scroll position. Don't mess with it! */
position: relative;
top: 0;
border-top: 3px solid #ffffff;
}
p { /* This is place holder content used to test jQuery scroll position. Feel free to delete. */
padding: 3%;
font-size: 2em;
line-height: 3em;
}
I have restyled your CSS a little bit, and also wrote the logic that will update the marker width when you scroll or resize the viewport. The CSS mainly involve changing
#marker
and#path
to absolute positioning instead of fixed:For the computation, the trick is the following:
And now here is the logic in jQuery. I have chosen to do the calculations in a function called
updateMarker()
, so we can call it whenever the viewport size changes, or when the user scrolls. You should also call it onload (in case the user does not start from the top of the page).See fiddle here: http://jsfiddle.net/teddyrised/e0ezfw6w/
ps: On a side note, you might want to consider use the
<progress>
element, which is semantically more relevant than just<div>
;)