2014-12-11 28 views
1

我将此添加到我的Gemfile:与jQuery的平滑滚动导轨遇到问题

group :assets do 
    gem 'jquery-smooth-scroll-rails', :git => '[email protected]:gretel/jquery-smooth-scroll-rails.git' 
end 

然后我跑bundle install并列入我application.js如下:

//= require jquery-smooth-scroll 

这是怎么我打电话在我custom.js文件:

$('a.scroll-to-form').smoothScroll(); 

这是referencin g由此标签创建的元素:

<%= link_to "See your options", "#bottom-form", :class=>"white-btn scroll-to-form" %> 

但是效果没有呈现。

回答

0

我刚刚尝试过,并且无法使用rails 4使用的gem,但是如果我直接从github目录中添加js文件,它会很好地工作。 https://raw.githubusercontent.com/kswedberg/jquery-smooth-scroll/master/jquery.smooth-scroll.js

你把jquery-smooth-scroll后的custom.js? JavaScript需要以正确的顺序加载,但如果它没有正确的顺序,你会看到在JavaScript控制台中的一些错误。 如果你保留// = require树。在application.js中,很有可能custom.js被放在jquery平滑滚动之前。

也许你应该使用这个片段,以确保正在执行的JavaScript一旦页面已custom.js被渲染:

(function() { 
    $(function() { 
    return $('.scroll-to-form').click("on", function(e) { 
     return $(this).smoothScroll({ 
     speed: 2000 
     }); 
    }); 
    }); 

}).call(this);