How to change this javascript to coffeescript?
$(document).ready(function(){
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash;
var $target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 900, 'swing', function () {
window.location.hash = target;
});
});
});
I tried this code in my rails project,but it doesn't work
$(document).on 'page:change', ->
$('a[href^="#"]').click (event) ->
event.preventDefault()
target = this.hash
$target = $(target)
$('html, body').stop().animate{
'scrollTop': $target.offset().top
}, 900,
'swing', ->
window.location.hash = target
Do I have something wrong?
returnstatements at the end of each block. But yes, online JS to CoffeeScript converters are your friend.$(document).on('page:change', function() {}); // ..instead$(document).on('page:change', function() { //... Second mistake is hereanimate{- lost whitespace again. Remember that сoffee uses significant whitespace to delimit blocks of code.