Correctly align the div on the screen as the viewport is scrolled

I am currently working on a parallax website where the sliders are designed to slide from the left and align within the viewport as the user scrolls. However, I have encountered an issue where multiple scroll actions are required for the slide to align properly. Is there a way to adjust this so that the alignment occurs with just one scroll?

Here is a sneak peek at my work in progress. I am utilizing skrollr for my parallax designs ()

HTML

<section  class="slide slide_1">
    <div class="slide_content"  >
        <h2>You see a blank canvas,<br/> we see potential</h2>
        <img src="<?php bloginfo('template_url'); ?>/images/canvas.png" alt="Canvas" />
    </div>
 </section>

<section data-0="transform:translateX(100%); " data-1500="transform:translateX(0%) " class="slide slide_2">
    <div class="slide_content"  >
        <h2>You see a brush,<br/> we see a dream</h2>
        <img src="<?php bloginfo('template_url'); ?>/images/brush.png" alt="brush" />
    </div>
</section>

<section data-1500="transform:translateX(100%); " data-2500="transform:translateX(0%)" class="slide slide_3">
    <div class="slide_content"  >
        <h2>You see a ball of clay,<br/> we see a world of creativity</h2>
        <img src="<?php bloginfo('template_url'); ?>/images/clay.png" alt="clay" />
    </div>
</section>

<section data-2500="transform:translateX(100%); " data-3500="transform:translateX(0%)" class="slide slide_4">
    <div class="slide_content">
        <h1>Every child is a creative kid.</h1>
        <img src="<?php bloginfo('template_url'); ?>/images/kid.png" alt="kid" />
    </div>
</section>

CSS

.slide{width:100%; height:100%; position:fixed}


.slide_1{background-image:url('images/patternfinal1.jpg'); z-index:1}
.slide_2{background-image:url('images/patternfinal2.jpg');  z-index:2}
.slide_3{background-image:url('images/patternfinal3.jpg');  z-index:3}
.slide_4{background-image:url('images/patternfinal4.jpg'); z-index:4}
.creative_content{ z-index: 10; position: relative; background-color: white; padding:20px 0; top:5%}

.slide_content{
    text-align:center; 
    height:100%; 
    position:absolute; 
    top:15%; 
    margin:0 auto;
    left:0; 
    right:0; 
    z-index:1; 
    font-family: 'anarchisticno_leaders..', sans-serif; 
    font-size:70px;
    color:#333
}

.slide_1 img, 
.slide_2 ing, 
.slide_3 img, 
.slide_4 img{display:block; margin:0 auto}

Despite implementing some javascript, there seems to be an issue where the scrolling halts midway.

JAVASCRIPT

<script>
    var tempScrollTop = 0;
    var currentScrollTop = 0;
    var scrollHeight = $(window).height();
    var newHeight = 0;


    function scrollIt() {

    $(window).off('scroll', scrollIt);

    currentScrollTop = $(window).scrollTop();


    if (tempScrollTop < currentScrollTop) {
        newHeight = newHeight + scrollHeight;
        $('html').animate({scrollTop: newHeight}, 500, function(){
            var setScroll = setTimeout(function(){
            console.log('Animation Complete');
            tempScrollTop = $(window).scrollTop();
            $(window).on('scroll', scrollIt);
            }, 10);
        }); 

    } else if (tempScrollTop > currentScrollTop){
       newHeight = newHeight - scrollHeight;
       $('html').animate({scrollTop: newHeight}, 500, function(){
            var setScroll = setTimeout(function(){
            console.log('Animation Complete');
            tempScrollTop = $(window).scrollTop();
            $(window).on('scroll', scrollIt);
            }, 10);
        }); 
    }


}

$(window).on('scroll', scrollIt);
</script>

Answer №1

Are you looking to speed up the transformation or keep them on screen longer as you scroll?

Option 1: increase transformation speed:

Your current skrollr data attributes are set to move your slide from 100% to 0% over 1000px. Here is what you have:

<section data-1500="transform:translateX(100%); " data-2500="transform:translateX(0%)">

If you want it to transform faster, adjust the numbers to match the desired speed. Consider this example:

<section data-1500="transform:translateX(100%); " data-1600="transform:translateX(0%)">

Option 2: extend time on screen:

To make the elements stay on screen longer, simply increase the gap between the end of one slide's transformation and the start of the next one. Your current setup looks like this:

<section data-1500="transform:translateX(100%); " data-2500="transform:translateX(0%)">
</section>
<section data-2500="transform:translateX(100%); " data-3500="transform:translateX(0%)">
</section>

If you want them to linger on screen more, consider trying this approach:

<section data-1500="transform:translateX(100%); " data-2500="transform:translateX(0%)">
</section>
<section data-3500="transform:translateX(100%); " data-4500="transform:translateX(0%)">
</section>

I hope this clarifies things!

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 could be causing the element.style.FontSize to not be effective on classes that have been looped through and stored in an array created with querySelectorAll beforehand?

Greetings Stackoverflow Community, Today, I'm facing an issue related to JavaScript and WordPress. I have a JavaScript script named setDynamicFontHeight.js, as well as PHP documents named header.php and navbar_mobile.php, which simply executes wp_nav_ ...

