This javascript code is working fine.
function goToByScroll(id){
$('html,body').animate({scrollTop: $("#"+id).offset().top - 50},'slow');
}
I’m having a hard time with its CoffeScript version. I had the following code on application.js.coffee.
goToByScroll = (id) ->
$("html,body").animate ->
scrollTop: $("#" + id).offset().top - 50
, "slow"
But I get the error
ReferenceError: Can't find variable: goToByScroll
Any idea what may be causing the error?
What Jordan said is correct: you’ll need to somehow export your function
to a globally-accessible scope or compile your CoffeeScript with the
bareflag, preventing the output from being wrapped in an anonymous function.
Additionally, you’ve got a bit of a bug in your CoffeeScript: you’re passing
a callback to
jQuery.animate, not an object literal like you do in theJavaScript code. For equivalent behavior, you probably want something like
this: