The constricted styles are causing the whole page to bounce (scroll) up and down

On my SPA frontend, I have a parent div with a height of 580 containing 9 smaller divs (about 190px in height each). The parent div has an overflow set to hidden so that only 3 elements are visible at one time. Every 5 seconds, I change the styles by adding a negative margin-top, creating a scrolling effect where new elements appear from the bottom as others move up.

<div class="most__popular"
     v-for="n, i in articles" :key="n.id"
     :style="{margin: sliderMargin[i]}">

-

data() {
    return {
        articles: [],
        errors: [],
        step: 0
    }
},
mounted() {
    setInterval(() => {
        this.step = (this.step + 1) % 3;
    }, 5000);
},
computed: {
    sliderMargin() {
        const thresholds = [0, 3, 6];

        return this.articles.map((_, i) =>
            `${(i > thresholds[this.step]) ? '10px' : '-190px'} 0 10px 0`
        );
    }
}

The articles data is currently hardcoded JSON with 9 records.

While this setup works well, I noticed that when I scroll the page slightly to view only the last element of the 580px tall parent div and it transitions from the third step back to the first, my entire page scrolls down unexpectedly.

I want to prevent this behavior from affecting the rest of the content on the page. How can I fix this issue?


EDIT

I've included a repository demo for you to test locally:

To run the demo, please make sure you have GIT and NodeJS installed:

  • Clone or download the repository using the command:
    git clone https://<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="67030817022408030227050e130512040c021349081500">[email protected]</a>/dopeCode/scrolling-issue.git
  • Navigate to the downloaded project directory in your terminal using: cd path/where/you've/cloned
  • Run npm install in the terminal
  • Launch the demo by running: npm run dev
  • Visit localhost:8080 in your browser

Scroll the page slightly to display only the last visible element of the 3 shown.

Answer №1

Instead of sliding pictures by changing the margin-top, I recommend using relative positioning for better behavior. Margins can be unpredictable and may not always work as expected.

I have made changes to your code to use relative positioning, and now it works perfectly. Please update your scrollElements.vue file with the following adjustments:

Include sliderTop in the computed section.


        sliderTop() {
            const scrollStep = 3;
            const itemHeight = 194;

            return this.articles.map((_, i) =>
                `${(i < this.step*scrollStep) ? (-i*itemHeight) : (-this.step*scrollStep*itemHeight)}px`
            );
        }

Apply sliderTop to style the section containing most__popular elements


             :style="{top: sliderTop[i]}">

Make the following CSS fixes:


.most__popular {
    margin: 10px 0;
    transition: 1s top; /* Only top transition is needed */
    position: relative; /* Added relative positioning */
}
.most__popular__container {
    overflow: hidden;
    height: 572px; /* Adjusted height */
}

Answer №2

It seems like the issue may be related to your CSS styling. I noticed that there is no mention of CSS in your post.

One possible solution could be adding overflow:hidden to your container. Another suggestion is inserting a zero-height div at the bottom of your website before performing an operation, and then removing it after. This can help prevent page scrolling during the operation.

Additionally, consider using CSS for sliders instead of relying on JavaScript. You can create slides with transitions and animations. Have you also considered designing a mobile-friendly website (Responsive Web Design)?

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

AngularJS's hidden input field is not being properly updated

I am currently using angularjs v1.0.7 and facing an issue with a hidden form field whose value is related to another input value. In the example provided at http://jsfiddle.net/4ANaK/, the hidden field does not update as I type in the text input field. &l ...

The development of the React app will pause momentarily upon encountering a single low-severity vulnerability. To address this, you can either run `npm audit fix` to resolve it, or use `npm audit` to gain

A challenge arises while executing the command below in Visual Studio Code to develop a react app: create-react-app my-first-react-app The process halts and displays this message: found 1 low severity vulnerability run `npm audit fix` to rectify th ...

Manipulate the visibility of a child element's dom-if based on a property retrieved from the parent element

Exploring the world of Polymer, I am eager to tackle the following scenario... I have a binding variable called {{readonly}} that sends data from a parent Dom-HTML to a child Dom-HTML in my Polymer project. The code excerpt looks something like this... C ...

Unable to locate the "fcm-node" module in Node.js with TypeScript

