Skip to content Skip to sidebar Skip to footer

Solution To Using My Tag Based Smooth Scroll Script Without 'a'

OK. So, I'm wrapping up a development. I've implemented a smooth scroll script based on the 'a' tag. I don't have time to work another script, so I'm looking for a solution that wo

Solution 1:

I know Javascript libraries are sometimes overkill, but you could do this much easier using jQuery and avoid having to deal with all the cross-browser issues yourself:

<script type="text/javascript">
$(function() {

$("a").each(function() {   // go through all links
    var href = $(this).attr("href");
    if (href.match(/^#/)) {   // if href starts with #
        $(this).click(function() {  // add a click event to it
            var name = $(this).attr("href").substring(1);  // get the anchor link 

            // on click, scroll to the a with a name attribute matching the anchor
            $('html, body').animate({ scrollTop: $("a[name='" + name + "']").offset().top }, 1000);  
        });
    }
});

});
</script>

Post a Comment for "Solution To Using My Tag Based Smooth Scroll Script Without 'a'"