Creating a responsive touch slider: tips and tricks

I am looking to transform my gallery into a touch slider specifically for mobile devices. To achieve this, I have created two sections - one for desktop and another for mobile. I am utilizing the display property in a media query but my slider is not responsive even after applying width:100%. I want to ensure that it becomes responsive and also add next or previous buttons on the slider.

$(window).load(function(){
       Add appropriate JavaScript functionality here...
    });
    

      Add your CSS code snippet here...
    

      Add your HTML markup here...
    

Answer №1

To achieve your desired outcome, you can duplicate all the images into a separate section, toggle between them based on screen size, and utilize the Owl Carousel library () for mobile devices.

Using a carousel library rather than building one from scratch saves time and effort, especially when implementing Touch and Drag support.

HTML

<div class="container">
    <!-- Display as block until md breakpoint (768px) -->
    <section class="owl-carousel owl-theme d-md-none">
        <div class="item">
            <img class="img-fluid" alt="image 1" />
        </div>
        <div class="item">
            <img class="img-fluid" alt="image 2" />
        </div>
        <div class="item">
            <img class="img-fluid" alt="image 3" />
        </div>
    </section>

    <!-- Display none until md breakpoint (768px), as block after -->
    <section class="gallery d-none d-md-block">
        <div class="row">
            <div class="col">
                <img class="img-fluid" alt="image 1" />
            </div>
            <div class="col">
                <img class="img-fluid" alt="image 2" />
            </div>
            <div class="col">
                <img class="img-fluid" alt="image 3" />
            </div>
        </div>
    </section>
</div>

This setup allows displaying the carousel section only on screens less than 768px width, and showing the gallery section otherwise.

JavaScript

$(function() {
    $('.owl-carousel').owlCarousel({
        margin: 10,
        nav: true,
        items: 1
    });
});

By using this script, the carousel will be transformed into touch-enabled with drag functionality. For more details, refer to the documentation:

Result

On small screen: https://i.sstatic.net/1AXhg.png

On larger screen: https://i.sstatic.net/e4rF4.png

fiddle: http://jsfiddle.net/aq9Laaew/243804/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Position the text of an h1 element next to its corresponding image in HTML5

I'm attempting to create a HEADER element with an H1 tag alongside a logo image. Here is the code snippet: Company name However, the issue is that "Some title" is not displaying correctly as it appears b ...

I am experiencing issues with the button click functionality in my Google extension

Greetings! I am currently developing a Chrome extension that will automatically redirect to my website and fill in the password. However, I am facing an issue with submitting the button click event. // Here is my manifest.json code { "name": "z", "v ...

'jQuery' is not recognized as defined, error no-undef

I am currently working on a file that utilizes jQuery for testing purposes: (function($) { "use strict"; // Start of use strict // Configure tooltips for collapsed side navigation $('.navbar-sidenav [data-toggle="tooltip"]').tooltip({ ...

JavaScript method parameters are essential components that help define the methods

Can someone explain how function definitions work within a method in Javascript? An example I'm struggling with is in AngularJS: sth.controller('ctrlname', function ($scope, $ionicModal, par3, par4){ //code }); Another piece of code ...

Require the utilization of both versions of jquery 1.7.2 and 1.8.2

I am facing a dilemma where I need to use two different versions of jquery on the same page. I require version 1.7.2 for a specific function that retrieves data from a database based on a selection change in PHP, and I also need version 1.8.2 for form vali ...

Determine if a JavaScript code in my ASP view will be executed based on control from the controller

Working on an ASP.NET MVC4 system, I have a javascript code that displays a performance graph of students when the page loads. Here is the code for the graph: <script> window.onload = function () { var r = Raphael("holder"), ...

JavaScript Question: How can I extract the click value from a JavaScript visualization when displayed in a table?

I am working with a Hierarchical Edge Bundling visualization in JS. My goal is to have the name of the value displayed on the table when I click on it. Currently, I am facing an issue with retrieving the value dynamically. If I manually input a value, I c ...

Maintain visibility of the vertical scroll bar while scrolling horizontally

Currently, I am in the process of setting up a table with fixed headers and columns that have a minimum width. To ensure this setup functions correctly, I require both vertical and horizontal scroll bars. The vertical scroll bar should only navigate throu ...

Using Vue js to scrollIntoView will only affect the specific element's div

I am currently working on a Vue page that consists of two divs. The Select-Div contains a list of elements, and when I click on an element from the list, the Scroll-Div is supposed to scroll to the corresponding ID element inside it. Ideally, the scrolling ...

best practices for data flow between components in React using hooks

I have successfully retrieved data from my database in the Recipes component. Now, I am attempting to pass this data into the RecipeList component. However, when I do so, only the bullet points are showing up in the RecipeList component and for some reas ...

Ways to retrieve information from a specific key

I'm currently facing a challenge accessing specific data objects that are referenced by keys. In this particular scenario, the "applicant" data is nested within an Event object. My goal is to extract this data and create a new object from it. While I ...

JavaScript controller with numerous dependencies in AngularJS

I have created a new controller: .controller('myCtrl', function($scope, $route, $routeParams, $location) Now I am trying to inject something called SharedState. I attempted: .controller('chatCtrl' ['SharedState', function($ ...

Conditionally defining variables in JavaScript only if they are currently undefined

I have been working on extracting two keywords from a URL in the following format: localhost:3000/"charactername"/"realmname" My goal is to extract "charactername" and "realmname" and assign them to variables. Here is the code snippet I am using: var c ...

What causes my CSS styles to vanish upon refreshing the page in Next.js?

After setting up a CSS folder within my app directory and importing the files into components for use, I noticed that the styles are applied initially. However, upon refreshing the page, they all disappear. I attempted to resolve this issue by going back ...

If a user touches outside of the scroll component on mobile, the scroll in the popup freezes

I have a webpage that includes two scrollable lists: one in the main body and another in a popup window. To prevent scrolling of the main body when the user scrolls inside the popup, I am using a standard solution recommended in this Stack Overflow post ab ...

EasyWaySaveArchive in ninja training - mastering the art of retrieving components

Recently started learning about dojo and JavaScript in general. I am currently working on a code snippet that requires a button to change based on the result of a php database query. The PHP script is already complete, and the code I am using so far looks ...

What is the best way to create an Angular image slider that smoothly transitions between slides without any intermediate steps?

In my Angular application, I've successfully implemented an image slider. However, I am looking to tweak its behavior specifically when navigating between non-adjacent slides using the circular navigation buttons located below the slider. Currently, ...

Challenges with Loading JSON Dynamically in Next.js using an NPM Package

In my TypeScript project, I have implemented a functionality where a json configuration file is dynamically loaded based on an enum value passed as a parameter to the getInstance function in my PlatformConfigurationFactory file. public static async getIn ...

In the production build of Next.js, accessing dynamic routes may lead to a 404 error

Incorporating Dynamic Routing in my next JS project has been a seamless process so far. During development mode, all features are functioning as expected. However, I noticed that when I access a dynamic page for the first time, it loads without any issue ...

Using AngularJS to Send Arrays of Objects to PHP

Currently, I am adapting to using AngularJS ng-resource, however, I am encountering an issue with serializing an array. The ng-resource implementation that I am working with is structured as follows: app.factory('MyModel', ['$resource' ...