After some puzzling I believe that the function to calculate the new scroll position needs to add scroll[axis] to it. So that line becomes: to[axis] = scroll[axis] + position[axis] - (containerSize[axis] - size[axis]) / 2;
This seems to work perfectly for me, at least for the vertical scrolling that I tested. See http://jsfiddle.net/woomla/D2nJJ/8/ The first two time it uses the mootools function. The second time the element is scrolled out of view. The third and forth time it uses the altered function. The third time it centers the element. The forth time the element stays centered. On Mar 6, 11:05 pm, woomla <[email protected]> wrote: > Thanks. > > And what happens if we center the element twice? > > http://jsfiddle.net/D2nJJ/6/ > > On 4 mrt, 16:18, Ryan Florence <[email protected]> wrote: > > > I didn't bother interpreting your example but made this example instead. > > >http://jsfiddle.net/rpflorence/D2nJJ/ > > > This appears to work as expected. It scrolls one element to the center of > > another element. > > > On Mar 4, 2011, at 1:57 AM, woomla wrote: > > > > Hi, > > > > I've created a large list of div's in a container div. I want the > > > container to scroll programmatically to some element. So I suppose to > > > use toElementCenter. But it appears that the calculation is wrong, or > > > my interpretation of the function is wrong. I hope someone can clarify > > > this for me. > > > > See this fiddle:http://jsfiddle.net/CB7Xj/17/ > > > > The first few functions create the list and put the items in a results > > > array. At the bottom of the script we start to scroll to all elements > > > one by one with function mooScrollTo. In the output you see some > > > scrolling, but at some point it is going up and down again.