When working on a TypeScript project, I usually rely on the fcm-node package to send Firebase push notifications in Node.js. However, this time around, I faced an issue. I know that for TypeScript projects, we also need to install type definitions (@types ...

Is it possible to use -webkit-transform with dual background layers?

I've implemented a unique animation for a div that features two semi-transparent backgrounds: /* Custom keyframes for the animated background DIV. */ @-webkit-keyframes wind { 0% { background-position: 0px 0px, 0px 0px; } 100 ...

Creating a responsive website that utilizes YAML and is optimized for extremely narrow screen widths

I have noticed that my responsive design using YAML is working well for the most part. However, on screens with very small widths, the YAML grids become extremely tiny. In such cases, it would be more practical to have the right grid displayed below the le ...

Ways to style CSS for inactive checkboxes?

How can I easily change the CSS for disabled checkboxes in a grid to make them more visible for users? Technologies I prefer to use, ranked from most preferred to least: CSS JavaScript jQuery Other ...

Upon receiving data from the Api, the data cannot be assigned to the appropriate datatype within the Angular Object

I am encountering an issue with the normal input fields on my page: https://i.stack.imgur.com/qigTr.png Whenever I click on the "+" button, it triggers an action which in turn calls a service class with simple JSON data. My intention is to set selectionC ...

aws-lambda Module Not Found

I am encountering an issue in the aws-lambda console every time I try to upload code from a zip file. Oddly, other zip files seem to work fine. The .js file within the problematic zip is named "CreateThumbnail.js" and I have confirmed that the handler is ...

Searching for a specific value within a list that has been fetched from a database using AngularJs can be achieved by simply clicking on the search button

Seeking assistance on a search functionality issue. I am able to filter search results from a list retrieved from the database and displayed on an HTML page, but facing difficulty in getting complete search results from the controller. When clicking the se ...

The integration of socket.io with static file routing in node.js is encountering issues

I am currently developing a chat application and I have encountered an issue. When the static file routing is functioning correctly, the socket.io for the chat feature throws a "not found" error in the console. http://localhost/socket.io/?EIO=3&tran ...

What are the specific constraints for CSS within web components?

<html> <style> body { color: blue; } </style> <body> <h1>Styles!</h1> <p>someone created a very general selector</p> <isolated-stuff></isolated-stuff> </body> <s ...

My WordPress loadmore function is not providing any additional posts

Hey everyone, I'm facing a Wordpress issue that I can't seem to resolve. I've checked other posts but haven't found the solution yet. So, I decided to share my code here and seek help. My problem is with trying to load more posts using ...

The issue of height and width always being zero in Chrome when using Classic ASP with JavaScript

Within my markup, I have an image field that I am setting the source for using JavaScript. I am in need of its height and width, so I utilized the image.height() and image.width() methods. Despite working properly in Internet Explorer, these methods do not ...

Sending a notification alert directly to the client's web browser

My goal is to develop an application that allows Super users to notify other users by providing access to content, such as a PDF file, when they click on a link. For example, in the scenario where a teacher wants to share a PDF with students, this applica ...

Issue [ERR_MODULE_NOT_FOUND]: The module 'buildapp' could not be located within buildserver.js

I am currently working on a node project using typescript. The project's structure is organized in the following way: --src |-- server.ts |-- app.ts --build |-- server.js |-- app.js In server.ts: import { app } from &q ...

Using JavaScript, generate ten clickable circles on the screen. Each circle should display the number of times it has been clicked in the center when interacted with

I am currently working on my JavaScript skills and would like to implement a fun project involving 10 colorful circles. While I can easily create these circles using HTML and CSS, I require assistance with the interactive functionality using JavaScript. ...

What is the best way to display a responsive website design exclusively for certain devices?

My website currently lacks responsiveness. Typically, we can check if a website is responsive by resizing the browser to see if it adjusts using CSS3 media queries. However, I am looking to create a better user experience by ensuring that visitors using a ...

Empty results received from MongoDB queries executed within an asynchronous loop

I'm encountering an issue where I have a list of IDs that I need to query Mongo with in a for loop, but it always returns an empty []. Initially, the queries were returning promises, so I switched from using forEach to a standard for loop as shown her ...

Transfer the values selected from checkboxes into an HTML input field

I am attempting to capture the values of checkboxes and then insert them into an input field once they have been checked. Using JavaScript, I have managed to show these values in a <span> tag successfully. However, when trying to do the same within a ...