Is there a way to replicate the iOS home screen paging effect using JS?

My goal is to develop a website optimized for tablets, specifically iOS devices, with swipe navigation between pages. I've attempted to achieve this by moving the page in relation to the touch location and then using scrollLeft on touchend to position the container at the page. Below is the code snippet:

$(function() {
  var xScreenStartPos, xScrollStartPos;
  xScrollStartPos = 0;
  xScreenStartPos = 0;
  $(document).on('touchstart', '.page', function(e) {
    xScreenStartPos = $(this).parent().scrollLeft();
    xScrollStartPos = e.originalEvent.touches[0].pageX;
  });
  $(document).on('touchmove', '.page', function(e) {
    var deltaX, xPos;
    e.preventDefault();
    xPos = e.originalEvent.touches[0].pageX;
    deltaX = xScrollStartPos - xPos;
    if ((deltaX > 50 || deltaX < -50) && scrollingVertical === false && menuOpen === false) {
      $(this).parent().scrollLeft(xScreenStartPos + deltaX);
    }
  });
  $('.page').on('touchend', function() {
    var previousPage;
    previousPage = void 0;
    $('.page:in-viewport').each(function() {
      if ($(this) !== previousPage && $(this).offset().left < 512 && $(this).offset().left > -511) {
        $(this).parent().animate({
          scrollLeft: $(this).parent().scrollLeft() + $(this).offset().left
        }, 250);
      }
    });
  });
});

Despite my efforts, I've noticed that this approach results in jittery and sluggish navigation, rather than the smooth user experience I aim for. I'm seeking a solution that replicates the seamless navigation seen when swiping between pages on an iPad home screen. While I understand it won't be identical to the native experience, any guidance on achieving a better solution would be greatly appreciated.

Answer №1

For a great swiping experience, I highly recommend checking out swipejs. It mimics native swiping effects and offers an excellent API for customization.

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

Utilize jQuery to automatically link URLs in order to enhance the appearance of user-generated content

Is it possible to generate anchor elements dynamically within any text containing a URL? For example: Transform this <p>go to http://google.com</p> Into this <p>go to <a href="http://google.com">link</a></p> ...

What is the best way to encapsulate a child's properties within a function?

Is there a way to automate wrapping each child of an object with a function? // current code import authController from "./authController"; import appController from "./appController"; import userController from "./userController&q ...

How can I achieve a letter-spacing of 0.5 pixels in CSS?

Encountering an issue, I set out to enhance readability by adjusting letter-spacing to 1px. Displeased with the result, I attempted a half pixel spacing of 0.5px but found it ineffective. Subsequently, I experimented with em attributes but failed to accomp ...

What is the process for modifying data in JSON structure?

Here is a snapshot of my view model: var PatientModel = function () { this.id = ko.observable(idValue); this.name = ko.observable(nameValue); this.degree = ko.observable(degreeValue); this.gender = ko.observable(genderValue); this.user ...

Various web browsers (display: -webkit-inline-box;)

Recently, I encountered an issue with my CSS code: .tudo ul { width: 100%; margin: 0px; display: -webkit-inline-box; } Surprisingly, Mozilla Firefox does not recognize the following line: display: -webkit-inline-box; Is it possible to set d ...

Trouble arises as the Raspberry Pi GPIO pins refuse to toggle

Encountering an intriguing issue here. I've developed a Python program to regulate the climate of a room with a web interface. While the program functions properly, upon restarting the Raspberry Pi, all GPIO pins are activated. It's only after ac ...

The HTML5 video tag may not start playing in Safari until the video has fully downloaded

While successfully loading videos on browsers using the HTML5 tag, I encountered a problem specifically with Safari. In all other browsers, the video plays while buffering, but in Safari it waits for the entire video to download before playing. Even after ...

I have an array in JavaScript containing data objects within curly braces, and I want to disregard it

In trying to sum all values for each key in an array and create a new array with the key-total pairs, encountering difficulties when dealing with empty data represented by {}. To address this issue, attempting to validate that the data is not equal to {} b ...

Is there a way to prevent continuous repetition of JavaScript animated text?

I'm working on a code that displays letters and words one by one, but I can't figure out how to stop it from repeating. Can someone help me with this? <div id="changeText"></div> <script type="text/javascript"> var te ...

Transfer data to image then back to data

After executing code like this: NSData *originalImageData = [NSData dataWithContentsOfFile:@"somefolder\somepicture.jpg"]; UIImage *theImage = [UIImage imageWithData:originalImageData]; Is there a method to retrieve the original NSData from the UIIm ...

An issue was encountered while trying to retrieve the months using the date.getMonth

I came across this page that had some sample code which seemed perfect for the project I'm currently working on. Datepicker for web page that can allow only specific dates to be clicked After trying to implement the code, I encountered the following ...

Tips for sending a localstorage value as a parameter in an axios request

Currently, in my React app, I am using Express to handle all of my database queries. One thing I am attempting to achieve is passing the user id stored in local storage into a post request. This is necessary for my application since it revolves around use ...

Instructions for removing a single key value pair from every object in an array and transferring it to a designated object within the same array

I need to update an array of objects by adding a key and value (e.g., age:15) to an object with the name email, while removing the age property from other objects in the array. [ { name: 'test', lname: 'last', age: 5 }, ...

Once a new element is inserted into the DOM, it no longer triggers the same functions as before

<script type="text/javascript"> $(function(){ $('button').each(function(i){ $(this).click(function(){ $(this).after('<br /><button type="button">Button</button>'); }); }); }); ...

Issue with loading CSS in Magento transactional emails

I designed a custom email template specifically for order cancelations. The email is functioning properly, with the header and footer displaying correctly. However, the existing CSS is not being applied to the email template. While default headers and fo ...

The Print Preview Displays No Content When an External Stylesheet Reference is Included in the Printable HTML Content

Is there a way to print the contents of a DIV on a webpage? Currently, I am using JavaScript to retrieve the contents of the div and then passing it to a new window object where I call the .print() function. The text contents and images are displayed corre ...

Steps for activating mouse dragging in a div that supports drag and drop functionality

I have a div containing multiple elements, and I need the ability to drag and drop the entire div with the attribute draggable=true. However, one of the elements in the div is a bar graph that allows users to select a specific range by dragging their mouse ...

Experience the sleek sliding effect of the Flipboard iPad app's right menu panel

How can I achieve a right panel slide effect similar to the image provided? Clicking on the left area should hide the right panel. Image URL: I'm considering: Is it implemented with a popover controller? Could it be a view added using "addSubview" ...

Efficient Json Parsing with Swift

Currently, I am utilizing the SwiftyJson library to parse a JSON object as shown below: { "data": { "id": "12345", "messages": { "message": "{\"data\":{\"msg\":\"HelloMsg\"}}" } } } ...

Creating a text file while in a suspended state within the event handler on Windows 8 using HTML5

When the suspend event is triggered inside the winjs.application.oncheckpoint event handler, I am attempting to write a text file. The content of the file is my object in JSON format. Below is the code snippet: applicationData.localFolder.createFileAsync( ...