dolls.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. var tethers = [];
  2. document.addEventListener('DOMContentLoaded', function(){
  3. dragging = null;
  4. document.body.addEventListener('mouseup', function(){
  5. dragging = null;
  6. });
  7. document.body.addEventListener('mousemove', function(e){
  8. if (dragging){
  9. dragging.style.top = e.clientY + 'px';
  10. dragging.style.left = e.clientX + 'px';
  11. Tether.position()
  12. }
  13. });
  14. document.body.addEventListener('mousedown', function(e){
  15. if (e.target.getAttribute('data-index'))
  16. dragging = e.target;
  17. })
  18. var count = 60;
  19. var parent = null;
  20. var dir = 'left';
  21. var first = null;
  22. while (count--){
  23. var el = document.createElement('div');
  24. el.setAttribute('data-index', count);
  25. document.querySelector('.scroll').appendChild(el);
  26. if (!first)
  27. first = el;
  28. if (count % 10 === 0)
  29. dir = dir == 'right' ? 'left' : 'right';
  30. if (parent){
  31. tethers.push(new Tether({
  32. element: el,
  33. target: parent,
  34. attachment: 'middle ' + dir,
  35. targetOffset: (dir == 'left' ? '10px 10px' : '10px -10px')
  36. }));
  37. }
  38. parent = el;
  39. }
  40. initAnim(first);
  41. });
  42. function initAnim(el){
  43. var start = performance.now()
  44. var last = 0;
  45. var lastTop = 0;
  46. var tick = function(){
  47. var diff = performance.now() - last;
  48. if (!last || diff > 50){
  49. last = performance.now();
  50. var nextTop = 50 * Math.sin((last - start) / 1000);
  51. var curTop = parseFloat(el.style.top || 0);
  52. var topChange = nextTop - lastTop;
  53. lastTop = nextTop;
  54. var top = curTop + topChange;
  55. el.style.top = top + 'px';
  56. Tether.position();
  57. }
  58. requestAnimationFrame(tick);
  59. };
  60. tick();
  61. }