matt
matt

Reputation: 44333

Extend Zepto.js with a jQuery method? scrollTop()

I'm using Zepto.js on a current project. Zepto doesn't support the scrollTop() method that jQuery has in it.

Is it possible to kind of extend Zepto to work with scrollTop() too?

Update: All I want is to create my own small and simple "animated scroll" function like I have used before with jQuery. See the working example here. However I have no idea how to make the same function work without the scrollTop() function available in Zepto.js.

Upvotes: 4

Views: 8357

Answers (6)

MountainAsh
MountainAsh

Reputation: 534

Note that version 1.0 of Zeptos now supports scrollTop(). See Documentation: http://zeptojs.com/#scrollTop

Upvotes: 0

survivol
survivol

Reputation: 11

(function ($) {

    ['width', 'height'].forEach(function(dimension) {
        var offset, Dimension = dimension.replace(/./, function(m) { return m[0].toUpperCase() });
        $.fn['outer' + Dimension] = function(margin) {
            var elem = this;
            if (elem) {
                var size = elem[dimension]();
                var sides = {'width': ['left', 'right'], 'height': ['top', 'bottom']};
                sides[dimension].forEach(function(side) {
                    if (margin) size += parseInt(elem.css('margin-' + side), 10);
                });
                return size;
            }
            else {
                return null;
            }
        };
    });

    ["Left", "Top"].forEach(function(name, i) {
        var method = "scroll" + name;

        function isWindow( obj ) {
            return obj && typeof obj === "object" && "setInterval" in obj;
        }

        function getWindow( elem ) {
            return isWindow( elem ) ? elem : elem.nodeType === 9 ? elem.defaultView || elem.parentWindow : false;
        }

        $.fn[method] = function( val ) {
            var elem, win;
            if (val === undefined) {
                elem = this[0];
                if (!elem) {
                    return null;
                }
                win = getWindow(elem);
                // Return the scroll offset
                return win ? ("pageXOffset" in win) ? win[i ? "pageYOffset" : "pageXOffset"] :
                    win.document.documentElement[method] ||
                    win.document.body[method] :
                    elem[method];
            }

            // Set the scroll offset
            this.each(function() {
                win = getWindow(this);
                if (win) {
                    var xCoord = !i ? val : $(win).scrollLeft();
                    var yCoord = i ? val : $(win).scrollTop();
                    win.scrollTo(xCoord, yCoord);
                }
                else {
                    this[method] = val;
                }
            });
        }
    });

})(Zepto);

Upvotes: 1

callumacrae
callumacrae

Reputation: 8433

scrollTop isn't animatable using Zepto's .animate method, as it uses CSS transitions.

Try something like this: http://jsfiddle.net/DVDLM/5/

function scroll(scrollTo, time) {
    var scrollFrom = parseInt(document.body.scrollTop),
        i = 0,
        runEvery = 5; // run every 5ms

    scrollTo = parseInt(scrollTo);
    time /= runEvery;

    var interval = setInterval(function () {
        i++;

        document.body.scrollTop = (scrollTo - scrollFrom) / time * i + scrollFrom;

        if (i >= time) {
            clearInterval(interval);
        }
    }, runEvery);
}

$('#trigger').click(function () {
    scroll('600px', 500);
});

EDIT: I added a runEvery variable, which specifies how often the interval should be ran. The lower this is, the smoother the animation is, but it could affect performance.

EDIT2: I think I misread the question. Here is the answer to the new question:

$.zepto.scrollTop = function (pixels) {
    this[0].scrollTop = pixels;
};

Upvotes: 6

cuzzea
cuzzea

Reputation: 1535

The answer is simple, Zepto dose not use timeout style animation, it uses css3, so here is a basic implementation for a scroll function:

HTML: Animated Scroll Hello You ​

CSS: #page { height:5000px; position:relative; } #element { position:absolute; top:600px }

JS:

function scroll(selector, animate, viewOffset) {
    $('body').scrollToBottom (600, '800');
}

$('#trigger').click(function(e) {
   e.preventDefault();
   scroll( $('#element'), true, 30 );
});
$.fn.scrollToBottom = function(scrollHeight ,duration) {
    var $el = this;
    var el  = $el[0];
    var startPosition = el.scrollTop;
    var delta = scrollHeight  - startPosition;

    var startTime = Date.now();

    function scroll() {
        var fraction = Math.min(1, (Date.now() - startTime) / duration);

        el.scrollTop = delta * fraction + startPosition;

        if(fraction < 1) {
            setTimeout(scroll, 10);
        }
    }
    scroll();
};
​

Upvotes: 0

rainykeys
rainykeys

Reputation: 594

dont want to steel nobody work so here is the short answer Porting from jQuery to Zepto

Upvotes: 5

Quadroid
Quadroid

Reputation: 825

Use the DOM native scrollTop property:

$('#el')[0].scrollTop = 0;

Upvotes: 1

Related Questions