1

I've been trying to add 2 new javascript libraries into my Magento 2 installation, and to do that, I've added them through "app/design/frontend/theme/themeName/layout/default_head_blocks.xml" and I have also created a "requirejs-config.js" into my theme folder, and I have loaded my libraries into my "web/js" folder. Now one of the librarie is "jquery-3.3.1.min.js" and the other is "jquery.bxslider.min", I've tried to load them from cdn, but wasn't working, so I loaded them on my hosting. Anyway now I'm calling the function for running the sliders, but is now working any of the sliders, and is not working even the menu on mobile, basically is not opening the hamburger menu anymore.

15
  • Check console, maybe js conflict with menu JS.
    – kunj
    Commented May 16, 2018 at 12:48
  • Can you shre website URL?
    – kunj
    Commented May 16, 2018 at 12:48
  • I get this "Uncaught TypeError: $(...).bxSlider is not a function"
    – alexcr
    Commented May 16, 2018 at 13:05
  • I have also added the code "var $ = jQuery.noConflict();" inline on the homepage content
    – alexcr
    Commented May 16, 2018 at 13:06
  • Have tried without your jquery-3.3.1.min.js file? Magento 2 loading Jquery by default.
    – kunj
    Commented May 16, 2018 at 13:06

1 Answer 1

2

Try this:

Add this code in requirejs-config.js:

var config = {
    paths: {            
            'bxslider': "'js/jquery.bxslider.min"
        },
    shim: {
        'bxslider': {
            deps: ['jquery']
        }
    }
};

And add this code to your PHTML file:

require(['jquery', 'bxslider'], function($){ 
    $(function() {      
        $('.slider').bxSlider({ auto: true, autoDelay: 2000, controls: false, pager: false });
    });
}); 

Remove your jquery-3.3.1.min.js.

2
  • what do you mean with PHTML file, my homepage content?
    – alexcr
    Commented May 16, 2018 at 14:06
  • I recommend adding JS in JS files and not in templates.
    – Ben Crook
    Commented May 16, 2018 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.