I'm new to jQuery; bear with me. Have been trying to use this simple (but great) solution to next-previous scrolling by pdoherty926:
jQuery Scroll to Next Div Class with Next / Previous Button
However, after copying the code from his jsfiddle link the scrolling won't work on my page. What am I missing?
I've put it all into an HTML5 page to play with it (will obviously move styles and js to separate linked CSS and JS files):
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>scroll test</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('div.section').first();
$('a.display').on('click', function(e) {
e.preventDefault();
var t = $(this).text(),
that = $(this);
if (t === 'next' && $('.current').next('div.section').length > 0) {
var $next = $('.current').next('.section');
var top = $next.offset().top;
$('.current').removeClass('current');
$('body').animate({
scrollTop: top
}, function () {
$next.addClass('current');
});
} else if (t === 'prev' && $('.current').prev('div.section').length > 0) {
var $prev = $('.current').prev('.section');
var top = $prev.offset().top;
$('.current').removeClass('current');
$('body').animate({
scrollTop: top
}, function () {
$prev.addClass('current');
});
}
});
});
</script>
<style type="text/css">
.section {background-color:gray; height:440px; margin-bottom:20px;}
.navigation {position:fixed; right:50px; top:10px;}
</style>
</head>
<body>
<div class="main">
<div class="navigation">
<a href="#" class="display">next</a><br>
<a href="#" class="display">prev</a>
</div>
<div id="one" class="section current">
One
</div>
<div id="two" class="section">
Two
</div>
<div id="three" class="section">
Three
</div>
<div id="four" class="section">
Four
</div>
</div>
Any and all help/advice very welcome.