我很难想出解决以下问题的方法 让我先说明一下:
场合
我有26个项目(在这个例子中,一般来说这个数字是未知的..)但是一次只能看到12个..我也有一些导航元素(绿色框)
紫色和绿色框的宽度是固定的,但紫色的高度可能因内容而异。
一切正常,我可以用css做。
我正在为我的项目使用无序列表(浮动项目),并且我已将前两个<li>
元素指定为导航元素。首先是左浮动,第二浮右。
这一切都有效,其余项目的流程介于两个绿色项目之间。
问题
但是现在我需要将绿色项目放在第二行(如果该概念有所帮助,则需要最后一行,因为只有两行)
我希望能够隐藏前X个元素并显示下一个X并且它们自己落在了位置上。
为了重新解释这个问题,我能否以某种方式定位某些元素(绿色元素)以控制它们的位置,但仍允许它们干扰来自新位置的流量?
我希望这很清楚。如果不问,我会提供尽可能多的信息..
我尝试过的不起作用的事情
[他们的项目被隐藏了,我只是展示它们,所以你知道它们存在.. ]
一些可以帮助您入门的代码
<style type="text/css">
ul,li{padding:0;margin:0; list-style-type:none;}
ul{
width:155px;
position:relative;
height:125px;
border:1px solid red;
}
li{
float:left;
background-color:purple;
margin-left:5px;
margin-top:5px;
width:25px;
text-align:center;
line-height:25px;
color:white;
}
.prev{
color:black;
background-color:green;
}
.next{
color:black;
float:right;
margin-right:5px;
background-color:green;
}
</style>
和
<body>
<ul>
<li class="prev"><</li>
<li class="next">></li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li>11</li>
<li>12</li>
<li>13</li>
<li>14</li>
<li>15</li>
<li>16</li>
<li>17</li>
</ul>
</body>
答案 0 :(得分:5)
好的,显然这只能通过Css / Html来解决..
所以,为了解决这个问题,我使用了一些CSS(带有 inline-block crossbrowser)和一些jQuery来移动导航按钮,这样他们总能保持在我想要的位置..
这里的参考是解决方案..
<强> CSS 强>
<style type="text/css">
ul,li{padding:0;margin:0; list-style-type:none;}
#performances{
width:155px;
border:1px solid red;
line-height:0;
font-size:0;
}
#performances li{
font-size:12px;
background-color:purple;
margin-left:5px;
margin-bottom:5px;
margin-top:5px;
width:25px;
text-align:center;
line-height:25px;
color:white;
display:none;
vertical-align:top;
}
#performances .prev{
color:black;
background-color:green;
display: -moz-inline-stack;
display:inline-block;
}
#performances .next{
color:black;
background-color:green;
display: -moz-inline-stack;
display:inline-block;
}
#performances .shown{
display: -moz-inline-stack;
display:inline-block;
}
#performances .placeholder{visibility:hidden;}
</style>
<!--[if lte IE 7]><style>
#performances .prev,#performances .next,#performances .shown{zoom:1;*display:inline;}
</style><![endif]-->
Javascript(jQuery)
<script type="text/javascript">
function resetNextPrev( ){
$next.insertAfter('#performances li.shown:eq('+ ($perpage-1) +')');
$prev.insertAfter('#performances li.shown:eq('+ ($perline-1) +')');
}
$(document).ready(function(){
$perpage = 8;
$perline = ($perpage+2) / 2;
$page = 1;
$itemcount = $('#performances li.performance').length;
$pages = Math.ceil ( $itemcount / $perpage);
$next = $('#performances li.next');
$prev = $('#performances li.prev');
$('#performances li.performance').slice(0,$perpage).addClass('shown');
if (($pages * $perpage) > $itemcount )
for (var i =0;i< ($pages * $perpage)-$itemcount;i++)
$('<li class="performance placeholder">test</li>').appendTo('#performances');
resetNextPrev();
$('li.next').click(function(){
if ($page < $pages)
$('#performances li.performance').filter('.shown').removeClass('shown').end().slice($perpage * (++$page-1),$perpage * $page).addClass('shown');
resetNextPrev( $perline );
});
$('li.prev').click(function(){
if ($page > 1)
$('#performances li.performance').filter('.shown').removeClass('shown').end().slice($perpage * (--$page-1),$perpage * $page).addClass('shown');
resetNextPrev( $perline );
});
});
</script>
..和 HTML
<ul id="performances">
<li class="prev"><</li>
<li class="next">></li>
<li class="performance">1</li>
<li class="performance">2</li>
<li class="performance">3</li>
<li class="performance">4 dfs s sf</li>
<li class="performance">5</li>
<li class="performance">6</li>
<li class="performance">7</li>
<li class="performance">8</li>
<li class="performance">9</li>
<li class="performance">10</li>
<li class="performance">11</li>
<li class="performance">12</li>
<li class="performance">13</li>
<li class="performance">14</li>
<li class="performance">15</li>
<li class="performance">16</li>
<li class="performance">17</li>
<li class="performance">18</li>
<li class="performance">19</li>
</ul>
里面有几个硬编码的值,但我会留给任何可能从中获取新东西的人......
感谢大家的指导:)
工作示例(适合想要在行动中看到它的人):http://www.jsfiddle.net/gaby/Ba3UT/
答案 1 :(得分:4)
对于这种复杂的情况,使用javascript可能是值得的。像jQuery这样的库可以让它变得非常轻松。在js中显式定义这些包装规则将比使用大量css规则隐式地执行它更清晰,更容易维护。
答案 2 :(得分:2)
将导航的li
设为display: inline-block
,并将导航li
移至列表末尾?
答案 3 :(得分:1)
没有看到你的代码,我无法确定。但是,您是否尝试将绿色元素放入标记并将其标记为清晰:两者;?这可能会让他们进入第3排。这是你应该结账的东西。