Changing the display order of divs in Angular 2 when they are shown or hidden

I have a fixed length div called Content div that contains two other divs - div1 and div2. Both of these divs are larger in size compared to Content div, allowing the user to scroll within the container. The user will initially read the content in div1 and then click on a button within div1 to reveal div2 while hiding div1. However, every time div2 is revealed, it only shows the bottom part of the content. How can I ensure that when div1 is hidden and div2 is shown, the top of div2 is visible inside Content div?

Any assistance on this matter would be greatly appreciated.

HTML CODE-

<div class = "completeBody">
  <div class = "header">
  </div>
  <div class = "content">
    <div class = "content1" *ngIf="showContent1">
        <div>
            top
        </div>
        <div class="bottomPoint">
            bottom
            <button (click)="showContent2()">SHOW Content2</button>
        </div>
    </div>
    <div class = "content2" *ngIf="!showContent1">
        <div>
            top
        </div>
        <div class="bottomPoint">
            SEE NOW YOU ARE IN BOTTOM,I WANT THAT WHEN CONTENT2 SHOULD BECOME VISIBLE,ITS TOP SHOULD COME FIRST
        </div>
    </div>
  </div>
  <div class = "footer">
  </div>
</div>

CSS CODE-

   /* Styles go here */

.completeBody
{
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0;
  bottom: 0;
}
.header
{
  height: 10%;
  width: 100%;
  background-color: red;
}
.content
{
  height: 80%;
  margin: 20px;
  background-color: #efefef;
  border: 2px solid black;
  overflow: auto;
}
.footer
{
  height: 10%;
  width: 100%;
  background-color: green;
}
.content1
{
  height:1000px;
  background-color: blue ;
}
.content2
{
  height:1000px;
  background-color: #909090 ;
}
.bottomPoint
{
    padding-top:950px;
}

TYPESCRIPT CODE-

import {Component} from '@angular/core';

@Component({
    selector: 'app',
    templateUrl: 'app/apphtml.html',
    styleUrls: ['app/appcss.css'],
})
export class App {
  showContent1:boolean;

  constructor() {
    this.showContent1 = true;
  }
  showContent2()
  {
    this.showContent1 = false;
  }

}

HERE IS THE LINK FOR PLUNKER - https://plnkr.co/edit/abjZKMt0CkvJaTgaBDRm?p=preview

Answer №1

To achieve this, we can connect our scrolling container using a ViewChild and access its native attributes to make necessary adjustments.

If you need more information, please refer to the provided Plunker link for the latest updates.

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

Ensure images are correctly aligned

Could really use some help with this issue I'm having. It's probably a simple fix for all you experts out there, but I can't seem to align my images properly so they are even and not one higher than the other. Please take a look at the scree ...

Activate the function when the element becomes visible within the viewport

When working with jQuery, you can use the plugin appear or waypoint to check if an element is visible within the viewport. I recently searched for a similar feature in Angular2 but couldn't find one. If it doesn't exist yet, what would be the mos ...

Enhancing user experience by implementing dynamic text box functionality that triggers ajax load for dropdown options using a combination

Here is the task I need help with, along with the code snippet: I want to enable the author select box when both the start and end dates are filled out. The author names should be fetched dynamically from the database based on the selected start and end ...

Using jQuery to capture click events and retrieve the ID of the clicked element

