2013-02-02 199 views
3

我有简单的画廊使用无序列表。Nth-child css属性将显示属性设置为none的html元素进行计数。如何改变它?

<h1>Projects</h1> 
    <hr> 

    <!-- Projects gallery as unordered list --> 

    <ul class="gallery"> 

     <li class="item residential"> 
     <img src="Projects/01-HighTorEast-EarlShilton/01-thumbnail.jpg" width="212" height="119" alt="High Tor East, Earl Shilton"> 
     <h2><a class="info" href="Projects/01-HighTorEast-EarlShilton/info.php">High Tor East, Earl Shilton</a></h2> 
     <h3><a class="cat" href="residential">Residential</a></h3> 
     </li> 

     <li class="item modernisation"> 
     <img src="Projects/02-Hollycroft-Hinckley/02-thumbnail.jpg" width="212" height="119" alt="Hollycroft, Hinckley"> 
     <h2><a class="info" href="Projects/02-Hollycroft-Hinckley/info.php">Hollycroft, Hinckley</a></h2> 
     <h3><a class="cat" href="modernisation">Modernisation & Domestic Extensions</a></h3> 
     </li> 

     <li class="item residential"> 
     <img src="Projects/03-SpaLane-Hinckley/03-thumbnail.jpg" width="212" height="119" alt="Spa Lane, Hinckley"> 
     <h2><a class="info" href="Projects/03-SpaLane-Hinckley/info.php">Spa Lane, Hinckley</a></h2> 
     <h3><a class="cat" href="residential">Residential</a></h3> 
     </li> 

     <li class="item residential"> 
     <img src="Projects/04-Farnhambridge-Rothley/04-thumbnail.jpg" width="212" height="119" alt="Farnhambridge Farm, Rothley"> 
     <h2><a class="info" href="Projects/04-Farnhambridge-Rothley/info.php">Farnhambridge Farm, Rothley</a></h2> 
     <h3><a class="cat" href="residential">Residential</a></h3> 
     </li> 

     <li class="item modernisation"> 
     <img src="Projects/05-NetherfieldClose-BroughtanAstley/05-thumbnail.jpg" width="212" height="119" alt="Netherfield Close, Broughtan Astley"> 
     <h2><a class="info" href="Projects/05-NetherfieldClose-BroughtanAstley/info.php">Netherfield Close, Broughtan Astley</a></h2> 
     <h3><a class="cat" href="modernisation">Modernisation & Domestic Extensions</a></h3> 
     </li> 

     <li class="item modernisation"> 
     <img src="Projects/06-Bridlepath-Elmesthorpe/06-thumbnail.jpg" width="212" height="119" alt="Bridlepath, Elmesthorpe"> 
     <h2><a class="info" href="Projects/06-Bridlepath-Elmesthorpe/info.php">Bridlepath, Elmesthorpe</a></h2> 
     <h3><a class="cat" href="modernisation">Modernisation & Domestic Extensions</a></h3> 
     </li> 

     <li class="item residential"> 
     <img src="Projects/07-Bridlepath-Elmesthorpe/07-thumbnail.jpg" width="212" height="119" alt="Bridlepath, Elmesthorpe"> 
     <h2><a class="info" href="Projects/07-Bridlepath-Elmesthorpe/info.php">Bridlepath, Elmesthorpe</a></h2> 
     <h3><a class="cat" href="residential">Residential</a></h3> 
     </li> 

     <li class="item feasibility"> 
     <img src="Projects/08-GrangeCroft-Ullesthorpe/08-thumbnail.jpg" width="212" height="119" alt="Grange Croft, Ullesthorpe"> 
     <h2><a class="info" href="Projects/08-GrangeCroft-Ullesthorpe/info.php">Grange Croft, Ullesthorpe</a></h2> 
     <h3><a class="cat" href="feasibility">Feasibility layouts</a></h3> 
     </li> 

     <li class="item master"> 
     <img src="Projects/09-RailwayInn-Sileby/09-thumbnail.jpg" width="212" height="119" alt="The Railway Inn, Sileby"> 
     <h2><a class="info" href="Projects/09-RailwayInn-Sileby/info.php">The Railway Inn, Sileby</a></h2> 
     <h3><a class="cat" href="master">Master planning</a></h3> 
     </li> 


    </ul> 

    </section> 

