Issues with Swiper functionality in Jquery Mobile

I have implemented the Swiper plugin by idangero.us along with jQuery Mobile...

In this case, I am utilizing the Scroll Container Swiper for a content slider...

However, I am encountering several difficulties when trying to integrate the code...

You can view the issue at this link: http://jsfiddle.net/keuKu/4/embedded/result/

The scrollbar does not display as shown in the demo, and the scroll position is not maintained properly...

$(function(){
        /* Scroll container: */
        var sScroll = $('.swiper-scroll-container').swiper({
            scrollContainer : true,
            scrollbar : {
                container : '.swiper-scrollbar' 
            }
        })  
     })

Answer №1

For demonstration purposes, you can view a live example here: http://jsfiddle.net/Gajotres/B7hwh/

In my approach, I made some adjustments to the original code. Instead of using document ready in jQuery Mobile, which may not trigger correctly during page initialization, I replaced it with the appropriate page event for proper functioning within a specific page context.

To ensure that the swiper functionality is applied only once and not every time the page is revisited, I utilized the pageshow event along with a conditional check. This was necessary because swiper requires accurate page dimensions, which are best calculated during the pageshow event.

If you want to explore the modified code further or see it in action, feel free to visit the working example provided: http://jsfiddle.net/Gajotres/B7hwh/

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>    
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
        <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>   
    </head>
    <body>
        <div data-role="page" id="page">
            <!-- Page content goes here -->
        </div>

        <div data-role="page" id="slider">
            <!-- Slider page content goes here -->
        </div>    
    </body>
</html>       

Javascript:

$(document).on('pageshow', '#slider', function(){ 
    /* Swiper initialization and configuration */
    if($('.swiper-scroll-container .swiper-scrollbar-cursor-drag').length == 0) {
        var sScroll = $('.swiper-scroll-container').swiper({
            scrollContainer : true,
            scrollbar : {
                container : '.swiper-scrollbar' 
            }
        });     
    }
});

CSS:

.swiper-scrollbar {
        /* CSS styles for scrollbar */
    }
    .swiper-scrollbar-drag {
        /* CSS styles for scrollbar drag element */
    }

Answer №2

I found a straightforward solution that worked well for me: creating a basic JQuery Mobile page division containing a simple iframe linking to a detailed slider page using src="". By doing this, the slider's DOM is isolated from your primary application and can function smoothly within JQM dialogs and similar elements.

Answer №3

In addition, I believe that integrating certain jQuery UI libraries can enhance the swiper functionality on mobile devices. To address this issue, I successfully resolved it by incorporating a delay.

setTimeout(() => {
    var swiper = new Swiper('.swiper-container', {
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
    });
}, 250);

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

What are my choices for showcasing hierarchical information?

I'm currently working on organizing hierarchical data using HTML, JS, and jQuery. My chosen method involves utilizing a left-floated div container system in place of a grid system for easier recursive code generation. One challenge I've encounter ...

Create a separate server session specifically for handling an ajax request?

Currently, I am working with a collection of PHP server-side scripts that manage user session state by utilizing PHP sessions extensively for authenticated users. For the client side within a mobile application and using Jquery ajax, I am striving to esta ...

Tips for eliminating the ripple effect when clicking on a q-list item

I have managed to create a sleek sidebar with a curved edge that seamlessly integrates into the body of the page. However, I am struggling to remove the semi-transparent ripple effect that appears when clicking on a list item. The current effect clashes ...

Utilizing the .on() method to select elements based on a unique attribute

It is possible to use the .on() method to attach a single click event to an element and then specify which child elements receive the click. For example: $(this.el).on("click", "span", function () { alert("Bloop!"); }); If you need to be more specifi ...

Adaptable material for collapsible panels

I'm facing a challenge with implementing an accordion menu. My goal is to have a 4 column layout that transforms into an accordion menu when the browser width is less than 600px. It almost works as intended, but there's a glitch. If you start at ...

Curved divs display outlines on more compact dimensions

