javascript - ul float 左列表上的边框底部问题,​​具有打开框功能

标签 javascript jquery html css

我之前问过一个问题:

Li float left, length dynamic : no border-bottom on the last row

感谢您的回答,但是,当我添加“打开框功能”时,边框底部出现问题。

代码如下:

$('li').click(function() {
  if (!$('div.' + $(this).index()).length) {
    for (a = $('li').length - 1; a >= 0; a--) {
      if ($('li').eq(a).offset().top == $(this).offset().top) {
        $('li').eq(a).after('<div class="' + $(this).index() + '">' + $(this).html() + '</div>');
        $('div:not(.' + $(this).index() + ')').remove();
        break;
        
        /*$('div.' + $(this).index()).slideDown(200);
        $('div:not(.' + $(this).index() + ')').slideUp(200, function() {
          $(this).remove();
        });*/
      }
    }
  } else {
    $('div.' + $(this).index()).slideUp(200, function() {
      $(this).remove();
    });
  }
})
ul {
  display: block;
  width: 100%;
  margin: 0;
  padding: 0
}

li {
  display: block;
  width: 33%;
  height: 120px;
  float: left;
  margin: 0;
  padding: 0;
  border-bottom: #ccc 1px solid;
  box-sizing: border-box;
  background: #fee;
}

ul:after {
  content: '';
  display: block;
  clear: both;
}

/*li.no-border {
  border-bottom: 0px;
}*/

li:nth-last-child(4) ~ li:nth-child(3n+1), 
li:nth-last-child(4) ~ li:nth-child(3n+1) ~ li{
  border-bottom: 0px; 
}

div {
  background: #ccc;
  height: 150px;
  width: 100%;
  clear: both;
  display: block
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li>7</li>
  <li>8</li>
</ul>

请帮忙。谢谢。

最佳答案

也许你可以绘制一个顶部边框(或阴影)并通过负边距 (或 position:relative 或 translate() ) 隐藏第一行的边框并溢出。

ul {
overflow:hidden;
}

li {
  margin:-1px 0 0;
  box-shadow:0 -1px #ccc;

下面的演示

$('li').click(function() {
  if (!$('div.' + $(this).index()).length) {
    for (a = $('li').length - 1; a >= 0; a--) {
      if ($('li').eq(a).offset().top == $(this).offset().top) {
        $('li').eq(a).after('<div class="' + $(this).index() + '">' + $(this).html() + '</div>');
        $('div:not(.' + $(this).index() + ')').remove();
        break;
        
        /*$('div.' + $(this).index()).slideDown(200);
        $('div:not(.' + $(this).index() + ')').slideUp(200, function() {
          $(this).remove();
        });*/
      }
    }
  } else {
    $('div.' + $(this).index()).slideUp(200, function() {
      $(this).remove();
    });
  }
})
ul {
  display: block;
  width: 100%;
  margin: 0;
  padding: 0;
  overflow:hidden;
}
li {
  display: block;
  width: 33%;
  height: 120px;
  float: left;
  padding: 0;     
  margin:-1px 0 0px;
  box-shadow:0 -1px #ccc;
  box-sizing: border-box;
  background: #fee;
}

ul:after {
  content: '';
  display: block;
  clear: both;
}

div {
  background: #def;
  height: 150px;
  width: 100%;
  clear: both;
  display: block
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li>7</li>
  <li>8</li>
</ul>

关于javascript - ul float 左列表上的边框底部问题,​​具有打开框功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40636203/

相关文章:

javascript - 提交jquery生成的表单

javascript - 让脚本等到 iframe 加载后再运行

javascript - Grunt CSS 缩小和谷歌字体

javascript - 不可变 vs javascript 数组、对象——什么是正确的平衡点?

javascript - FreeBSD Ant javax 脚本引擎

javascript - 通过 AJAX 将 javascript 数组发送到同一页面时遇到问题

html - 仅在鼠标悬停后为第一组列表应用边框

php - 传递 PHP 变量时,Javascript 抛出 TypeError、document.getElementById

javascript - 不同设备上两个不同HTML文件之间的通信(JS)

javascript - 如何设置页面加载时的可滚动区域?