scroll.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.fx.scroll"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.fx.scroll"] = true;
  8. dojo.provide("dojox.fx.scroll");
  9. dojo.experimental("dojox.fx.scroll");
  10. dojo.require("dojox.fx._core");
  11. dojox.fx.smoothScroll = function(/* Object */args){
  12. // summary: Returns an animation that will smooth-scroll to a node
  13. // description: This implementation support either horizontal or vertical scroll, as well as
  14. // both. In addition, element in iframe can be scrolled to correctly.
  15. // offset: {x: int, y: int} this will be added to the target position
  16. // duration: Duration of the animation in milliseconds.
  17. // win: a node or window object to scroll
  18. if(!args.target){ args.target = dojo.position(args.node); }
  19. var isWindow = dojo[(dojo.isIE ? "isObject" : "isFunction")](args["win"].scrollTo),
  20. delta = { x: args.target.x, y: args.target.y }
  21. ;
  22. if(!isWindow){
  23. var winPos = dojo.position(args.win);
  24. delta.x -= winPos.x;
  25. delta.y -= winPos.y;
  26. }
  27. var _anim = (isWindow) ?
  28. (function(val){
  29. args.win.scrollTo(val[0],val[1]);
  30. }) :
  31. (function(val){
  32. args.win.scrollLeft = val[0];
  33. args.win.scrollTop = val[1];
  34. });
  35. var anim = new dojo.Animation(dojo.mixin({
  36. beforeBegin: function(){
  37. if(this.curve){ delete this.curve; }
  38. var current = isWindow ? dojo._docScroll() : {x: args.win.scrollLeft, y: args.win.scrollTop};
  39. anim.curve = new dojox.fx._Line([current.x,current.y],[current.x + delta.x, current.y + delta.y]);
  40. },
  41. onAnimate: _anim
  42. },args));
  43. return anim; // dojo.Animation
  44. };
  45. }