ded / morpheus

A Brilliant Animator
504 stars 57 forks source link

scrollTop support #22

Closed iamdustan closed 12 years ago

iamdustan commented 12 years ago

It would be really fly to get some scrollTop support in here. With the current implementation needing a css style set first, how much of a break would it be to add scrollTop into the mix?

ThePixelDeveloper commented 12 years ago

I'd like to see this too.

iamdustan commented 12 years ago

hey @ThePixelDeveloper. Here’s a quick general scrollTo method I dropped using bonzo, bean and morpheus, though it’d be trivial to remove everything but morpheus.

body.delegate('a[data-trigger=scroll]', 'click', function (e) {

  var morpheus = require('morpheus')
    , endEl = this.getAttribute('href') && $(this.getAttribute('href'))[0] || document.documentElement
    , start = isNaN(window.pageYOffset) ? document.documentElement.scrollTop : window.pageYOffset
    , end = getPos(endEl)
    , length = getPos(endEl).y - start

  function getPos( elm ){
    var x = 0, y = 0;
    while( elm != null ) {
      x += elm.offsetLeft;
      y += elm.offsetTop;
      elm = elm.offsetParent ;
    }
    return {x:x, y:y};
  }

  morpheus.tween(500,
    function (pos) { window.scrollTo(0, start+length*pos) },
    function () { console && console.log('done') },
    null, start, end
  )
});