Designing a content carousel specifically optimized for mobile devices

I'm currently working on creating an image slider that is responsive to screen size, specifically functioning when the screen width is less than 640 pixels. For wider screens, it should display 3 items with 33% width to fit nicely. I have been able to make the slider slide, but when resizing back to a larger size and clearing the interval, the slider freezes and leaves distorted images. Is there a way to restrict the slider to work only on one screen size? The code provided below showcases my current progress, however, I have not found any similar tasks or examples online.

Jquery Code

 var slider = $("#slider")
    var container = $("#slides");
    var slides = $(".slides");
    var len = 3;
    var current = 1;
    var width = '100%';
    var animationSpeed = 3000;
    var pause = 3000;
    var interval;

    function startSlider() {
        interval = setInterval(function () {
                container.animate({
                    'margin-left': '-=' + width
                }, animationSpeed, function () {
                    if (++current === len) {
                        current = 1;
                        container.css('margin-left', 0);
                    }
                });
            }, pause);
    }

    function stopSlider(){
        clearInterval(interval);
    }

    function checkView() {
        var winWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
        if(winWidth < 640   ) {
           startSlider();
        } else{
            stopSlider();
        }

    }
    checkView();
    $(window).resize(checkView);

Html Code

<section id="slider" class="slider clearfix">
       <nav class="nav-arrow">
           <img id="#prev" class="prev" src="images/1463330656_br_prev.png" alt="prev">
           <img id="#next" class="next" src="images/1463330669_br_next.png" alt="next">
       </nav>
     <ul id="slides" class="slides">
         <li><img class="slide" src="images/box.jpg" alt=""></li>
         <li><img class="slide" src="images/bridge.jpg" alt=""></li>
         <li><img class="slide" src="images/smile.jpg" alt=""></li>
     </ul>
</section>

Answer №1

Consider resizing the content or adjusting 'CheckView' based on css media queries rather than manually setting sizes in your jquery code.

You can add multiple media queries to your css, allowing you to modify the size as needed. For example:

@media only screen and (min-width:320px) and (max-width:640px){ // include your relevant css here}
 @media only screen and (min-width:1024px){ // include your relevant css here}

Please keep in mind that these are just sample queries.

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

Looking for ways to ensure React is responsive and feeling a bit challenged by the React mentality?

I'm still getting the hang of ReactJS, and I've been facing some challenges with making React components responsive. For my app, I am utilizing react-tabs. It works well when the screen is wide, but I need to switch to a hamburger panel layout w ...

HTML counterpart to PHP's include for JavaScript components

I am searching for a Javascript alternative to a method I have been using in PHP. Specifically, I want to streamline the basic setup of my pages: <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

What is the functionality of react.js state?

As I delve into learning React.js, I've been quite impressed with its state management capabilities. However, I'm curious about the technical workings behind it. Does React.js utilize cookies or browser storage for its state management? ...

What is the best way to interpret the JavaScript code within a Vue/Quasar project?

Sample Code: <script type="text/javascript" src="https://widget.example.com/widgets/<example_id>.js" async defer></script> I am looking to integrate this code into Quasar Framework and utilize it with Vue.js. Do you have any suggesti ...

JavaScript: Populating an Array with Image URLs Using a Loop

My image gallery project has hit a roadblock and my JavaScript skills are not up to par. Maybe it's time to brush up on the basics with a good book from the library. Here's what I want to achieve: 1. Create an array of URL's for each imag ...

Conceal social media icons when hovering over specific images similar to the feature on the website medium

I am facing an issue with fading out social links that appear over medium and large images on a webpage. I want the social links to fade out when they are over the images and fade back in once they move off the images. The number of medium and large images ...

Manipulating jQuery datepicker settings programmatically from the codebehind

I am in the process of developing a custom user control that incorporates the jQuery date picker widget. The current implementation is as follows: $(function () { $("#datePickerYearMonth").datepicker({ changeMonth: true, changeYear: tr ...

How can I extract echoed information from PHP after submitting form data through jQuery Ajax?

I am looking to transfer a file from an input form to a php script using ajax and handle the response echoed by my php script. Here is my HTML form: <form id="fileUploadForm" method="POST" enctype="multipart/form-data"> <input name="fileToU ...

Implementing Formik in React for automatic updates to a Material-UI TextField when blurred

Presently, I am developing a dynamic table where users can simultaneously modify multiple user details in bulk (Refer to the Image). The implementation involves utilizing Material-UI's <TextField/> component along with Formik for managing form s ...

What is the best way to upload a file to Firebase Storage using React?

I am facing difficulty uploading a file to Firebase in order to retrieve its getDownloadURL. This is the code snippet I currently have: import React, {useState, useEffect} from 'react' import { Container, Button, Row, Col, Form, Alert } from &ap ...

React Transition Group failing to apply CSS classes

I am utilizing React Transition Group to apply animations with different timeouts to each item. However, the issue arises when my links do not receive any classes. Despite this, wrapping the ScrollLinks tag with TransitionGroup allows the animation to func ...

Enhance the aesthetics of placeholder text in Semantic UI React using CSS styling

Currently, I am utilizing Semantic UI dropdowns within my React application to generate drop-down menus similar to the ones showcased in their documentation found here: The initial text displayed is "Select Friend", and it appears with a semi-transparent ...

Issue with Javascript function failing to assign correct value

Here is a JavaScript function I am working with: function editWebsite(IP) { document.getElementById('ctl04_txtIP').value = IP; } The ctl04_txtIP textbox represents the IP address passed through the parameter IP in the editWebsite function. ...

Utilizing jQuery and DOM to interact with form elements

Below is the form content <form method="post"> <input type="hidden" name="resulttype" value="Create"> <table> <tr> <td>Full Name</td> <td><input ...

The getMonthlyBalances function is providing inaccurate results when calculating stock balances

One of my functions is called getMonthlyBalances, which takes in two arrays - stocks and trades. It calculates the monthly balance of a user's stock holdings based on their trade history. The stocks array includes objects with stock ID, prices, and da ...

Add a new division to a component when a specific event handler is triggered by another component using React and the reduce method

Currently, I am developing an interactive drag-and-drop application using React and Redux. My goal is to insert a new div element into a container when the ondragstart event handler is triggered by a component. The component responsible for the dragging o ...

Using React and Ant Design: Sharing state between multiple <Select> components within a <Form> element

Just getting started with ReactJS and AntDesign. Currently, I am working on connecting a series of <Select> components to share state. The goal is for the selection made in the first dropdown to dynamically update the options available in the follow ...

Exploring the concept of calculating scaled distance in TopoJSON files

I have been exploring various methods to connect pixel space with real-world distances using a projection. I came across the following method which proved to be quite useful: var actual_map_bounds = d3.geo.bounds(this_topojson); var radians = d3.geo. ...

What is the best way to repair a table header?

How do I make the table header fixed so only the table body scrolls? * { padding: 0px; margin: 0px; } body { padding: 0px; margin: 0px; } .responsive { max-width: 100%; height: auto; } /* Other CSS code here */ /* Include necessary exter ...

Instructions for integrating a select query within another select query in Codeigniter

I have a question regarding two tables shown below: /* questionnaire table */ | q_id | q_title | | ------- | ---------------------- | | 1 | What is your name? | | ------- | ---------------------- | | 2 | What is your gend ...