5

i found a post here showing me how to use jquery with gatsby and that works fine. Now my main problem is using sticky kit, a jquery plugin and i am getting the error "$(...).stick_in_parent is not a function", i have checked all possible documentations and web forums but i cant seem to get this to work. heres my code for using jquery and sticky-kit in gatsby:

const React = require("react")

export const onRenderBody = ({ setHeadComponents }, pluginOptions) => {
    setHeadComponents([
        <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>,
        <script src="js/animsition/jquery.animsition.min.js"></script>,
        <script src="https://cdnjs.cloudflare.com/ajax/libs/sticky-kit/1.1.3/sticky-kit.min.js" integrity="sha512-MAhdSIQcK5z9i33WN0KzveJUhM2852CJ1lJp4o60cXhQT20Y3friVRdeZ5TEWz4Pi+nvaQqnIqWJJw4HVTKg1Q==" crossorigin="anonymous"></script>
    ])
}

heres the code implementing the two:

 function activeStickyKit() {
            $('.sticky-column').stick_in_parent({
                parent: '.sticky-parent'
            });

            // bootstrap col position
            $('.sticky-column')
                .on('sticky_kit:bottom', function(e) {
                    $(this).parent().css('position', 'static');
                })
                .on('sticky_kit:unbottom', function(e) {
                    $(this).parent().css('position', 'relative');
                });
        };
        activeStickyKit();

        function detachStickyKit() {
            $('.sticky-column').trigger("sticky_kit:detach");
        };

        //  stop sticky kit
        //  based on your window width

        var screen = 1200;

        var windowHeight, windowWidth;
        windowWidth = $(window).width();
        if ((windowWidth < screen)) {
            detachStickyKit();
        } else {
            activeStickyKit();
        }

        // windowSize
        // window resize
        function windowSize() {
            windowHeight = window.innerHeight ? window.innerHeight : $(window).height();
            windowWidth = window.innerWidth ? window.innerWidth : $(window).width();
        }
        windowSize();

        function debounce(func, wait, immediate) {
            var timeout;
            return function() {
                var context = this, args = arguments;
                var later = function() {
                    timeout = null;
                    if (!immediate) func.apply(context, args);
                };
                var callNow = immediate && !timeout;
                clearTimeout(timeout);
                timeout = setTimeout(later, wait);
                if (callNow) func.apply(context, args);
            };
        };

        $(window).resize(debounce(function(){
            windowSize();
            $(document.body).trigger("sticky_kit:recalc");
            if (windowWidth < screen) {
                detachStickyKit();
            } else {
                activeStickyKit();
            }
        }, 250));

the code above is placed in the gatsby-browser.js file

Please any insight will be of assistance!. Thanks

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
  • Try wrapping all your jQuery functions/logic in gatsby-browser.js with: `exports.onInitialClientRender = () => { ...your logic here... }`. See: https://www.gatsbyjs.com/docs/reference/config-files/gatsby-browser/#onInitialClientRender – epsilon42 Mar 23 '21 at 15:58
  • ive tried that, it still doesnt work –  Mar 24 '21 at 09:16
  • you may try checking this https://github.com/leafo/sticky-kit/issues/188#issuecomment-318818401 – vijayP Mar 24 '21 at 12:00
  • 1
    @DBankx Just to clarify: What file have you included the first code block in your question (i.e. `export const onRenderBody` )? When you say you've added jQuery to Gatsby and it works fine, what do you mean by that? Do you see your included scripts when you view page source and/or inspect the elements on the page? Are you able to run another jQuery method fine? Are there any other errors in the console? – epsilon42 Mar 25 '21 at 06:41

0 Answers0