Minirock Minirock - 1 year ago 95
jQuery Question

Scroll to div from li

I have a symfony view which looks like this

{% for menu in menus %}
<li>{{menu.name}}</li>


I want to be able to go on a specific div when I click to the li element.

Here is the full simplified page:

{% for menu in menus %}
<li>{{menu.name}}</li>
{% endfor %}
<p id="generalAnchor">Some text</p>
//And here i include some view depending on the li element clicked.


But in fact, when depending on the imported view size, the page is not really pretty.

So I want to scroll directly to the generalAnchor element.
I know I can do this with
a
and
href
but here I need to work with the
li
and I don't know what method I can use.

Answer Source

you can use jquery to scroll to the div.

 $("li").click(function() {
    $('html, body').animate({
        scrollTop: $("#generalAnchor").offset().top
    }, 2000);
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download