3

順序なしリストを使用した単純なギャラリーがあります。

    <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クラスの4つの子ごとに右マージンの値をゼロに設定しました。ページの 1 行に 4 つのアイテムを保持するためにそれを行いましたが、余白を削除しないと 3 つしか収まりませんでした。

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();

私の問題は次のとおりです。上記の jQuery スクリプトを使用してギャラリー アイテムを並べ替えると、.item:nth-child プロパティがまだ表示プロパティが none に設定されている要素をカウントしているようです。噛み方がわかりません。jQuery プラグインが起動された後に表示される要素のみをカウントするには、css .item:nth-child プロパティが必要です。

ウェブサイトはこちらhttp://www.damianwojtczak.com/avd2/projects.php

4

4 に答える 4

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");
})

firebug を使用してサイトでテストしたところ、うまくいきました。

于 2013-02-02T16:57:42.493 に答える
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);
于 2013-02-02T17:08:18.040 に答える