开发者

How to do a smooth-scroll?

I have the following working code to make a element of my scrollbox visible:

var next = elements.item(i+1);
var xpcomInterface = scroll.boxObject.QueryInterface(
Components.interfaces.nsIScrollBoxObject);
xpcomInterface.en开发者_运维知识库sureElementIsVisible(elements);

But I would like to make a smooth-scroll (slow or not). Any idea how to do it?

update

By the way, it's for Mozilla environment.


The simplest way would be to just use a setTimeout function and just keep moving the top and left value on the element's div by a small number, until you get where you want to be.

You may want to experiment with how fast to make it move, as there is a trade-off with smooth, and the fact that it should reach the endpoint in some reasonable time.

Update:

I forgot, you will want to keep calling the setTimeout until you reach the final destination, otherwise it won't redraw the browser window.


I don't know exactly how to do it, but here's a Chrome Extension called "Smooth Scroll" where you can potentially pick through the code to see what they do and maybe get you in the right direction.

PS I love this extension.


jQuery .animate() with accelerated motion can be kind of smooth (see demo for animate with a relative move). And with a plug-in, there are other easing equations it can use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