Substitute the division

Can I make the old div change its position and move to the side when I add a new div? And can all the old divs be hidden when adding four new divs? This is for my full news website and I want this applied to all four pages.

First page:

https://i.sstatic.net/yMXeM.png

More pages:

https://i.sstatic.net/84b6I.png

2 pages have the same format and 1 has another format.

$(".js-post").on("mouseover", function() {
  const mainSrc = $(".js-main").attr("src");
  const targetSrc = $(this).attr("src");

  $(".js-main").attr("src", targetSrc);
  $(this).attr("src", mainSrc);
});
.wrapper {
  display: flex;
  width: 100%;
  padding-left: 200px;
  padding-top: 30px;
}

.main-view {
  width: 70px;
  height: 500px;
  margin-right: 30rem;
  object-fit: cover;
}

img.main.js-main {
  width: 500px;
  object-fit: cover;
  height: 400px;
}

thumbnail-view {
  width: 200px;
  padding-top: 20px;
  height: auto;
}

.post {
  width: 100%;
  height: 100px;
}

.box {
  background: white;
  top: 50%;
  left: 50%;
  padding: 10px;
}

box .imgBx {
  width: 150px;
}

* {
  font-family: 'Lato', sans-serif;
}

.hey {
  display: flex;
  width: 70%;
  justify-content: space-around;
  margin-top: 100px;
  margin-left: 180px;
  margin-bottom: 100px;
}

.card {
  width: 280px;
  height: 260px;
  padding: 2rem 1rem;
  background: grey;
  display: flex;
  position: relative;
  align-items: flex-end;
  transition: 0.5s ease-in-out;
  box-shadow: 0px 7px 10px rgba(0, 0, 0, 0.5);
}

card:hover {
  transform: translateY(20px);
}

card:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  width: 100%;
  height: 100%;
  background: linear-gradient(to bottom, rgba(0, 176, 155, 0.5), rgba(150, 201, 61, 1));
  z-index: 2;
  transition: 0.5s all;
  opacity: 0;
}

card:hover:before {
  opacity: 1;
}

card img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  position: absolute;
  top: 0;
  left: 0;
}

card.info {
  position: relative;
  z-index: 3;
  color: black;
  opacity: 0;
  transform: translateY(30px);
  transition: 0.5s all;
}

card.info h1 {
  padding-top: 25px;
  font-size: 30px;
  padding-left: 70px;
  padding-right: 50px;
}

card.info.btn {
  text-decoration: none;
  background: rgb(70, 218, 203);
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  transition: 0.4s ease-in-out;
  margin-top: 20px;
  margin-left: 70px;
}

card:hover.info {
  opacity: 1;
  transform: translateY(0px);
}

footer {
  padding: 50px 0;
  background-color: rgb(70, 218, 203);
  bottom: 0;
  padding: 2px 0px 5px 0px;
  position: relative;
  margin-top: auto;
  clear: both;
}

.on {
  width: 70px;
  white-space: nowrap;
  height: 30px;
  margin-left: 170px;
  margin-top: 60px;
  position: absolute;
}

on p {
  font-weight: bold;
}
<!-- jQuery dependency -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<!-- Body -->
<div class="wrapper">

  <div class="main-view">
    <div>heading</div>
    <img class="main js-main" src="{% static 'images/banner/pic9.jpg' %}" width="300px" height="100" alt="yoga">
  </div>
  <div class="thumbnail-view">
    <div class="box">
      <div class="imgBx">
        <img class="post js-post" src="{% static 'images/banner/pic1.jpg' %}" height="100" alt="handshake">
      </div>
    </div>
    <div class="box">
      <div class="imgBx">
        <img class="post js-post" src="{% static 'images/banner/pic5.jpg' %}" width="300px" height="100" alt="peoplejoined">
      </div>
    </div>
    <div class="box">
      <div class="imgBx">
        <img class="post js-post" src="{% static 'images/banner/pic8.jpg' %}" width="300px" height="100" alt="peoplejoined">
      </div>
    </div>
    <div class="box">
      <div class="imgBx">
        <img class="post js-post" src="{% static 'images/banner/pic7.png' %}" width="300px" height="100" alt="peoplejoined">
      </div>
    </div>
  </div>

</div>
<div class="on">
  <p> Must Read </p>
</div>
<div class="hey">

  <div class="card">
    <img src="{% static 'images/banner/m.PNG' %}">
    <div class="info">
      <h1>US's Miami Fashion </h1>

      <a href="#" class="btn">Read More</a>
    </div>
  </div>
  <div class="card">
    <img src="{% static 'images/banner/c.PNG' %}">
    <div class="info">
      <h1>Muted demand for cotton </h1>

      <a href="#" class="btn">Read More</a>
    </div>
  </div>
  <div class="card">
    <img src="{% static 'images/banner/ss.jpg' %}">
    <div class="info">
      <h1>Srilanka garment export </h1>

      <a href="#" class="btn">Read More</a>
    </div>
  </div>
</div>

Answer №1

It seems like you're looking for a solution in JavaScript to manage a list of items with one main item and four additional ones on the side. One approach could be to place all 5 items within the same parent container, assigning them the same classes. By using CSS (specifically first-child/nth-child) you can style the first item differently to make it stand out.

For adding a new item dynamically, consider utilizing jQuery's .prepend() method (refer to https://api.jquery.com/prepend/) which allows you to insert content at the beginning of the parent container. To control the display of items beyond the fifth, you could either use CSS to hide them or employ jQuery's .remove() function (check https://api.jquery.com/remove/). This will help manage the list effectively based on your requirements.

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

Using React in ES5 to create a button that triggers two separate functions with a

I have encountered an issue with two React classes that both contain a render function returning forms with buttons. In class A, I had to import class B in order to use the form from class B. The problem: Whenever I click the inner button, the outer butto ...

Tips for designing an Ionic app with a Pinterest-inspired layout

Recently stepping into the world of Ionic development, I find myself facing a challenge in creating a Pinterest-inspired layout using Ionic. Specifically, I am struggling to achieve a two-wide list of items with varying heights. Can anyone offer guidance ...

Identify all td inputs within a TR element using jQuery

Is there a way to retrieve all the input values within each table cell (td) of a table row (tr) using jQuery? Suppose I have a tr with multiple td elements, and some of these tds contain inputs or select elements. How can I extract the values from these in ...

Allow Vue to handle the registration of the datepicker event

Is there a way to notify Vue when the datepicker changes the selected date? new Vue({ el: '#app', data: { termin: '' }, computed: { }, methods: { } }) This is just an input field: <div id="app"> <div c ...

What are some effective methods for testing web applications on mobile devices?

While I may not have the funds to purchase mobile phones and iPhones, I am eager to ensure my web applications can be accessed on mobile devices. Are there any software solutions available to simulate these devices, or what steps should I take? ...

Define characteristics of object contained within an array

My task involves handling an array of image objects: var pics = ["pic1","pic2","pic3","pic4","pic5","pic6"] I'm trying to loop through the array and adjust the style.left value by subtracting 10 from the current value. Here is what I attempted: for ...

The newly added radio button does not function as a separate group as expected

I currently have a set of radio buttons: <input type="radio" class='form-control' name="progress_type[]" value="Journal Papers"><span class='radio-label'>Journal Paper</span> <input type="radio" class='form-co ...

Navigating files using NodeJS and ExpressJS

Can NodeJS (or ExpressJS) facilitate the following task? Although I appreciate the flexibility that routing provides, I find the configuration process quite cumbersome. (I don't consider myself an expert in Express) For instance, imagine an applicat ...

Vue and Axios encountered a CORS error stating that the 'Access-Control-Allow-Origin' header is missing on the requested resource

I've encountered the error above while using Axios for a GET request to an external API. Despite consulting the Mozilla documentation, conducting thorough research, and experimenting with different approaches, I haven't made any progress. I&apos ...

Is there a way to grab the inner content of an e-mail link by right-clicking on it?

I am currently developing a Chrome Extension that functions similarly to the "Search on Google" feature when you right-click on selected text. However, I am facing an issue with making it work when right-clicking on a mailto: email link. How can I extract ...

Place an image in the middle of a div

Here are a few images for you to consider: <div class="builder-item-content row"> <div class="twelve columns b0" style="text-align:center;"> <ul class="upcoming-events"> <li> <a href="http://www.padi.com/scub ...

Struggling to receive a response from request.POST.get within the index function of views.py

I am struggling to develop a web application that shows the IP Address of a hostname entered by the user in a text field. Unfortunately, I keep encountering an error where I can't seem to get a response from the URL. As a beginner in this field, I wou ...

Replacing the yellow autofill background

After much effort, I have finally discovered the ultimate method to remove autofill styling across all browsers: $('input').each(function() { var $this = $(this); $this.after($this.clone()).remove(); }); However, executing t ...

Sending messages through a Discord Bot without the use of a command

I have code that is constantly running in the background in JavaScript, 24/7. I am looking to configure a discord.js bot to send a notification message if any issues or errors occur within the code. Is there a way to send a message to any channel without ...

Utilizing JavaScript to retrieve data using AJAX

Having trouble sending emails through ajax on my website. The form is in a modal (using custombox), but all values being sent are null. How can I utilize getElementById in modal content? Link to custombox git's hub: https://github.com/dixso/custombox ...

What location is most ideal for incorporating JavaScript/Ajax within a document?

Sorry for the seemingly simple question, but I would appreciate some clarification from the experts. When it comes to the three options of placing JavaScript - head, $(document).ready, or body, which would be the optimal location for ajax that heavily rel ...

Leveraging ES6 import declarations within Firebase functions

I've been experimenting with using ES6 in Firebase functions by trying to import modules like "import App from './src/App.js'. However, after adding type:"module" to my package.json, I encountered a strange error with Firebase ...

Stop jQuery from resetting the background image when using fadeOut()

Currently, I am using fadeIn and fadeOut functions with jQuery to create a fading effect. However, when the final fade out occurs, the entire CSS table fades out and the background image of the CSS body comes into view. Despite setting the background-posit ...

Using JavaScript, add a complex JSON record to a JSON variable by pushing it

I have been attempting to insert a complex JSON data record into a JSON variable using the following code: var marks=[]; var studentData="student1":[{ "term1":[ {"LifeSkills":[{"obtained":"17","grade":"A","gp":"5"}]}, {"Work":[{"obtained":"13"," ...

"Rest assured that Coldfusion 9 users may encounter a JSON parsing

I have a function that performs the following operation: <cffunction name="foo" access="remote" returnformat="JSON" > <cfreturn ["000", "001", "002"]> <cffunction> When I retrieve this array using jQuery.ajax, I noticed in Firebu ...