1

Hi peeps wondering if anyone could help me with a click function, should animate to the target anchor which it does fine when using an integer for the scrollTop value however when I pass a variable it doesnt like it. Any ideas?

$(".project a").click(function(event){
    event.preventDefault();
    var full_url = this.href;
    var parts = full_url.split("#");
    var trgt = parts[1];
    var target_offset = $("#"+trgt).offset();   
    var target_top = target_offset.top; //- 90; 

    $('html, body').animate({scrollTop:target_top}, 1000, function() {  
    });
});

FIXED: Ok apologies peeps the function was working fine from the get go however a colleague has pointed out var trgt contained an element that was set to display:none in the css, with the original intention of using slideToggle on callback... massive sigh. Cheers for your responses though!

1 Answers1

0

Try to parse variable to integer with

parseInt(target_top)

and try again. Let me know if not working my suggestion.

Or read this: Using a variable for a key in a JavaScript object literal. Look at Andy E's answer.

Community
  • 1
  • 1
Snake Eyes
  • 16,287
  • 34
  • 113
  • 221