Animated Autoscroll To Div Onclick
I have a simple menu with fixed position and some text inside it and some boxes under this menu(divs). I'm searching for a code that when I click on one of the texts that inside me
Solution 1:
Check this:
jQuery(document).ready(function($) {
$(".scroll").click(function(event){
event.preventDefault();
$('html,body').animate({scrollTop:$(this.hash).offset().top - 59}, 800);
});
});
Solution 2:
You will need to do something like this:
<style>
#menu {
background-color: #ccc;
position:fixed;
width:100%
}
.menutext {
padding:25px 40px 0px 0px;
display:inline-block;
}
.alldivs {
width:300px;
height:200px;
background-color:a9a9a9;
}
#content {
/* added for visualization */
position: absolute;
top: 100px;
border: 1px solid black;
height:200px;
overflow: hidden;
}
</style>
<div id="menu">
<div class="menutext" data-scrollid="DIV1">Auto-scroll to (DIV1) onclick on me</div>
<div class="menutext" data-scrollid="DIV2">Auto-scroll to (DIV2) onclick on me</div>
<div class="menutext" data-scrollid="DIV3">Auto-scroll to (DIV3) onclick on me</div>
<div class="menutext" data-scrollid="DIV4">Auto-scroll to (DIV4) onclick on me</div>
</div>
<div id="content">
<div class="alldivs"><div id="DIV1">DIV1</div></div>
<div class="alldivs"><div id="DIV2">DIV2</div></div>
<div class="alldivs"><div id="DIV3">DIV3</div></div>
<div class="alldivs"><div id="DIV4">DIV4</div></div>
</div>
<script>
var $content = $('#content');
$('.menutext').each(function(){
var $this=$(this);
scrollTo = 0;
$this.on('click', function(){
var $target = $('#'+$this.data('scrollid'));
scrollTo = scrollTo + $target.position().top + $target.scrollTop();
$content.animate({scrollTop: scrollTo}, 1000);
})
})
</script>
Solution 3:
Check this out:
$(function(){
var offsets = [],
menuText = $('#menu .menuText');
$("div.contentDiv").each( function(i, div) {
offsets.push({ id: div.id, offset: $(div).offset().top - 60});
});
$(window).scroll(function(e) {
var start = $(this).scrollTop();
for ( var div = 0; div < offsets.length; div++ ) {
if ( start > offsets[div].offset ) {
menuText.removeClass('menutext2').addClass('menutext');
menuText.filter('[linkId="'+offsets[div].id+'"]').addClass('menutext2').removeClass('menutext');
}
}
if ( start === 0 ) {
menuText.removeClass('menutext2').addClass('menutext');
}
});
});
Post a Comment for "Animated Autoscroll To Div Onclick"