javascript - How to animate transitions triggered by links -
i found this useful jquery navigation highlights linked content scrolls. think understand how works, i'm having trouble including transitions / animations clicked items. want sections smoothly transition when triggered navigation.
i have tried adding css
.section { transition: transform .5s ease-in-out; }
and jquery
$('#navigation').click(function(){ $('.section').animate('slow'); });
i'd appreciate explanation of doing wrong in particular example.
here code , https://jsfiddle.net/r040p7oa/
<div id="navigation"> <ul> <li><a href="#section1">section 1</a></li> <li><a href="#section2">section 2</a></li> </ul> </div> <div id="sections"> <div id ="section1" class="section">i'm section 1</div> <div id ="section2" class="section">i'm section 2 </div>
#sections { position: absolute; top: 0; left: 120px; } #navigation { position: fixed; top: 0; left: 0; } .active { background: red; } .section { padding-bottom: 400px; }
-
$(window).scroll(function() { var position = $(this).scrolltop(); $('.section').each(function() { var target = $(this).offset().top; var id = $(this).attr('id'); if (position >= target) { $('#navigation > ul > li > a').removeclass('active'); $('#navigation > ul > li > a[href=#' + id + ']').addclass('active'); } }); });
$('a').click(function(){ $('html, body').animate({ scrolltop: $( $.attr(this, 'href') ).offset().top }, 500); return false; });
Comments
Post a Comment