Check out this relevant Codesandbox There seems to be a recurring issue in my application where rounded divs display edges when they are of smaller sizes. Refer to the image below for a visual representation. What could be causing this problem and how can ...

How Webpack 4 Utilizes splitChunks to Create Numerous CSS Files

I need to create multiple CSS files using webpack 4 with the min-css-extract-plugin and splitChunks plugin. Here is my webpack configuration. const MiniCssExtractPlugin = require("mini-css-extract-plugin"); const path = require("path"); module.exports = ...

Ways to modify the hue of an li element in the context menu upon hovering

I'm currently working on a project that involves VueJS and Bootstrap. To enhance user experience, I've incorporated a context menu using the npm package called vue-context Vue Context Menu When a user hovers over an item on the context menu, the ...

Upon refreshing the datatable, I encountered a issue where the checkbox cannot be selected

After updating my data table with new content through an AJAX request, I am facing an issue where I am unable to select the check-boxes in the table. I use a class selector to choose the rows that contain multiple check-boxes. The select event is placed in ...

Creating a CSS auto center for elements, with a unique twist

I have a design layout like this: Click here to view the image My auto-centered content is styled with the following CSS: width: 960px; margin: 0px auto; Within this auto-centered content, there is a DIV element that needs to span the full width of the ...

I am attempting to implement a feature that changes the color of buttons when they are clicked within an ng-repeat loop

A problem is occurring with the ng-class directive in this HTML code, which is generating seats using the ng-repeat directive. The colors are not being added properly when a seat is selected. If you'd like to view my code, it can be found in this JSf ...

When attempting to bind various data to a single div using knockout js, the issue of duplicate records appearing arises

I am encountering an issue with a div that is set up to display 10 records at a time. When the user clicks on the next link, the next set of 10 records should be loaded from the server. However, after binding the newly added records, they are being shown m ...

Enhance your ReactJS application by integrating Bootstrap 4 for a sleek

Recently, I installed bootstrap 4 using yarn add, but now I'm facing issues with using components that rely on JavaScript as they require jQuery. Can anyone guide me on where to find the webpack file for adding files related to jQuery and Proper? My p ...

Tips for saving HTML data locally

Is there a way to persist my HTML element tag data even after the user closes the browser? For example: <div class="classOne" data-random="50"> If I use jQuery to change the data attribute like this: $(".classOne").attr("data-random","40") How ca ...

Various HTML tables display varying column widths even when utilizing the same colgroup settings

I'm facing an issue with multiple HTML tables on a single page, each following this structure: H2 Header Table H2 Header Table H2 Header Table My requirement is to have all tables occupy 100% width, with columns set at widths of 50%, 30%, and 20%. I ...

Can I extract JSON data from the AJAX response?

When receiving JSON data, such as in the example below, it is often necessary to separate each value into individual variables: var reviewDate ='2015-06-01T05:00:00Z' var developers ='Ankur Shah,Srikanth Vadlakonda,Tony Liu, Qiuming Jie&ap ...

Modify the button's color based on a separate column within the table

Currently, I am implementing Datatables along with X-editable and including some bootstrap buttons within a table. My objective is to change the color of the button in the 'Validated' column to green when the user updates the editable 'Statu ...

Contrast arrays and eliminate values that do not match

$scope.territories = [ { name : "One"}, { name : "Two"}, { name : "Three"}, { name : "India"}, { name : "Japan"}, { name : "China"} ]; $scope.tempTerritories = [ { name : "One"}, { name : "Two"}, { name : "global"}, ]; ...

Enable checkboxes to be pre-selected upon page loading automatically

Although I've found a lot of code snippets for a "select all" option, what I really need is more direct. I want the WpForms checkboxes to be pre-checked by default when the page loads, instead of requiring users to press a button. My goal is to have a ...

Obtaining the value of the selected option in an autocomplete textbox using jQuery

How can I retrieve the selected value from an auto-complete textbox using JQuery? <script type="text/javascript" src="JS/jquery.min.js"></script> <script src="JS/jquery.autocomplete.js"></script> <script> ...