RJS scroll to DOM object

Is there an rjs helper for zooming or scrolling to a DOM object, e.g. page.view_object "object"?

I've tried using named anchors in combination with link_to_function, but unless I'm doing something wrong, the onclick seems to override the default browser behavior of skipping to a DOM element. For example;

This works;

<%= link_to "Nav", "#header" %>

But this doesn't;

  <%= link_to_function("Nav", :href => "#header") do |page|       some javascript    end %>

It doesn't seem to matter which rjs helpers I use, or even if I don't use any at all, the onclick seems to kill the skip to the named anchor. So I was hoping I could just call an additional rjs helper to avoid the problem.

page['some_id'].scrollTo() should do the trick. The reason why the default behaviour is overriden is that rails generates you something that looks like <a href="#header" onclick="someJavascript(); return false;"> and the return false prevents the default action.


Frederick Cheung wrote:

The reason the link_to_function doesn't scroll to your named element is that it appends a "return false;" after your javascript, which stops the default action.

You can use prototype javascript call Element.scrollTo('header').

<%= link_to_function "Nav", "Element.scrollTo('header')" %>