1

I have a question that was answered before probably, but I was not able to make a code work.

I have an <a href="#page-contact">, and once the user click on it, it should scroll down (with animation) to a div with id "page-contact". It does take me to the form but with no animation

Here is the code:

<li class=""><a href="#page-contact">Contact</a></li>


<section id="page-contact" class="page-contact">
</section>

 $('.page-contact').on('click', function(event) {
    var target = $(this.href);
    if( target.length ) {
       event.preventDefault();
       $('html, body').animate({
         scrollTop: $("#page-contact").offset().top
    }, "slow");
  }
});
anyavacy
  • 1,618
  • 5
  • 21
  • 43

4 Answers4

1

One reason is href property returns the absolute path of the anchor, i.e. "http://www.example.com#hash". As jQuery can't find the target element the length of the collection is 0 and the if block is not executed.

Either use this.getAttribute('href') which returns the original href attribute or the hash property that returns hash segment of the href attribute instead of the href property.

var target = $(this.hash); // $('#page-contact')

Also note that your click handler is bound to the target section element not the a element.

$('li a').on('click', function(event) {
    var target = $(this.hash);
    if( target.length ) {
       event.preventDefault();
       $('html, body').animate({
         scrollTop: target.offset().top
        }, "slow");
     }
});
Ram
  • 143,282
  • 16
  • 168
  • 197
1

$(this.href) not valid selector so target variable always empty, change var target = $(this.href); to var target = this.href; to get link

$(this.href) seletor work as.

$("http://stackoverflow.com/questions/29515247/scrolling-to-a-div-using-jquery#29515468")

> Object[]

 $('.page-contact').on('click', function(event) {
    var target = this.href;
    if( target.length ) {
       event.preventDefault();
       $('html, body').animate({
         scrollTop: $("#page-contact").offset().top
    }, "slow");
  }
});
Girish
  • 11,907
  • 3
  • 34
  • 51
1

Actually something really small..

$('.page-contact').on('click', function(event) {
     //..
};

You connect this to a click on the target div, not the button. Connect to event to a class that your button has and it will work.

$('#scrollButton').on('click', function(event) {
    var target = $(this.hash);
    if( target.length ) {
        event.preventDefault();    
        $('html, body').animate({
            scrollTop: target.offset().top
        }, "slow");
    }
});

Fiddle to try out: http://jsfiddle.net/Macavity/xeqmnoLL/2/

Pape
  • 291
  • 1
  • 9
1

Your selector is wrong, change to this:

$('a[href="#page-contact"]').on('click', function(event) {
    var target = this.getAttribute("href");
    if($(target).length) {
       event.preventDefault();
       $('html, body').animate({
           scrollTop: $("#page-contact").offset().top
       }, "slow");
    }
});
Jai
  • 74,255
  • 12
  • 74
  • 103