Jquery Hexagon分选

时间:2015-11-03 00:57:34

标签: jquery html5 twitter-bootstrap css3 sass

我正在尝试制作和六边形网格,可以对网格进行排序和维护,并且能够快速响应,但我无法解决这个问题。

我创建了一个Jsfile来更好地理解我的问题。 https://jsfiddle.net/Miguel12979/keucaf7o/

JQuery的

$( document ).ready(function() {
 $("#categories").children().show();
});
$(function(){
$("#btn-menu").on('click','div',function(){
   var classToShow = this.id.split('-')[1],
       filter = classToShow === "all" ? 'li': '.' + classToShow;
   $("#categories")
       .children().show()
       .not(filter).hide();
  });  
});

Sass mixins

    /*Transform mixin*/
    @mixin transform($transforms) {
       -moz-transform: $transforms;
         -o-transform: $transforms;
        -ms-transform: $transforms;
    -webkit-transform: $transforms;
          transform: $transforms;
    }
    /*Transition mixin*/
    @mixin transition($tran...) {
     -webkit-transition: $tran;
     -moz-transition: $tran;
     -ms-transition: $tran;
     -o-transition: $tran;
     transition: $tran;
    }

带有少量scss的CSS

    /*CSS*/
    body{
      font-family: 'Open Sans', arial, sans-serif;
    }
    * {
     margin: 0;
     padding: 0;
    }

#categories {
      overflow: hidden;
      width: 90%;
      margin: 0 auto;
      display: block;
     }

      .clr:after {
      content: "";
      display: block;
      clear: both;
      }

#categories li {
    position: relative;
    list-style-type: none;
    float: left;
    overflow: hidden;
    visibility: hidden;
    @include transform (rotate(-60deg) skewY(30deg));
    }

#categories li * {
    position: absolute;
    visibility: visible;
    }

#categories li > div {
    width: 100%;
    height: 100%;
    text-align: center;
    color: #fff;
    overflow: hidden;
    @include transform (skewY(-30deg) rotate(60deg));
    backface-visibility: hidden;
    }


/* HEX CONTENT */

#categories li img {
    left: -100%;
    right: -100%;
    width: auto;
    height: 100%;
    margin: 0 auto;
   }

#categories div h1,
#categories div p {
    width: 100%;
    padding: 0 5%;
    background-color: #008080;
    background-color: rgba(0, 128, 128, 0.8);
    font-family: 'Raleway', sans-serif;
    @include transition (top .2s ease-out, bottom .2s ease-out, .2s padding .2s ease-out);
    margin: 0;
    }

#categories li h1 {
    bottom: 110%;
    font-style: italic;
    font-weight: normal;
    font-size: 1.5em;
    padding-top: 100%;
    padding-bottom: 100%;
    }

#categories li h1:after {
    content: '';
    display: block;
    position: absolute;
    bottom: -1px;
    left: 45%;
    width: 10%;
    text-align: center;
    z-index: 1;
    border-bottom: 2px solid #fff;
    }

#categories li p {
    padding-top: 50%;
    top: 110%;
    padding-bottom: 50%;
    }


/* HOVER EFFECT  */

#categories li div:hover h1 {
    bottom: 50%;
    padding-bottom: 10%;
    }

