Make cards disappear with a sleek scrolling effect in Angular (I'm open to library recommendations)

I want the cards to appear on the screen as I scroll. My knowledge of angular animation is limited, but I have used it for now.

html:

<div class="row" *ngIf="posts">
  <div id="feed-{{post.id}}" (click)="getFeed(post)" *ngFor="let post of posts | conteudoFilter:nivel; " [@scrollAnimation]="state" 
  class="feed card col s12" >
  <img src="{{post.thumb}}" alt="" class="responsive-img">
  <!-- <div class="">{{post.descricao | titlecase}}</div> -->
</div>
</div>

<app-video *ngIf="currentVideo" [currentVideo]="currentVideo"></app-video>
<app-imgdisplay ></app-imgdisplay>

ts

export class FeedComponent implements OnInit {
  @ViewChild(VideoComponent) videoComp:VideoComponent;
  @ViewChild(ImgdisplayComponent) imgDisplay:ImgdisplayComponent;

  state = 'hide'

 public posts:Conteudo[];
 public nivel:{};
 public currentVideo;
 public currentImg: string;

  constructor(public auth:AuthService, public database:DatabaseService<Conteudo>, public router:Router, public el: ElementRef ) { 
  }

  ngOnInit() {
    console.log("ok");
    if(this.auth.User.nivel){
      this.nivel = {nivel:this.auth.User.nivel};
      this.database.listValues("CONTEUDO").subscribe(result => this.posts = result);
    }
  }

  @HostListener('window:scroll', ['$event'])
  checkScroll() {
    const componentPosition = this.el.nativeElement.offsetTop
    const scrollPosition = window.pageYOffset

    if (scrollPosition >= componentPosition) {
      this.state = 'show'
    } else {
      this.state = 'hide'
    }

  }

The applied transition is not functioning as desired; when scrolling the page, all cards appear simultaneously. However, I would like the effect to be applied to each card individually as it comes into view on the screen.

I suspect the issue lies with my code. I have attempted to switch to another div without success. If anyone would like to suggest a library or provide an example for better animation management, please feel free to do so.

Answer №1

There are a couple of options to achieve this unique effect. First, add the following to the component:

@Component({
  ..
  animations: [rowsAnimation],
})

Next, create a fade-in animation that moves from left to right.

import { trigger, sequence, animate, transition, style } from '@angular/animations';

export const rowsAnimation = trigger('rowsAnimation', [
    transition('void => *', [
      style({ 'height': '*', 'opacity': '0', 'transform': 'translateX(-550px)', 'box-shadow': 'none' }),
      sequence([
        animate('.35s ease', style({
          'height': '*',
          'opacity': '.2',
          'transform': 'translateX(0)',
          'box-shadow': 'none',
        })),
        animate('.35s ease', style({
          height: '*',
          opacity: 1,
          transform: 'translateX(0)',
        })),
      ]),
    ])
]);

Then, add the animation to the HTML.

<div class="row"
     *ngIf="posts">
    <ng-container *ngFor="let post of posts | conteudoFilter:nivel; ">
        <div id="feed-{{post.id}}"
             (click)="getFeed(post)"
             [@rowsAnimation]=""
             class="feed card col s12">
            <img src="{{post.thumb}}" alt="" class="responsive-img">
            <!-- <div class="">{{post.descricao | titlecase}}</div> -->
        </div>
    </ng-container>
</div>

The animation is set to void => *, which means it will appear when a new row is added to the DOM. The final step is to add these rows one by one. Below is a simple example to test if the animation works:

addRow() {
  this.posts.push({id: 1});
}

The animation should appear. Then you can trigger it in the scroll.

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 transparent sticky navigation bar isn't functioning as intended

I stumbled upon a code for the sticky navbar that I found on this website. Intrigued, I decided to copy the code and test it out on my Laravel blade file. The output turned out like this: https://i.sstatic.net/lexRl.jpg Here is how I copied the code: CS ...

Links are functional in browsers, but seem to be unresponsive on iPad devices

I'm currently developing a website that allows users to navigate using swipe gestures. Each slide contains multiple links that should redirect users to different locations upon clicking. However, I've encountered a bizarre issue where the links a ...

Invoke a function and assign it to an export variable

I have a query regarding a file containing an export constant that is utilized to construct a navigation bar in CoreUI. However, I am exploring methods to generate dynamic JSON data within other Components or the same file and then inject it into the exp ...

On smaller screens in portrait mode, CSS automatically incorporates margin adjustments

Here is the code I am working with: <html> <head> <style> body { margin: auto; width: 720px; } </style> </head> <body> <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ip ...

What is the best way to place text side by side with an image?

How can I align text below a picture like this? https://i.stack.imgur.com/WcNRa.jpg Here is the code snippet: https://jsfiddle.net/vzjj9eLy/ HTML: <span class="test"> <img src="http://cdn.sstatic.net/Sites/stackoverflow/img/<a href="/cd ...

What could be causing my buttons to malfunction once I've applied padding to the container?

I'm struggling with getting my buttons to appear active. They currently don't have any functionality and lack a hover effect when clicked. Despite my efforts to add effects, nothing seems to change. After experimenting with the CSS, I've no ...

The anticipated outcomes are not achieved when utilizing environmental variables in Angular 2

Is it expected that when we use ng serve --env=prod, it should work with the values set in environment.prod.ts? Well, in my experience, it doesn't seem to be working as expected as I always receive the values from environment.ts (which is the developm ...

What is the correct way to access and assign a value from a different getter or setter? I am facing an issue with the creation of my second array

Two http GET API calls are being made in the constructor. The first call is working fine and has a getter/setter to filter the main array (studentNameData) into a filtered array (filteredName). However, the second call is also trying to do the same thing b ...

Styling elements using the nth-child selector in JavaScript

I am trying to apply a CSS class based on the combined height of the 2nd and 3rd child elements. For example, if the height of the 2nd child plus the height of the 3rd child equals a certain value, I want to add a specific class. Here is my JavaScript cod ...

Donut and Bar Graph by Highchart

My goal is to create a chart similar to the one shown in the Example Image linked below. I have provided two separate plunkers for each type of chart. What I am aiming for: Example Image Donut Chart: ===> Refer to Comment section for Plunkr link // ...

How can I remove the arrow from a CSS selector?

I'm looking to enhance the appearance of a select element on my webpage, so I've crafted some custom CSS: .selectWebDropDown { background-color:Transparent; background: url(../Images/ddl_Normal.png) no-repeat right #FFFFFF !important; ...

Unable to modify the appearance of text on the canvas

Trying to customize the font style of canvas text with Press Start 2P The URL has been imported into a CSS file as follows: @import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap'); .canvasClass{ font-family: ...

Is the confirmation window not showing up on the active tab in Chrome Extension? It only seems to appear on the popup.html tab

I have created a Chrome extension for an e-gym that is designed to prompt a confirm window after every hour of using the Chrome browser. The issue I am facing is that the confirm window only appears on popup.html. I want to activate my extension so that it ...

The latest update of Google Maps Javascript API Version 3.54 is causing compatibility issues with Angular Google Maps 13.2

Since Nov 16, 2023, a concerning issue with Google Maps has emerged in my Angular Application. The problem involves receiving 403 Errors from Google Maps and the maps failing to load (resulting in a blank page), despite having a valid API Key. The followi ...

Utilizing Nativescript Angular, personalize the styling by separating the SCSS file into two distinct platform-specific SCSS files labeled as .android.scss and .ios

After modifying my component to convert it into a nativescript angular platform specific SCSS, everything seems to be working fine. The button's background turns yellow on the android platform/simulator and green on IOS, as specified in the SCSS files ...

You can tab through form input boxes on an Adobe Muse website, but they cannot be clicked

Lately, I created a small website using Adobe Muse and incorporated their form widget. However, I haven't altered any of the default settings and now the input boxes are not clickable. The only way to interact with them is by tabbing through. How can ...

Issue with MEAN app: unable to retrieve response from GET request

FETCH The fetch request is being made but no data is returned. There are no errors, just the fetch request repeating itself. app.get('/:shortUrl',async (req,res)=>{ try{ const shortUrl = await shorturl.findOne({ short: req.params.sh ...

What techniques can I use to generate a 3D visual effect by shifting the background image layer as the user scrolls (creating a Parallax effect

I have combined two images in the heading; one on top of the other. My goal is to create a subtle vertical movement in the background image as the user scrolls, giving the illusion of depth. Both images share the same width, but the background image is tal ...

Adjusting my navbar to dynamically resize with the changing window dimensions

Seeking guidance on creating a responsive navbar. Currently, I've successfully adjusted the bar's size based on monitor resolution, but the buttons remain static in size. I experimented with using %, vm, and vh, but suspect that there might be a ...

Using React Native to showcase a background grid of dimensions {height: 10, width: 10}

Hey there! I'm looking to create a grid that can adapt to different device sizes and screen positions, similar to a chess grid. I want to use this as a background to help with sizing and positioning elements like text and images on the screen. Here&a ...