比我的CSS我设定每四个孩子.item类的右边距值为零。我这样做是为了在我的页面上保留四行,并且不删除页边距,我只能放三页。

ul.gallery { clear:both; list-style:none; margin:0; padding:0; width:940px; } 
.gallery a:hover { text-decoration:underline; } 
li.item { display:inline; margin:30px 20px 30px 0px; padding:0px; height:178px; width:220px; float:left; background:#ededed url('../Images/featuredline.png') repeat-x 0% 100%; overflow:hidden; } 
li.item:nth-child(4n+4) { margin:30px 0px 30px 0px; } 

接下来,我写了一个小的jQuery代码来按类别排序项目。所以如果有人点击“住宅”链接,它会隐藏不同类别的元素。

$('.cat').bind('click', function(e) { 
    var cat = $(this).attr('href'); 
    $('.item').each(function() { 
    var itemClass = $(this).attr('class'); 
    if (itemClass != 'item '+cat) { 
     $(this).css({"display":"none"}); 
    }; 


    }); 
    e.preventDefault(); 

我的问题是:第n个孩子财产仍在计数具有显示属性设置为none元素:当我和上面的jQuery脚本似乎.item我的画廊项目进行排序。我不知道如何咬它。我需要css .item:nth-​​child属性来计算jQuery插件启动后仅显示可见元素。

网站可以在这里找到http://www.damianwojtczak.com/avd2/projects.php

+0

当你设置'display:none'时,你可以将元素移动到最后并在再次使其可见时将其移回去? –

+1

...或者只是完全从DOM中删除元素,而不是仅隐藏它们? – Bergi

+0

使用js代替css代替第n个孩子 – salexch

回答

1

删除您的第n个孩子的CSS样式,并在每次更改布局时,称此:

$("li.item:visible").each(function(i) { 
    if((i+1)%4==0) $(this).css("margin","30px 0"); 
    else $(this).css("margin","30px 20px 30px 0"); 
}) 

只是测试它在网站上使用萤火虫和工作一种享受。

+0

插入它点击事件 – salexch

+0

是的,我希望它是明显的,但作为此评论,我会澄清,这将是最适合您的当前点击处理程序 – SmokeyPHP

+0

完美!解决我的问题。谢谢。 – Guferos

1

一种解决方案是将nth-child css选择器更改为类,并使用jQuery切换类。

li.item.marginClass { margin:30px 0px 30px 0px; } 

JS:

$(function(){ 
    var $items=$('li.item'); 
    function toggleMargin(}{ 
     $items.removeClass('marginClass').filter(':visible').addClass(function(index){ 
       return index % 4 ===0 && index !=0 ? 'marginClass' : ''; 
     }); 
    } 
    /* call for page loaded items*/ 
    toggleMargin(}; 

    $('.cat').bind('click', function (e) { 
     /* filtering code here*/ 

     /* adjust class*/ 
     toggleMargin(}; 

    }) 



}); 
1

当您隐藏节点,设置一些“是否隐藏”属性的标志$(this).attr('isHidden'),当你告诉你的节点,删除此标志.removeAttr('isHidden'),那么你就可以更新你的选择

li.item:not([isHidden]):nth-child(4n+4) { margin:30px 0px 30px 0px; } 

如果您不想使用额外的属性,可以将其添加为类名称,如Tomalak's answer

+0

我认为这是一个问题。我可以写'li.item:not([isHidden]):nth-​​child(2n + 1)',但是现在,Firefox解释为'li.item:nth-​​child(2n + 1):not [isHidden])'并在类似的开发工具中显示它。 – ngmir

+1

@ngmir好奇,在规范中找不到提及说伪类的顺序取决于解释器,所以它听起来像一个错误 –

+1

@ngmir同样的事情发生在Chrome v40中:/ http://jsfiddle.net/c1081h0f/ –

0

CSS

,而不是

li.item:nth-child(4n+4) { margin:30px 0px 30px 0px; } 

使用

li.item.split { margin:30px 0px 30px 0px; } 

jQuery的

reorderColumns = function(ev) { 
    $('li.item').removeClass('split'); 
    $('li.item:visible:nth-child(4n+4)').addClass('split'); 
    ev.preventDefault(); 
}; 

$('.cat').on('click',reorderColumns); 
+0

'4n + 4'与'2n + 1'(奇数)或'2n'(偶数) –

+0

不一样......你是对的......误解了它。修复了我的回复。 – Chris