#categories li div:hover p {
    top: 50%;
    padding-top: 10%;
}

    @media (min-width:1201px) {
    #categories li {
        width: 19.2%;
        /* = (100-4) / 5 */
        padding-bottom: 22.17%;
        /* =  width /0.866 */
    }
    #categories li:nth-child(9n+6),
    #categories li:nth-child(9n+7),
    #categories li:nth-child(9n+8),
    #categories li:nth-child(9n+9) {
        margin-top: -4.2%;
        margin-bottom: -4.2%;
        @include transform (translateX(50%) rotate(-60deg) skewY(30deg));
    }
    #categories li:nth-child(9n+6):last-child,
    #categories li:nth-child(9n+7):last-child,
    #categories li:nth-child(9n+8):last-child,
    #categories li:nth-child(9n+9):last-child {
        margin-bottom: 0;
    }
    #categories li:nth-child(9n+6) {
        margin-left: 0.5%;
        clear: left;
    }
    #categories li:nth-child(9n+10) {
        clear: left;
    }
    #categories li:nth-child(9n+2),
    #categories li:nth-child(9n+ 7) {
        margin-left: 1%;
        margin-right: 1%;
    }
    #categories li:nth-child(9n+3),
    #categories li:nth-child(9n+4),
    #categories li:nth-child(9n+8) {
        margin-right: 1%;
    }
    }

    @media (max-width: 1200px) and (min-width:901px) {
    #categories li {
        width: 24.25%;
        /* = (100-3) / 4 */
        padding-bottom: 28.002%;
        /* =  width /0.866 */
    }
    #categories li:nth-child(7n+5),
    #categories li:nth-child(7n+6),
    #categories li:nth-child(7n+7) {
        margin-top: -6.1%;
        margin-bottom: -6.1%;
        @include transform (translateX(50%) rotate(-60deg) skewY(30deg));
    }
    #categories li:nth-child(7n+5):last-child,
    #categories li:nth-child(7n+6):last-child,
    #categories li:nth-child(7n+7):last-child {
        margin-bottom: 0;
    }
    #categories li:nth-child(7n+2),
    #categories li:nth-child(7n+6) {
        margin-left: 1%;
        margin-right: 1%;
    }
    #categories li:nth-child(7n+3) {
        margin-right: 1%;
    }
    #categories li:nth-child(7n+8) {
        clear: left;
    }
    #categories li:nth-child(7n+5) {
        clear: left;
        margin-left: 0.5%;
    }
    }

    @media (max-width: 900px) and (min-width:601px) {
    #categories li {
        width: 32.666%;
        /* = (100-2) / 3 */
        padding-bottom: 37.721%;
        /* =  width /0.866 */
    }
    #categories li:nth-child(5n+4),
    #categories li:nth-child(5n+5) {
        margin-top: -8.622%;
        margin-bottom: -8.622%;
        @include transform (translateX(50%) rotate(-60deg) skewY(30deg));
    }
    #categories li:nth-child(5n+4):last-child,
    #categories li:nth-child(5n+5):last-child {
        margin-bottom: 0;
    }
    #categories li:nth-child(5n+4) {
        margin-right: 1%;
        margin-left: 0.5%;
    }
    #categories li:nth-child(5n+2) {
        margin-left: 1%;
        margin-right: 1%;
    }
    #categories li:nth-child(5n+6) {
        clear: left;
    }
    }

    @media (max-width: 600px) {
    #categories li {
        width: 49.5%;
        /* = (100-1) / 2 */
        padding-bottom: 57.16%;
        /* =  width /0.866 */
    }
    #categories li:nth-child(3n+3) {
        margin-top: -13.392%;
        margin-bottom: -13.392%;
        @include transform (translateX(50%) rotate(-60deg) skewY(30deg));
    }
    #categories li:nth-child(3n+3):last-child {
        margin-bottom: 0;
    }
    #categories li:nth-child(3n+3) {
        margin-left: 0.5%;
    }
    #categories li:nth-child(3n+2) {
        margin-left: 1%;
    }
    #categories li:nth-child(3n+4) {
        clear: left;
    }
    }

1 个答案:

答案 0 :(得分:0)

酷!我喜欢在网上思考不是长方形的形状!我用三角形尝试了一次网格..

无论如何,你的问题是

  • 被隐藏了。但是当它们被隐藏时,它们仍然被css选择器li:nth-​​child()选中。

    所以我在想,如果我们将元素更改为隐藏那些元素会怎么样?并将css更改为使用li:nth-​​of-type(),以便div不会计数: - )

    你可以在小提琴中找到编辑过的css,只觉得我已经改变了找到替换所有" nth-child" =>" nth-of-type"

    https://jsfiddle.net/3e8y7rsy/

    JS:

    //Hide content Script
    $( document ).ready(function() {
        $("#categories").children().show();
    });
    
    $(function(){
        $("#btn-menu").on('click','div',function(){
           var classToShow = this.id.split('-')[1],
               filter = classToShow === "all" ? 'li': '.' + classToShow;
    
            //i don't know how to do this nicer..
            if(classToShow === "mon") {
                $('#categories').children().not('.mon').each(function(){
                    $(this).hide().changeElementType('div');
                });
                $('#categories').children('.mon').each(function(){
                    $(this).show().changeElementType('li');
                });
            } else if(classToShow === "ser") {
                $('#categories').children().not('.ser').each(function(){
                    $(this).hide().changeElementType('div');
                });
                $('#categories').children('.ser').each(function(){
                    $(this).show().changeElementType('li');
                });
            } else {
                $('#categories').children().each(function(){
                    $(this).show().changeElementType('li');
                });
            }
         /*  $("#categories")
               .children().show()
               .not(filter).changeElementType('div').addClass('hidden');*/
        });  
    });
    
    // function from http://stackoverflow.com/questions/8584098/how-to-change-an-element-type-using-jquery
    (function($) {
        $.fn.changeElementType = function(newType) {
            var attrs = {};
    
            $.each(this[0].attributes, function(idx, attr) {
                attrs[attr.nodeName] = attr.nodeValue;
            });
    
            this.replaceWith(function() {
                return $("<" + newType + "/>", attrs).append($(this).contents());
            });
        };
    })(jQuery);