cancel
Showing results for 
Search instead for 
Did you mean: 

owl carousel not working in magento2.4

owl carousel not working in magento2.4

it's my first time using magento and i have luma theme "without sample data" and i want to put slider in it, when i do this the images showing under each other like a block not a slider, i searched a lot for this issue and found a lot of solutions but nothing worked for me so i want to know if i'm doing something wrong.

these is what i did:

  1. in app\design\frontend\Magento\luma\Magento_Theme i have created requirejs-config.js file and put this code
var config = {
    paths: {
        'owlcarousel': "Magento_Theme/js/owl.carousel",
        'customjs': "Magento_Theme/js/custom"
    },
    shim: {
        'owlcarousel': {
            deps: ['jquery']
        },
        'customjs': {
            deps: ['jquery']
        }
    }
};
  1. then in app\design\frontend\Magento\luma\Magento_Theme\web\css i have put custom_style.css , owl.carousel.css , owl.theme.default.css

  2. in app\design\frontend\Magento\luma\Magento_Theme\web\js i have put custom.js , owl.carousel.js

  3. inside custom.js

require(['jquery', 'owlcarousel'], function ($) {
    $(document).ready(function () {
        
        $(".owl-carousel").owlCarousel({
            navigation: true,
            autoplay: true,
            autoplayHoverPause: false,
            autoplaySpeed: 2000,
            loop: true,
            smartSpeed: 450
        });
    });
});
  1. inside app\design\frontend\Magento\luma\Magento_Theme\layout\default_head_blocks.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <font src="fonts/opensans/light/opensans-300.woff2"/>
        <font src="fonts/opensans/regular/opensans-400.woff2"/>
        <font src="fonts/opensans/semibold/opensans-600.woff2"/>
        <font src="fonts/opensans/bold/opensans-700.woff2"/>
        <font src="fonts/Luma-Icons.woff2"/>
        <css  src="css/owl.carousel.css"/>
        <css  src="css/owl.theme.default.css"/>
        <css src="css/custom_style.css" media="all">
        <link src="js/owl.carousel.js">
        <link src="js/custom.js">
    </head>
</page>

also i tried to change the path like this but doesn't work either

        <css  src="Magento_Theme::css/owl.carousel.css"/>
        <css  src="Magento_Theme::css/owl.theme.default.css"/>
        <css src="Magento_Theme::css/custom_style.css" media="all">
        <link src="Magento_Theme::js/owl.carousel.js">
        <link src="Magento_Theme::js/custom.js">
  1. i create static block from admin dashboard like this
<div class="owl-carousel owl-theme">
    <div class="item"><img></div>
    <div class="item"><img></div>
    <div class="item"><img></div>
</div>

and if i wanted to put script after the div or put inline-style it get removed that's why i created custom style and js files

2 REPLIES 2

Re: owl carousel not working in magento2.4

Hi @amirkhan56e983,

 

Greetings of the day!

 

You are doing minor mistakes. Kindly check the following steps. 

 

1.Copy owl.carousel.js into app/design/frontend/vendorname/themename/Magento_Theme/web/js
(create a directory if not already there)


2. The other CSS solution could work just as well, or you could copy owl.carousel.css and owl.theme.default.css into the. Less file for the theme, and adjust the styling to your preferences.


3. Copy the following code into app/design/frontend/vendorname/themename/Magento_Theme/require-config.js
(create file/directory if necessary, this code appears to solve the "cannot read property of FN" issue.)

var config = {
paths: {
'owlcarousel': "Magento_Theme/js/owl.carousel"
},
shim: {
'owlcarousel': {
deps: ['jquery']
}
}
};

4. In the page content place the following code to define the carousel images

<div class="owl-carousel owl-theme">
<div><img src="{blah blah}"/></div>
<div><img src="{blah blah}"/></div>
<div><img src="{blah blah}"/></div>
<div><img src="{blah blah}"/></div>
</div>


5. After the above code, add the following code for the carousel (this appears to solve the "$ is not a function" error )

<script>
require([
'jquery',
'owlcarousel'
], function () {
'use strict';
jQuery.noConflict();
jQuery(".owl-carousel").owlCarousel({
nav: true, // Show next and prev buttons
navText:["<",">"], //show prev next
loop: true,
autoplay: true, //Set AutoPlay
autoplayHoverPause: true, //stop when mouse on carousel
items : 1
});
});
</script>

6. Deploy the static content, e.g. php magento setup:static-content:deploy (note there are various methods for doing this which involve the manual clearing of cache folders and a php magento cache:clean command).


7. Check it out on the website.

 

Hope this will help you to solve your problem.

If not, feel free to contact me.

 

Worked?

Click KUDOS and accept as a solution.

Thank you!

Re: owl carousel not working in magento2.4

it was because i didn't put / to close the css &link tag worked after i did this

 

<css src="Magento_Theme::css/owl.carousel.css"/>

<css src="Magento_Theme::css/owl.theme.default.css"/>

<css src="Magento_Theme::css/custom_style.css" media="all"/>

<link src="Magento_Theme::js/owl.carousel.js"/>

<link src="Magento_Theme::js/custom.js"/>

 

LitExtension - #1 Shopping Cart Migration Expert

LitExtension helps store owners and agencies migrate all important data from one eCommerce platform to another accurately, securely and at the highest speed.

Visit website: http://litextension.com/