0

In regards to an earlier question of mine here and the answer here I posted this new question since this is more Javascript related.

Checkout my fiddle here

I want to create a dropdown menu with CSS flex. I'm struggeling to get the container div to be as wide as the columns. With below code the calculation of the last item in the list to completely left is wrong. It looks like it's calculating the distance to the previous column.

So what I have is

<div class="cats">
    <ul>
        <li class="item sub">
            <a title="Baby" href="http://ministry.webshopapp.com/baby/" class="itemLink">Baby</a>
            <div class="subnav">
                <ul class="flex-wrap">
                    <li class="subitem title">
                        <a title="Borstvoeding" href="#" class="title">something</a>
                    </li>
                    <li class="subitem">
                        <a title="ATTITUDE" href="#" class="subitemLink">ATTITUDE</a>
                    </li>
                    <li class="subitem">
                        <a title="Apple Park" href="#" class="subitemLink">Apple Park</a></li>
                    <li class="subitem title">
                        <a title="Borstvoeding" href="#" class="title">something</a>
                    </li>
                <ul>
            </div>
        </li>
    </ul>
</div>
.cats .subnav {
  background: #fff none repeat scroll 0 0;
  border: 1px solid #eee;
  display: none;
  margin: 0;
  padding: 30px;
  position: absolute;
  text-align: left;
  z-index: 99;
}
.cats .item.sub:hover .subnav {
  display: block;
}




.subnav .wrap{
            display:inline-flex;
            flex-flow : column wrap;
            align-content : flex-start;
            height : 350px;
            background : blue;
        }
.subitem{
    width : 150px;
    height:100px;
    background : red;
    margin : 20px;
}
$(document).ready(function() {
    $('.flex-wrap').each(function(index) {
        var lastChild = $(this).children().last();
        var newWidth = lastChild.position().left - $(this).position().left + lastChild.outerWidth(true);
        $(this).width(newWidth);
    });
});

When using it in this way it looks like it's calculating the distance to the previous column. So something like 200px instead of 600px. Can this be caused by positioning or am I doing something wrong I'am completely overlooking now?

Community
  • 1
  • 1
Meules
  • 1,349
  • 4
  • 24
  • 71

1 Answers1

0

EDITED

The width of all li elements can't be obtained, since they are at display:none;.

I've made something with your Fiddle...
You will be able to continue styling from this. ;)

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64