Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine which div is in the middle of a div list

I have a div list that looks like this:

<div class="item"></div>
<div class="item"></div>
<div class="item"></div> <!--the middle one-->
<div class="item"></div>
<div class="item"></div>

I need to determine which div is in the middle of the list, please note that the div number is dynamic, depends on user's input. my final goal is to determine which divs are on the left and right side of the "middle div" then apply a class depends on its position.

The final result should look like this:

<div class="item left"></div>
<div class="item left"></div>
<div class="item center"></div> <!--the middle one-->
<div class="item right"></div>
<div class="item right"></div>

I was thinking to add a number identifier for each div and use median to determine the "middle div" but I'm not quite sure.

Perhaps there is a better approach for this problem using javascript, jquery or even pure css?

Update:

Additional information for handling even number:

in case the list has even number of child divs, it should divide it like this

<div class="item left"></div>
<div class="item left"></div>
<div class="item left"></div> 
<div class="item right"></div>
<div class="item right"></div>
<div class="item right"></div>

in my problem, both Rory McCrossan and user3297291 works well. I added some modification to both of it for handling even numbers.

Rory McCrossan's (with JQuery):

    var $items = $('.item');
    var middleIndex = Math.floor($items.length / 2);
    var hasMid = $items.length % 2;
    console.log(middleIndex);

    if(hasMid == 1){
         $items.eq(middleIndex).addClass('middle')
             .prevAll().addClass('left').end()
             .nextAll().addClass('right');
    }

    if(hasMid == 0){
         $items.eq(middleIndex).addClass('right')
             .prevAll().addClass('left').end()
             .nextAll().addClass('right');
    }

user3297291's :

    var setMidClasses = function (elementList, beforeMid, atMid, afterMid) {
    var i = 0,
        hasMid = elementList.length % 2,
        mid = Math.floor(elementList.length / 2);

    while (i < mid) {
        elementList[i].classList.add(beforeMid);
        i += 1;
    }

    if (hasMid == 1) {
        elementList[i].classList.add(atMid);
        i += 1;
    }

    while (i < elementList.length) {
        elementList[i].classList.add(afterMid);
        i += 1;
    }

};



setMidClasses(document.querySelectorAll(".item"),
    "left", "middle", "right");

feel free to edit the code snippets as it might be not very tidy after my edits.

like image 341
Christian Hadikusuma Avatar asked Oct 11 '16 10:10

Christian Hadikusuma


People also ask

How do I get the center of a div?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do I display the contents of a div in the middle?

For vertical alignment, set the parent element's width / height to 100% and add display: table . Then for the child element, change the display to table-cell and add vertical-align: middle . For horizontal centering, you could either add text-align: center to center the text and any other inline children elements.

How do I center a div in another div?

Use the "inline-block" value of the display property to display the inner <div> as an inline element as well as a block. Set the text-align property on the outer <div> element to center the inner one. This property only works on inline elements.

How do you center a div in the middle of the page?

Center Align Elements To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.


2 Answers

In the case of an odd number of items you can get the middle item using Math.floor(items.length / 2). From there you can use prevAll() and nextAll() to add the classes to the relevant elements:

var $items = $('.item');
var middleIndex = Math.floor($items.length / 2);

$items.eq(middleIndex).addClass('center')
  .prevAll().addClass('left').end()
  .nextAll().addClass('right');
.left { color: red; }
.center { color: green; }
.right { color: blue; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div> <!--the middle one-->
<div class="item">4</div>
<div class="item">5</div>
like image 121
Rory McCrossan Avatar answered Oct 22 '22 11:10

Rory McCrossan


Without jQuery you might as well do like this;

var els = Array.from(document.querySelectorAll(".item")),
    mid = ~~(els.length/2);
els.forEach((e,i) => i < mid ? e.classList.add("left")
                             : i === mid ? e.classList.add("center")
                                         : e.classList.add("right"));
.left   {color: red}
.center {color: green}
.right  {color: blue}
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
like image 39
Redu Avatar answered Oct 22 '22 10:10

Redu