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.

On Apr 12, 11:30 am, Neil C. [email protected]
wrote:

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

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 and the return false prevents the default action.

Fred

Frederick C. wrote:

On Apr 12, 11:30�am, Neil C. [email protected]
wrote:

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

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 and the return false prevents the default action.

Fred

That works just fine, thank you. Slightly related; is there an easy way
to get the cursor to sit inside the page[‘some_id’]? I’m looking to use
the same behavior on a textarea, so it would make sense to put the
cursor inside the textarea.

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’)” %>

On Apr 12, 6:30 am, Neil C. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs