Is it feasible for a series of Divs to be arranged in a row, each with varying starting positions and widths that seamlessly fill up to their

Seeking a method to incorporate divs into a container with the ability to specify the left starting position in pixels for each one, while also ensuring that each div automatically stretches to its neighbor sibling's left. I have an array of start positions for each div and could potentially calculate the width for each div in JavaScript. However, I am hoping for a more refined solution.

I have extensively searched for a resolution and thus far, the most viable option I have come across is the flex solution which involves computing the widths:

.s-container {
  height: 20px;
  background-color: red;
  min-width: 100%;
  display: flex;
}

.s-child {
  border-left: 1px solid black;
  background-color: yellow;
  height: 100%;
}
<div class="s-container">
  <div style="flex-basis:50px;" class="s-child">left: 0</div>
  <div style="flex-basis:100px;" class="s-child">left: 50px</div>
  <div style="flex-grow:1;" class="s-child">left: 150px</div>
</div>

Is there a way to assign the left position for each div and ensure that each one fills its own width up to the neighboring element on the right?

Answer №1

Although it may be a bit late, I came up with a solution that involves checking the next sibling's starting position to determine the width of the current sibling while iterating through them. Initially, I was hesitant to go down this route, but it ended up working quite nicely.

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

export class Slide  {
  px: number;    
}

@Component({
  selector: 'app-root',
  template: `
  <div class="s-container">
    <ng-container *ngFor="let s of slides; let i = index; let last = last">
      <div *ngIf="!last" [ngStyle]="{'flex-basis': getSlidePx(i + 1) - s.px + 'px'}" class="s-child">{{s.px}}</div>
      <div *ngIf="last" style="flex-grow:1;" class="s-child">{{s.px}}</div>
    </ng-container>
  </div>

  <form [formGroup]="newSlideForm" (ngSubmit)="onInsert()">

    <div class="form-group">
      <label>Pixels</label>
      <input type="number" class="form-control" required formControlName="Pixels" (change)="newSlide.px = $event.target.value" name="pixels">
    </div>

    <input type="submit" value="Insert" [disabled]="!newSlideForm.valid" />
  </form>
  `,
  styles: [`
    .s-container {
      height: 20px;
      background-color: red;
      min-width: 100%;
      display: flex;
    }

    .s-child {
      border-left: 1px solid black;
      background-color: yellow;
      height: 100%;
    }
  `]
})
export class AppComponent implements OnInit {
  newSlide: Slide;
  newSlideForm: FormGroup;
  slides: Array<Slide>;

  constructor(private _fb: FormBuilder){
    this.newSlide = new Slide();
    this.newSlideForm = this._fb.group({
      Pixels: [0, Validators.required]
    });
  }

  getSlidePx(n: number){
    if(n < this.slides.length)
      return this.slides[n].px;
  }

  onInsert(){
    this.slides.push(this.newSlide);
    this.slides.sort((a, b) => a.px - b.px);
    this.newSlide = new Slide();
  }

  ngOnInit(){
    let temp = [];

    let s1 = new Slide();
    s1.px = 0;
    temp.push(s1);

    let s2 = new Slide();
    s2.px = 100;
    temp.push(s2);

    let s3 = new Slide();
    s3.px = 210;
    temp.push(s3);

    let s4 = new Slide();
    s4.px = 300;
    temp.push(s4);

    let s5 = new Slide();
    s5.px = 499;
    temp.push(s5);

    let s6 = new Slide();
    s6.px = 650;
    temp.push(s6);

    this.slides = temp;
  }
}

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

Nested array in an object within ag-grid

Hello everyone, I am currently working with an array as one of the column data and I am looking for ways to display it using ag-grid. Can anyone advise me on what my options are? columnDefs = [ { field: 'freLoanId' , checkboxSelection: true, ...

Display the first item last in an *ngFor loop in Nativescript Angular

I'm facing some confusion with the sorting of an array in JavaScript. Although the index, last, and first seem to be correct, the result is not as expected. Versions @angular/core: 4.1.0 nativescript-angular: 3.1.3 typescript: 2.4.0 Expected 1234 ...

Tips for Connecting Jquery Ajax to the Parent Element

With the generous help of everyone, I was able to resolve my issue. By changing 'context:' from 'this.parentNode' to simply 'this', I managed to correct the problem I was facing with running multiple instances. Despite still f ...

What is the best way to update the value of a PHP variable using AJAX or jQuery?

I've developed a small PHP application and I'm attempting to incorporate AJAX functionality. I have two lists: <select name="author" id="author"> <option value='-1'>No author</option> ...

Issue with Charts.js not rendering properly during page load

My Charts.js line chart is experiencing a strange issue that I can't seem to resolve. When the page first loads, the <canvas> element appears empty: https://i.sstatic.net/HEj2P.png However, if I resize the browser window, the chart suddenly ap ...

Attempting to create a button cluster using Bootstrap and AngularJS for the purpose of navigating to various URLs

Looking to create a pair of buttons using bootstrap and angularjs that will redirect to different URLs when clicked. Here is the relevant code snippet: app.controller('LinkController',function(link, $scope, $location){ $scope.go = function() ...

There is a noticeable absence of top padding

Currently in the process of designing a custom WP theme, I have encountered an issue with styling the header links as seen in the image. Despite my attempts to fill the bluish gradient area with a black background using the .current class, the desired effe ...

Ways to eliminate unnecessary items from a JavaScript object array and generate a fresh array

My JavaScript object array contains the following attributes: [ { active: true conditionText: "Really try not to die. We cannot afford to lose people" conditionType: "CONDITION" id: 12 identifier: "A1" ...

Code update results in a blank screen on Angular-NativeScript application

Currently diving into the world of NativeScript, I encountered a curious issue. Installing one of the sample apps was a breeze, with the emulators functioning perfectly post-installation. However, as soon as I make even the slightest code tweaks and save t ...

Show a brief description alongside multiple post thumbnails

I have successfully uploaded multiple featured images to my WordPress website using the multiple post thumbnail plugin. Now, I want to showcase them all below the content along with their descriptions. While I have managed to display the main featured imag ...

Is it possible to utilize the base-tag in order to transmit parameters?

I have implemented a custom debugging feature within a large framework where by adding ?debug to any URL, I am able to access specific server data. However, the issue arises when clicking on a link as the ?debug parameter disappears. Is there a way to reta ...

Facing a 'No provider for' error in my Angular 2.0.0 application

I recently developed a service called SecurityService to handle authentication. Check out the code for this service below: import { Injectable } from '@angular/core'; @Injectable() export class SecurityService { items: any[]; construct ...

Crafting personalized objects from an array

In the process of creating an object from an array, I am faced with a dilemma. The elements in the array are as follows: var arr = [ 'find({ qty: { $lt: 20 } } )', 'limit(5)', 'skip(0)' ] Despite my efforts, my code is ...

Replace the <button> tag with an <input> tag that triggers an onclick event

I am struggling to swap one element for another using an "onclick()" function. I can create new elements, but that doesn't solve my problem. Imagine I have a element that I want to replace with a different element on the same spot. I am familiar w ...

Converting an ajax request from jQuery to pure JavaScript

Is there a way to convert this jQuery Ajax to plain Javascript? $("#newsLetter-form").on('submit',function(event){ event.preventDefault(); email = $('#emailId').val(); console.log(email); $.aj ...

Utilize Firestore's limit feature with variables

Is it possible to utilize a variable (page) in the limit parameter within Firebase Firestore while using Vue? export default { data() { return { page: 1, } }, methods: { }, firestore: { Words: db.collection("Words").w ...

What is the best way to set conditions for document side script?

I'm struggling to disable the horizontal scroll when the viewport width is 480px or less. The script that controls the scroll behavior on my website looks like this: <script> $(function () { $("#wrapper").wrapInner("< ...

I am experiencing a peculiar issue where a string is properly displayed in HTML, but appears as "undefined"

I'm currently facing an issue where I can successfully retrieve a string and display it on an HTML page, but I am unable to use it as a string in my TypeScript code. Below are the snippets of my code: TypeScript: user: User = new User(); construct ...

Troubles with styling RadioButtons in jQuery Mobile

Currently, I am attempting to utilize an AJAX call to generate a radio button list. However, I am encountering formatting issues resulting in my list appearing like this: (Radio-button) Text This is the snippet of code that I am using: $.get( "sched ...

The property 'matCellDefTrackBy' cannot be bound to 'mat-cell' as it is not recognized as a valid property

Wondering how to implement trackBy in Angular Material? I encountered the error message below: Can't bind to 'matCellDefTrackBy' since it isn't a known property of 'mat-cell' html: <mat-table [dataSource]="data" ...