Several Divs Positioned on Different Pages with Backgrounds That "Scroll"

Currently utilizing: jQuery Mobile 1.4.5

I am interested in knowing if it is feasible to achieve the following, and if so, how to start working on it.

At the moment, there are 4 page divs that utilize swipe for navigation between them with a horizontal transition.

My goal is to have one long background that moves slightly (according to the page width) as users progress through the containers. This will create a smooth scrolling effect and reveal more of the background.

I may be approaching this in the wrong way, so any helpful tips would be greatly appreciated.

Thank you!

Answer №1

To achieve this effect, you can set the background-image to either the body element or a page division on your website.

If you opt for setting it to the body, you will experience an animated background transition. In this case, you need to ensure that the page division is made transparent.

body {
  background-image: url(image.png);
}
.ui-page {
  background: transparent !important;
}

During the pagecontainerbeforechange event, calculate the new position of the background by multiplying the index of the page being navigated to by the screen/window width and updating the background's background-position-x.

On the initial page, keep the background-position-x at 0. Also, add a listener for pagecontainercreate to obtain the index() of the page being navigated to.

$(document).on("pagecontainercreate", function () {
    $(document).on("pagecontainerbeforechange", function (e, data) {
        if (typeof data.toPage == "object") {
            var index = data.toPage.index(),
                screenWidth = window.innerWidth;
            $("body").animate({
                'background-position-x': "-" + screenWidth * index + "px"
            });
        }
    });
});

Check out the Demo!


Alternatively, you can set the background-image directly to the page division and adjust the background-position-x based on the index() during the pagecreate event for each page.

.ui-page {
  background-image: url(image.png);
}
$(document).on("pagecreate", function (e) {
    var index = $(e.target).index(),
        screenWidth = window.innerWidth;
    $(e.target).css({
        "background-position-x": "-" + screenWidth * index + "px"
    });
});

View the Demo here!

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

Simple method for converting pixel values to em units

I'm searching for a simple method to incorporate a line of code into one of my plugins in order to convert specific pixel values into em values. My project's layout requires the use of ems, and I'd prefer not to rely on external plugins for ...

Sliding through sections with a full-screen vertical gap using the Slick Carousel

I have implemented the slick carousel plugin from here with vertical slides and I am using jQuery to adjust the height of the section to fill the entire screen. However, I seem to be facing a peculiar issue where on sliding to the 4th and 5th slide, the b ...

AngularJS IP Address masking

Looking for an IP address mask plugin that works with AngularJS. I attempted to use the "Jquery Input IP Address Control" plugin, but it wasn't functioning properly. The problem I encountered was that the "ngModel" attribute wasn't capturing the ...

Creating a centered transparent rectangle of a specific width using HTML

I am working on a page layout similar to this FIDDLE body { margin:0 auto; max-width: 800px; padding:0 0 0 0; text-align:left; background-color:#FFFFFF; background-image: url('http://freeseamlesstextures.com/images/40-dirty-pa ...

What is the best way to delete a CSS class from a specific element in a list using React?

I need to implement a functionality in React that removes a specific CSS class from an item when clicking on that item's button, triggering the appearance of a menu. Here is my code snippet. import "./Homepage.css" import React, { useState, ...

What measures can be taken to guarantee that a user is only able to delete designated records?

When it comes to writing SQL commands directly, it's simple to specify which records to delete using identifiers like ID. However, when dealing with webpages that contain a list of links pointing to different records (with the ID embedded as a query ...

Issue with vertical alignment within nested divs

I have been attempting to center a search bar inside a top banner vertically. My understanding was that the following code could achieve this: #banner { height: 35px; width: 100%; } #searchbar { height: 15px; position: relative; top: 50%; ...

Foundation: the importance of source order (without using flex grid)

I have a 3-column grid, with content and a sidebar. On small and medium devices each takes up 12 columns. Take a look at the example here: cdpn.io/anon/pen/bqENqG. I am looking to have the sidebar appear after the articles on small and medium devices like ...

The system of jQuery Dialog UI is unable to recognize when the close icon is clicked

How can I detect if a user closes my dialog modal box using the 'X' button in the upper-right corner of the titlebar? I've been struggling to find a solution while using jQuery's Dialog UI plugin. Here is what I have tried so far: $(&a ...

Positioning the horizontal menu and footer links to the right side of the page

Struggling to get my menu and footer links aligned all the way to the right of the page. Here's what it currently looks like http://prntscr.com/32snbr, and this is the desired alignment http://prntscr.com/32snrm I've shared the HTML code below, ...

How can I properly integrate jQuery libraries, along with CSS, in a create-react-app project?

I'm currently working on incorporating the select2 jquery library into my React application, which was created using create-react-app. After adding jquery and select2 to my package.json file, I was able to get the javascript functionality to work pro ...

Struggling to insert a high-quality image into inline divs of exactly 33.3333% width

After creating 3 inline divs, each taking up 33.333% of their parent width, I encountered an issue while trying to insert images into them. Whenever the image exceeds the 33.333% width of the div, it overflows outside the container. My goal is to make the ...

The Flot chart updates with a new data point appearing at the end whenever there is a change in

Incorporating the following flot chart directive: /**========================================================= * Module: flot.js * Initiates the Flot chart plugin and manages data refresh =========================================================*/ (fu ...

Turn off the autofill option for passwords in Google Chrome

Is there a way to prevent the password autocomplete dropdown from appearing when clicking on the password field? In Chrome Version 61.0.3163.100, none of the solutions seem to be working. Screenshot This issue has been raised many times, but setting autoc ...

Issues with hidden overflow and height attributes not functioning correctly in Internet Explorer 9 when using the DOCTYPE HTML standards mode

I have a <div> with overflow:hidden and height:100% that adjusts the div contents to fit the window's height on Chrome and Safari. However, in IE9, the div does not respect the styles of overflow:hidden and height:100%, causing it to expand to ...

Is there a way to rearrange html elements using media queries?

Here is a snippet of code showcasing how my website is structured for both desktop and mobile views. Display on Desktop <body> <div> <header> <div>example</div> <a><img src"my logo is here"/& ...

Ensure that the HTML image and text are positioned side by side, with the text appearing alongside the picture rather than below

Looking for a way to keep the text aligned to the right of an image, regardless of length? Check out this exercise: https://www.example.com/css/exercise-1 Is there a method to maintain the text layout next to an image while accommodating varying amounts o ...

angular material drag and drop triggers a callback once the css transition has completed

I have successfully implemented a list of elements that can be dragged and dropped using Angular Material's drag and drop feature, similar to the tutorial available at this link. In my implementation, I have a "drop(event)" function that not only mov ...

Jssor Slider: Captions briefly overlap just as the slideshow begins to play

I am currently working on a slideshow with 3 caption-only slides. Here is the code snippet: <div id="sliderHeader_container" style="position: relative; width: 965px; height: 85px;"> <!-- Slides Container --> <div u="slides" style=" ...

Adjust the dropdown width based on the screen's resolution

I recently encountered an issue while implementing a dropdown menu in my React application. Everything was functioning smoothly until I attempted to change the browser resolution through the console. // index.js: <select id="project_type" ...