Magento – How to call custom jQuery slider in magento 2

jquerymagento2

Can anyone help me, I am new to Magento 2. I want to call custom jquery slider on home page on my local site. I have tried myself but not getting how to use it with requirejs-config.js. It will be very helpful if you explain from basic step by step.

Best Answer

For example to call custom jQuery slider in home page of Magento you need to follow this steps

Step 1: Call you custom slider's CSS and JS in home page layout

Override cms_index_index.xml to your custom theme

app/code/Vendor/theme/Magento_Cms/layout/cms_index_index.xml

<?xml version="1.0" ?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <link src="Magento_Cms/js/myslider.js"/>
        <css src="Magento_Cms/css/myslider.css"/>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="index.home" template="Magento_Cms::slider.phtml"/>
        </referenceContainer>
    </body>
</page>

Step 2: Define your custom JS in requirejs-config.js

app/code/Vendor/theme/Magento_Cms/requirejs-config.js

var config = {
    map: {
        '*': {
            mySlider: 'Magento_Cms/js/myslider'
        }
    },
    shim: {
        magnificPopup: {
            deps: ['jquery']
        }
    }
};

Step 3: create phtml file which we called in layout for your slider html and js

app/code/Vendor/theme/Magento_Cms/templates/slider.phtml

  <!-- Your Custom Slider HTML -->

  <script type="text/javascript">
    requirejs(['jquery', 'myslider' ],
      function ($, myslider) {
          $(document).ready(function(){

              // Custom Slider JS

          });
      });
  </script>

I have created popup manager module with custom jQuery popup you can take reference from here:

https://github.com/mageprince/Magento2-PopupManager

Related Topic