Attempting to create a multi-page slider using a combination of CSS and JavaScript

Looking for help with creating a slider effect that is triggered when navigating to page 2. Ideally, the div should expand with a width of 200% or similar. Any assistance would be greatly appreciated!

http://jsfiddle.net/juxzg6fn/

<html>

<head>

    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <style>

        body {
            overflow: hidden; <!-- remove scroll -->
        }

        .page {
            position: absolute;
            padding: 12px;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }

        .page.left {
            -webkit-transform: translate3d(-100%, 0, 0);
            transform: translate3d(-100%, 0, 0);
        }

        .page.center {
            -webkit-transform: translate3d(0, 0, 0);
            transform: translate3d(0, 0, 0);
        }

        .page.right {
            -webkit-transform: translate3d(100%, 0, 0);
            transform: translate3d(100%, 0, 0);
        }

        .page.transition {
            -webkit-transition-duration: .25s;
            transition-duration: .25s;
        }

    </style>

</head>
<body>

    <!-- PAGE 1 -->
    <div id="homePage" class="page transition center" style="background-color: #5AAED5">
        <h1>Home Page</h1>
        <a href="javascript:slidePageFrom(page1, 'right');">Page 1</a><br/a>
        <a href="javascript:slidePageFrom(page1, 'right');">Page 2</a>
    </div>

    <!-- PAGE 2 -->
    <div id="p1" class="page transition right" style="background-color: #8ca83d">
        <h1>Page 1</h1>
        <a href="javascript:slidePageFrom(homePage, 'left');">Back</a>
    </div>

    <script>

        var homePage = document.getElementById("homePage");
        var page1 = document.getElementById("p1");

        var currentPage = homePage;

        function slidePageFrom(page, from) {
            // Position the page at the starting position of the animation
            page.className = "page " + from;

            // Position the new page and the current page at the ending position of their animation with a transition class indicating the duration of the animation
            page.className ="page transition center";
            currentPage.className = "page transition " + (from === "left" ? "right" : "left");
            currentPage = page;
        }

    </script>


</body>
</html>

Any assistance would be greatly appreciated!

Answer №1

To invoke page2, you can use the following code:

<a href="javascript:slidePageFrom(page2, 'right');">Page 2</a>
. It's important to note that page2 must be declared as a variable before being used.

To rectify this issue, simply add the following line to your JavaScript code:

var page2 = document.getElementById("p2");

Alternatively, for each new page added, you'll have to declare a new variable. To make things simpler, consider making it dynamic by utilizing the pages' id (which is already in use).

You can modify your HTML like so (pay attention to the single quotes in the anchor tags around p1, p2, and homePage):

<!-- PAG 1 -->
<div id="homePage" class="page transition center" style="background-color: #5AAED5">
    <h1>Home Page</h1>
    <a href="javascript:slidePageFrom('p1', 'right');">Page 1</a><br/>
    <a href="javascript:slidePageFrom('p2', 'right');">Page 2</a>
</div>
<!-- PAG 2 -->
<div id="p1" class="page transition right" style="background-color: #8ca83d">
    <h1>Page 1</h1>
    <a href="javascript:slidePageFrom('homePage', 'left');">Back</a>
</div>
<!-- PAG 2 -->
<div id="p2" class="page transition right" style="background-color: #8ca83d">
    <h1>Page 2</h1>
    <a href="javascript:slidePageFrom('homePage', 'left');">Back</a>
</div>

Then utilize the corresponding JS code provided below:

var currentPage = document.getElementById('homePage');

function slidePageFrom(page, from) {
    var page = document.getElementById(page);
    page.className = "page " + from;

    page.className = "page transition center";
    currentPage.className = "page transition " + (from === "left" ? "right" : "left");
    currentPage = page;
}

For a demonstration, check out this working demo.

Answer №2

insert this code:

const section = document.querySelector("#section");

I recommend using this updated version: http://jsfiddle.net/abc123def/2/ Best of luck!

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

The CKEditor value is set to the result of the dropdown selection

I need to implement a dropdown feature on my form where the options correspond to titles of content in my database. Once an option is selected, I want the corresponding content to display in a CKEditor field. I'm attempting to achieve something simil ...

The callback function in a Node.js loop can be executed in parallel

Exploring the behavior of callbacks invoked by async.parallel functions has led to an interesting observation: the execution flow appears to differ depending on whether the callback utilizes a parameter or not. var async = require("async"); function cr ...

The Dojo claro css method utilizes absolute positioning for styling ContentPane elements

