fabiospampinato / cash

An absurdly small jQuery alternative for modern browsers.
MIT License
6.45k stars 268 forks source link

Support for scroll methods #373

Open juandavm opened 3 years ago

juandavm commented 3 years ago

Please, support the jQuery scrollTop method https://api.jquery.com/scrolltop/

It's a widely used method of jQuery

fabiospampinato commented 3 years ago

Happy to add these, if somebody can manage to squeeze enough bytes out of the bundle, I think we are currently nearly at 6kb min+giz, I don't want to exceed that.

vovayatsyuk commented 3 years ago

@juandavm, meanwhile, I recommend creating cash-extends.js file with all new methods you need. Here is a scrollTop implementation:

$.fn.scrollTop = function (val) {
    var el = this.get(0);

    if (val === undefined) {
        return el ? el.scrollTop : null;
    }

    if (el) {
        el.scrollTop = val;
    }

    return this;
};
fabiospampinato commented 3 years ago

@vovayatsyuk Actually that might be buggy because this.get (0) can return undefined.

vovayatsyuk commented 3 years ago

Ah, yes.. Fixed now. I hope :)

Thank you.

p.s. Fixed one more time.🤫

cyfung1031 commented 2 years ago

Cash has removed the shortcuts like ".scroll" ".click" (basically it is $(...).on("scroll", ...) ) Similarly, you shall use $(...).prop("scrollTop", ... ) instead.

$.fn.scrollTop = function (val) {

    if (val === undefined) {
        return this.prop('scrollTop');
    }

    return this.prop('scrollTop', val);

};
AliN11 commented 2 years ago

@juandavm, meanwhile, I recommend creating cash-extends.js file with all new methods you need. Here is a scrollTop implementation:

$.fn.scrollTop = function (val) {
    var el = this.get(0);

    if (val === undefined) {
        return el ? el.scrollTop : null;
    }

    if (el) {
        el.scrollTop = val;
    }

    return this;
};

Replaced el.scrollTop with el.pageYOffset and everything works fine:

$.fn.scrollTop = function (val) {
    var el = this.get(0);

    if (val === undefined) {
        return el ? el.pageYOffset : null;
    }

    if (el) {
        el.pageYOffset = val;
    }

    return this;
}
omidgfx commented 1 year ago

@juandavm, meanwhile, I recommend creating cash-extends.js file with all new methods you need. Here is a scrollTop implementation:

$.fn.scrollTop = function (val) {
    var el = this.get(0);

    if (val === undefined) {
        return el ? el.scrollTop : null;
    }

    if (el) {
        el.scrollTop = val;
    }

    return this;
};

Replaced el.scrollTop with el.pageYOffset and everything works fine:

$.fn.scrollTop = function (val) {
    var el = this.get(0);

    if (val === undefined) {
        return el ? el.pageYOffset : null;
    }

    if (el) {
        el.pageYOffset = val;
    }

    return this;
}

But it's not working!

cyfung1031 commented 9 months ago

Happy to add these, if somebody can manage to squeeze enough bytes out of the bundle, I think we are currently nearly at 6kb min+giz, I don't want to exceed that.

Can we have partial build option so that there will be "polyfill" for .click, .scroll, etc which are excluded by default, but user can do the partial build to add @require events/click.ts (the ts file is provided in this repo) This can help users to do migration easier and they might not care about the file size increment.