jump.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. function JumpObj(elem, range, startFunc, endFunc) {
  2. var curMax = range = range || 6;
  3. startFunc = startFunc || function(){};
  4. endFunc = endFunc || function(){};
  5. var drct = 0;
  6. var step = 1;
  7. init();
  8. function init() { elem.style.position = 'relative';active() }
  9. function active() { elem.onmouseover = function(e) {if(!drct)jump()} }
  10. function deactive() { elem.onmouseover = null }
  11. function jump() {
  12. var t = parseInt(elem.style.top);
  13. if (!drct) motionStart();
  14. else {
  15. var nextTop = t - step * drct;
  16. if (nextTop >= -curMax && nextTop <= 0) elem.style.top = nextTop + 'px';
  17. else if(nextTop < -curMax) drct = -1;
  18. else {
  19. var nextMax = curMax / 2;
  20. if (nextMax < 1) {motionOver();return;}
  21. curMax = nextMax;
  22. drct = 1;
  23. }
  24. }
  25. setTimeout(function(){jump()}, 200 / (curMax+3) + drct * 3);
  26. }
  27. function motionStart() {
  28. startFunc.apply(this);
  29. elem.style.top='0';
  30. drct = 1;
  31. }
  32. function motionOver() {
  33. endFunc.apply(this);
  34. curMax = range;
  35. drct = 0;
  36. elem.style.top = '0';
  37. }
  38. this.jump = jump;
  39. this.active = active;
  40. this.deactive = deactive;
  41. }