I am currently utilizing dojo 1.8 and facing an issue with unwanted padding in my bordercontainer/contentpane layout. The problem arises when I incorporate the claro css file, as it seems to apply styles directly inline to the div elements used for my cont ...

The occurrence of an unanticipated character '#' was found

I'm currently facing an issue while using webpack to load dependencies. Whenever I execute the npm run dev command, I encounter the following error: Uncaught Error: Module build failed (from ./node_modules/babel-loader/lib/index.js): SyntaxError: ...

Using ngForm to implement multiselect options in HTML

Attempting to implement a multiselect dropdown that is tied to a dynamic property receiving data from a JSON script via service. Successfully displayed the data in the dropdown, but encountering abnormalities when adding the multiple attribute within the s ...

commenting system through ajax across multiple web pages

Experimenting with the webcodo comment system has led me to this URL: Database table: "comments" CREATE TABLE IF NOT EXISTS `comments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(40) NOT NULL, `email` varchar(60) NOT NULL, `comment` te ...

Sending an ID from an array within a *ngFor loop to a different component in Angular: a step-by-step guide

I have a collection of items that I loop through using ngFor. My goal is to pass all its attributes to another component. I attempted to accomplish this with the following code: *ngFor='let item of postList [routerLink]="['/detailed-post&ap ...

applying a margin to the cover div

I am currently working on a #cover element with the following CSS styling: #cover { background-color: #FFFFFF; height: 100%; opacity: 0.4; position: fixed; width: 100%; z-index: 9000; } The goal is to have it cover the entire visi ...

Understanding the significance of the argument in the context of `express.json({ extended: false})`

Currently, I am in the process of setting up an API using express and encountered this particular line of code: app.use(express.json( { extended: false } )); Although I have referred to the documentation provided by express, I was unable to locate this sp ...

What is the best way to extract specific values from one JSON object and transfer them to another using lodash?

//I have a pair of objects var obj1={ "name":"mayur", "age":23 } var obj2={ "name":"keyur", "age":29, "limit":54, "surname":"godhani" } //I am familiar with one approach var j1 = {name: 'Varun', age: 24}; var j2 = {code: &ap ...

Encountering issues with bidirectional data binding functionality

I have integrated a pagination component from ng-bootstrap into a generic component that includes a select dropdown to choose the number of items per page. I triggered an event from this generic component and caught it in the parent component (member-list. ...

Having trouble with Webpack dynamic import not returning files in ReactJS? Here's how to fix it

Struggling with importing and using images in Reactjs? I've imported all images from a folder but it's not returning an array with links. Take a look. function importAll(r) { return r.keys().map(r); } const images = importAll(requi ...

Enable browser caching for form data when using AJAX to submit

I'm currently working on a web application that relies heavily on AJAX to submit form data. However, I've encountered an issue where I want the browser to cache the user's input for auto-completion in future form fillings. While I know I cou ...

Oops! Looks like there's an issue with reading the property 'value' of undefined in React-typeahead

Having some issues with setting the state for user selection in a dropdown menu using the react-bootstrap-typeahead component. Any guidance or resources on this topic would be highly appreciated. Details can be found here. The function handleAddTask is su ...

A guide on how to efficiently update and submit a reactive form with a single click of the submit button in Angular

Currently, I have a view component setup where clicking the edit button directs me to the register component for form updates using patchvalue. The issue that I am facing is that when I update and register the form using the same button, it creates anothe ...

Obtain a collection of the corresponding keys for a given value from dictionaries

I'm implementing a function that retrieves a list of keys associated with a specific value in the dictionary. Although I am able to print out the first key successfully, I'm facing difficulties in displaying subsequent keys. I understand that I ...

What is the procedure for placing an item into a vacant area in react-dnd?

Looking to create a drag and drop list using react-dnd. Manage to put together an example: visit codesandbox example here Currently facing one issue: Unable to drop an item into an empty section. If trying to move image1 to the first or third group, un ...

CSS background image not displaying in ReactJS with SCSS

Currently, I am working on a React project and I am trying to incorporate a local image from a public folder as the background for a div container with transparent color. However, I am facing an issue where the image is not being displayed. I have success ...

Page Not Found: React and Express Route Not Found

I encountered an error while attempting to use the sign-up route: POST http://localhost:3001/sign-up 404 (Not Found). I searched for similar issues on StackOverflow but couldn't pinpoint the source of my problem... In the frontend, I am fetching the ...

After reloading the page, Nuxt dynamic routes are displaying a 404 error

Hey there! I'm currently diving into a project that involves using nuxt js, and it's all new to me. I've set it up in spa mode without any modifications in the nuxt config file, just sticking with the default settings. Here's how I&apos ...