跳过列的CSS列计数元素

时间:2014-04-24 04:33:46

标签: javascript jquery html css css3

我正在尝试使用CSS3 column-count在5个元素中显示动态数量的元素,但是当我在悬停时展开列表项高度时,它偶尔会导致跳转(元素转到下一列) )。

您可以看到行为here

我假设是因为column-count使用高度来计算哪个项目在哪里或哪里......我们怎样才能让它按预期工作?

如果我尝试增加<ol>的高度,它们会变成4列甚至3列,因为元素会填满第一列,然后启动第二列,依此类推。

3 个答案:

答案 0 :(得分:5)

简而言之,CSS3列不是您尝试做的正确解决方案。 (如果我理解正确,你希望hovered元素通过跳出它的框来溢出它的父容器。但是,CSS3列被设计成溢出将继续在下一列的顶部,并且我无法意识到改变这种行为)。

我建议使用不同的方法来实现您所使用的UI,例如使用JQuery在每列之间插入包装器。

但是,如果您开始使用列计数,您可以通过执行以下操作来破解它:

JSFiddle:

http://jsfiddle.net/p6r9P/

<强> CSS:

ol li:nth-child(5n) {
  padding-bottom: 40px;
}

<强> JQuery的:

function togglePadding(li, status) {
    var index = li.index();
    var target = (index % 5 === 4) ? li : li.siblings().eq(index + 3 - (index % 5));
    target.stop(true).animate({
        "padding-bottom": (status === "on") ? "40px" : 0
    });
}

$('a.song').each(function () {
    var origwidth = $(this).width();
    var origheight = $(this).height();
    $(this).hover(function () {
        togglePadding($(this).parent(), "off");
        $(this).stop(true).animate({
            width: origwidth * 2,
            height: origheight * 2
        })
    }, function () {
        $(this).stop(true).animate({
            width: origwidth,
            height: origheight
        });
        togglePadding($(this).parent(), "on");
    });
    $(this).clone(true).attr({
        "class": "song-detail"
    }).css({
        "z-index": 1,
        "background-color": "#CCC"
    }).appendTo('ol').wrap("<li></li>");
});

这只是一个粗略的演示,需要进行清理才能进行生产。基本上,策略是在每第5个元素(列的结尾)之后添加40px填充“缓冲区”。当一个元素悬停时,我们在其列的末尾找到兄弟,并将其填充设置为0。

但是你可以看到,如果你连续快速地将鼠标放在几个元素上,有时盒子会因为一个盒子暂时跳到下一列而不寒而栗。 CSS3列数真的想要平衡这些列。

因此我建议您使用不同的方法,但请随意使用,看看是否可以使用它。

**编辑:没有列数的一种方法**

由于您已经在使用JQuery,因此可以将每个X元素包装在<div class="col">中,并将这些div用作列。

JSFiddle: http://jsfiddle.net/QhTvH/

<强> JQuery的:

var container;
var i = 0;
var numCols = 5;
var colCount = Math.ceil($('.songs a').length / numCols);

$('.songs a').each(function () {
    if (i % colCount === 0) {
        container = $('<div class="col"></div>').appendTo(".songs");
    }
    $(this).appendTo(container);
    i++;
});

<强> CSS:

.songs .col {
    max-width: 18%;
    overflow: hidden;
    display: inline-block;
    vertical-align: top;
    margin: 0 5px;
}
.songs a {
    display: block;
    margin: 10px 10px;
    background-color: #EEE;
    width: 200px;
    height: 40px;
    cursor: pointer;
    text-overflow:ellipsis;
    overflow: hidden;
    white-space: nowrap;
}

<强> HTML:

<section class="songs">
  <a class="song" data-src="songs/Titanic.mp3" style="width: 187px;">Titanic</a>
  <a class="song" data-src="songs/Titanic.mp3" style="width: 187px;">Titanic2</a>
  etc...
</section>

答案 1 :(得分:0)

您也可以找出最后一个col中剩余的空间,并将其填充到列中。

来自你的

DEMO codepen \ 0 / DEMO fiddle

jQuery补充道:

var col = 5; // number of columns
var liH =50; // average height ol li, including margins
var nbli = $('ol li').length; // how many do we have ?
var olH = nbli*liH; // total height of ol
var colnbli = Math.ceil(nbli/col); // how many li would it make if each column is fully filled ?
var colH = colnbli*liH; // what's average height of each col 
var ceilOlH= colH*col; //what's total height of columns together
var olH = nbli*liH; // what's real height of ol ?
var fixLiH = ceilOlH - olH; // how much difference do i have ?
var fixcol  =  $('<li style="height:'+fixLiH+'px;width:50%;padding:0; "></li>').appendTo('ol'); // let's see if we can fill the remaining gap

和CSS

li:hover ~li:last-child {
  margin-top:-10px ;
  display:block
}

要与动画匹配并避免跳跃li,请为transition添加limargin

ol li {
  display:inline-block;
  margin: 5px 10px;
  transition:0.5s;/* to match jQuery animate */
}

添加/删除<li>以确定这是您查找的内容还是重置column-count:5;

中的var col = 5 ;

答案 2 :(得分:0)

您是否尝试过使用a标签作为容器? 并为内部div(或其他)设置动画

Js小提琴:http://jsfiddle.net/keypaul/Yk2du/41/

<强> HTML

<ol>
    <li>
        <a class="song" data-src="songs/Titanic.mp3" style="width: 187px;">
            <div>Titanic</div>
        </a>
    </li>
    <li>
        <a class="song" data-src="songs/Titanic.mp3" style="width: 187px;">
            <div>Titanic</div>
        </a>
    </li>
    <li>
        <a class="song" data-src="songs/Titanic.mp3" style="width: 187px;">
            <div>Titanic</div>
        </a>
    </li>
    <li>
        <a class="song" data-src="songs/Titanic.mp3" style="width: 187px;">
            <div>Titanic</div>
        </a>
    </li>
    <li>
        <a class="song" data-src="songs/Titanic.mp3" style="width: 187px;">
            <div>Titanic</div>
        </a>
    </li>
</ol>

<强> JS

$('a.song').each(function() {
    var origwidth = $(this).width();
    var origheight = $(this).height();
    $(this).hover(
        function() {
            $(this).find('div').stop().animate({width: origwidth * 2, height: origheight * 2});
        }, function() {
            $(this).find('div').stop().animate({width: origwidth, height: origheight});
        });
    $(this).clone(true).attr({"class":"song-detail"}).css({"z-index": 1, "background-color":"#CCC"}).appendTo('ol').wrap("<li></li>");
});

<强> CSS

ol {
  padding: 0px;
  list-style: decimal-leading-zero inside;
  color: #000;
  font-size: 0.9em;
  -moz-column-count: 5;
  -webkit-column-count: 5;
  column-count: 5;
}

ol li {
  display: inline-block;
  margin: 5px 10px;
}

ol li a {
  background-color: #EEE;
  display: block;
  width: 200px;
  height: 40px;
  cursor: pointer;
  text-overflow:ellipsis;
  white-space: nowrap;
  margin: 0;
    position:relative;
}

ol li a div {
    position:absolute;
    top:0;left:0;
    width:187px;
    height:40px;
    background:red;
}