The URL in a request is altered prior to execution

I am encountering an issue with my NodeJS application where it automatically appends my domain to the URL set in my http request. How can I prevent this from happening? I have tried to search for similar problems but have not found any relevant solutions. ...

The webmethod does not respond to the AJAX function

I am facing an issue with two separate ajax functions on my webpage. One function works perfectly when a link inside a grid is clicked, while the other one produces an error every time a button is clicked. I need help in identifying where I am making a mi ...

Tips on obtaining checkbox values other than "true"

Having trouble retrieving the values of selected checkboxes instead of displaying "Custom Category"? I've attempted to access the values and attributes with no success. I'm aiming to display the values of the selected checkbox. app.component.ht ...

What is the process by which the browser displays pseudo-element selectors?

One thing that's been on my mind is the potential resource cost of using *:after. Would the pseudo element be the primary selector, or would all elements still be processed by the client? In simpler terms, what impact does *:after have compared to ju ...

Using percentages for sizing and margins in CSS

Looking to create a visually appealing page that always fills the entire screen? Check out this code snippet that does just that: #posts{ width:100%; height:auto; background:#666; } .entry{ float:left; margin-left: 4%; margin-top:10 ...

When viewing HTML "Div" on smaller screens, the full height may not be displayed properly

My setup includes two monitors: one is 24" and the other is my laptop's 12.5" screen. I have a red box (div) in my web application that displays full height on the larger monitor, but only partially on the smaller one. I'm puzzled as to why it s ...

Exploring Javascript parameters with the power of jquery

Is it possible to pass a parameter from PHP into a JavaScript function within HTML? I am currently facing an issue where my code crashes when it reaches a certain condition. This is the code snippet causing the problem: $str="<input type='submit&a ...

Adjusting the height of a Jquery Fancybox to allow for scrolling down

I've implemented fancybox to display certain pages on my website, but I'm struggling with setting a fixed height and enabling scrolling for content that exceeds this height within the fancybox. Here are the parameters I have used: <script> ...

Tips for creating an auto-incrementing ID within Firebase's real-time database

How can I create an automatic incrementing ID for entries in a Firebase database? The first item should have an ID of 1, and the second one should be 2. var database = firebase.database(); var userDetails = database.ref("Article"); userDetails. ...

Using JQuery and Ajax, what is the best way to transfer information from a popup form to a PHP page?

I've noticed that my question has been asked a few times already, but I'm still struggling to get my code working. It seems like I'm using the same code as those solutions, so I can't figure out what's going wrong. Currently, I am ...

Is there a way for me to effectively utilize the data from a successful

This is a form validation checker. I'm looking to incorporate an ajax success message feature. After all validations are complete, I would like to display a success message such as "You have successfully signed up". jQuery(function ($) { $(' ...

jQuery click event not working post Ajax request returned a 403 error

I am facing an issue with my ajax request where I receive a 403 error message. Strangely, when this error occurs, the click() function associated with the ajax call stops working. There is no manipulation of HTML elements before or after the ajax call, and ...

Are NPM and Eslint supposed to be this confusing, or am I missing something?

Recently, I've started delving into the world of JS and have been eager to learn more about linting. Following a tutorial, we set up the lint stage in our package.json file. The configuration looks like this: "lint": "./node_modules/.bin/eslint ." U ...

How can I dynamically adjust an element's attribute as a user scrolls past it (creating a sticky effect)?

For instance, when you visit and try scrolling up, you will notice a bar displaying: "Typography Code Tables Forms Buttons Icons by Glyphicons" As you continue to scroll past the element, a class is added to alter the appearance. However, as you scrol ...

Issue with Firefox browser: Arrow keys do not function properly for input tags in Twitter Bootstrap drop down menu

Requirement I need to include a login form inside the dropdown menu with username and password fields. Everything is working fine, except for one issue: Issue When typing, I am unable to use the arrow keys (up/down) in Firefox. This functionality works ...

Attempting to retrieve AJAX response in JavaScript by utilizing an OOP object

Here is the code snippet I am working with: function CreateDiv(D) { D.body = function () { var d = $.ajax({ type: "GET", url: "Default.aspx", data: 'Ext ...

How should h tags be correctly utilized?

Is it better to use h tags or lists to style content? Take a look at this code, is using h tags the appropriate choice here or should I consider using a list instead? <div class="col-xm-12 col-sm-6 col-md-4"> <h4>Get in touch< ...

What steps should I follow to successfully incorporate Zurb Foundation 4 Sections (tabs) into my Javascript Ajax functionality?

I am currently incorporating Zurb Foundation 4.1.5 into my application and I am faced with the challenge of implementing Zurb Section Javascript to handle "tabs" on the page. The content within these tabs is dynamic and fetched via Ajax calls. I am seeking ...

Incorporate the value of a variable from the .gs file into a personalized HTML sidebar

In my google sheet, I have set up a custom sidebar through scripting. Within this sidebar, I aim to showcase the value from column I in the currently active row. I've successfully created a script that captures and stores the 'I' variable ...