scroll to anchor only on data-anchor links
html
- <a name="anchor-0"></a>
- <a data-anchor="anchor-0" class="flex-item-1" href="#target-1">1</a>
- <a data-anchor="anchor-0" class="flex-item-1" href="#target-2">2</a>
jquery
- $(document).ready(function() {
- function scrollToAnchor(e){
- var aTag = $("a[name='anchor-0']");
- $('html,body').animate({scrollTop: aTag.offset().top},'400');
- e.preventDefault();
- }
- $("a").data('anchor','anchor-0').click(function() {
- scrollToAnchor();
- return false;
- });
- });
but if if I click on any link in the page where
name="anchor-0" the function
scrollToAnchor is called
it must be called only on
data-anchor="anchor-0" links
what is wrong in my function ?
thank you