My jQuery clicking function that I have written is not working as expected. There seems to be an error on line 4. for (var i = 1;i<=3;i++){ $("#Block"+i).click(function(){ $(this).effect("shake", function(){ if (this == "#Block3 ...

Invalid character entered into HTML5 number input field

How can I validate a number field on my own when element.value returns nothing in Chrome if the content is alphabetical? Setting 'novalidate' on the form does not prevent this issue. As a result, I am unable to distinguish between an empty entry ...

Learn how to retrieve a meta tag's content value and perform a subtraction operation

I need assistance in retrieving the value of the meta tag content and performing a subtraction operation on it. Could someone lend me a hand with this, please? The meta tag is structured as follows: <meta itemprop="price" content="1699"> I ha ...

"AngluarFirestore - issues with limit and startAfter functions not functioning properly

Currently, I am in the process of constructing a webpage utilizing AngularFireStore as my chosen data storage solution. To implement infinite scrolling functionality, I have been attempting to utilize the limit and startAfter features provided by AngularFi ...

Which JQuery plugins can transform regular <select> dropdowns into more stylish options?

After an extensive search, I was only able to locate one solution at this link. I scoured the entire internet for alternatives. ...

Formatting numbers in Angular 2 to include a space every three zeros in a money amount

Let's say I have the number 30000 and I want to format it as 30 000. What method should I use to achieve this? Here are more examples: 300000 -> 300 000, 3000000 -> 3 000 000. Just to clarify, this is not about using dots or commas, but rathe ...

Unraveling HTML in TypeScript with Angular 2

After receiving encoded HTML from the back-end, I am struggling to decode it. I have attempted to use decodeURIComponent and decodeURI. The server sent me: "<table style="background-color: white;"> <tbody> <tr> ...

What is the correct way to implement a text field using jQuery?

Having trouble getting a text field to show up on the stage of the pong game with the code below. Looking for some assistance with this, as it's my first attempt at using jquery and javascript. Thanks in advance for your help! <!doctype html& ...

Maintain Angular Dropdown Menu Open Across Page Refresh

I am currently working on an HTML/Typescript view that is connected to a SQL Database. Whenever there are changes made to the database, the entire webpage reloads. The issue we are facing is that we have dropdown menus on the page that clients want to rema ...

Unable to connect to a live Docker container using public DNS on an AWS EC2 instance

I have a requirement to host my Angular Webapp on an AWS EC2 instance. I followed the steps of creating a Dockerfile within my Angular project and deploying it onto the EC2 instance. Gitlab CI is responsible for building the Angular project and generating ...

The element 'mat-toolbar' is unrecognized and not known:

Encountering an issue while executing karma tests - the error message indicates that 'mat-toolbar' is not a recognized element within the application. Here are some steps to troubleshoot this problem: 1. Confirm whether 'mat-toolbar' is ...

How can the date pattern be set for the ngx-bootstrap Daterangepicker?

When using the pattern date for bsDatepicker, everything works perfectly fine. <input type="text" class="form-control" formControlName="birthday" bsDatepicker [bsConfig]="bsConfig" [(bsValue)]="birthday" value="{{dataNascimento | date:'dd/MM/yyy ...

Set the minimum width of CSS headers <h1, h2, h3> to align next to a floated image

I am dealing with a layout issue where headlines should be displayed next to floated images if there is enough space, and drop below the images if not. Unfortunately, I do not have access to the HTML code, so I need to solve this problem purely through CSS ...

The application was not functioning properly due to an issue with the getSelectors() function while utilizing @ngrx/entity to

Currently, I am facing an issue with implementing a NgRx store using @ngrx/entity library. Despite Redux Devtools showing my collection loaded by Effect() as entities properly, I am unable to retrieve any data using @ngrx/entity getSelectors. Thus, it seem ...

changing the css transformation into zoom and positioning

My goal is to incorporate pinch zoom and panning using hardware-accelerated CSS properties like transform: translate3d() scale3d(). After completing the gesture, I reset the transform and switch to CSS zoom along with absolute positioning. This method ensu ...

Learn the position of a div element that appears following the lazy loading of images

I am struggling to make a div sticky below a set of lazy load images. The issue lies in the fact that the offset of the div keeps changing due to the lazy loading images pushing it down. There are 4 images with unknown heights, making it difficult to acc ...

`Incorporating width and vertical alignment in an image`

I am trying to figure out a way to make the image fill up 100% of the width, aligning it vertically with the text below. The code I'm working on is for email newsletters using foundation. Check out my Example Site Unfortunately, I can't seem to ...