Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - move to a specific element location

I have a pagination system like this:

[content]
1 2 3 Next >

when you click on 1/2/3/next the [content] element gets replaced trough ajax.

the problem is that the screen focus moves up if the new height of the [content] is larger than the previous one.

can I force the screen to stay focused where where the pagination links are?

like image 984
Alex Avatar asked Jul 06 '10 13:07

Alex


People also ask

How do I move a DIV from one place to another in jQuery?

All you have to do is select the element(s) you want to move, then call an “adding” method such as append() , appendTo() or prepend() to add the selected elements to another parent element. jQuery automatically realises that the element(s) to add already exist in the page, and it moves the element(s) to the new parent.

How do I set relative position in jQuery?

jQuery position() MethodThe position() method returns the position (relative to its parent element) of the first matched element. This method returns an object with 2 properties; the top and left positions in pixels.

How do you move an element into another element?

Answer: Use the jQuery . appendTo() Method You can use the jQuery . appendTo() method to move an element into another element.

How do I scroll down to a div?

The scrollIntoView method: The scrollIntoView() is used to scroll to the specified element in the browser. Example: Using scrollIntoView() to scroll to an element.


1 Answers

You need to find the position of the element and scroll to that position each time the height changes. Something like:

var p = $(".links").position();
$(window).scrollTop(p.top);
like image 165
Luca Matteis Avatar answered Sep 22 '22 11:09

Luca Matteis