Last active
December 6, 2016 01:12
-
-
Save onigetoc/d2207fdd877ac1897ddbf6ad07fea350 to your computer and use it in GitHub Desktop.
Scroll to href atribute link like href="#targetID"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DEMO: https://jsfiddle.net/onigetoc/5kh0e5f4/ | |
function ScrollTo(target, speed, timeout, margin) { | |
if (!timeout) timeout = 0; | |
if (!speed) speed = 1000; | |
if (!margin) margin = 0; | |
if (typeof target !== "undefined") { | |
setTimeout(function() { | |
$('html,body').animate({ | |
scrollTop: $(target).offset().top - margin | |
}, speed); | |
}, timeout); | |
} | |
} | |
//////////////// END SCROLL TO FUNCTION ///////////////////// | |
// USAGE: | |
// Check if link begin with # HASH or . DOT | |
// for example it will automatically scroll to comments | |
$('a[href^="#"], a[href^="."]').click(function(e) { | |
e.preventDefault(); | |
ScrollTo($(this).attr("href"), 500,0,10); //target, speed, timeout, margin | |
}); | |
// OR USUAL JQUERY .CLICK WITH TARGET | |
/* | |
$("#link-li a").click(function(e) { | |
// Prevent a page reload when a link is pressed | |
e.preventDefault(); | |
// Call the scroll function | |
ScrollTo($(this).attr("href"), 500,0,100); //target, speed, timeout, margin | |
//ScrollTo( "#about", 500,0,100 ); //target, speed, timeout, margin | |
}); | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="container"> | |
<div id="link-li"> | |
<ul> | |
<li><a id="aboutlink" href="#about">About (ID)</a> | |
</li> | |
<li><a id="projectslink" href="#projects">Projects (ID)</a> | |
</li> | |
<li><a id="resumelink" href="#resume">Resume (ID)</a> | |
</li> | |
<li><a id="contactlink" href=".contact">Contact (Class)</a> | |
</li> | |
</ul> | |
</div> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<div class="" id="about"> | |
<p class="header">About</p> | |
<p class="info">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure | |
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> | |
</div> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<div id="content"> | |
<div class="sections" id="projects"> | |
<p class="header">Projects</p> | |
<p class="info">Infos Projects</p> | |
</div> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<div class="sections" id="resume"> | |
<p class="header">Resume</p> | |
<p class="info">Infos Resume</p> | |
</div> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<div class="sections contact" id="contactID"> | |
<p class="header">Contact</p> | |
<p class="info">Infos Contact</p> | |
</div> | |
</div> | |
</div> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> | |
<br/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment