我想弄清楚一個簡單的方法來使用jquery有5個項目的列表,然後按下箭頭,它顯示5個項目等等等等。如果向上和向下箭頭在到達頂部和底部時漸漸消失,那將是一個加號。任何幫助將不勝感激。jquery向下滑動,並用箭頭
0
A
回答
2
thi s應該工作,測試
<script type="text/javascript">
$(document).ready(function() {
//hide all list items, and show the first 5 items
$('#items li').hide().slice(0, 5).show();
//hide the up button
$('#up').hide();
var length = $('#items li').length;
var current_page = 0;
var per_page = 5;
$(".arrow").click(function(){
var arrow = $(this).attr("id");
if(arrow == 'down') {
current_page = current_page + 1; //increment the page
if (length >= current_page*per_page) { //check if it's possible page
$('#items li').hide().slice((current_page*per_page), (current_page*per_page+per_page)).fadeIn(); //show the next page
}
}
else if(arrow == 'up') {
current_page = current_page - 1; //decrement the page
if (current_page >= 0) { //check if it's possible page
$('#items li').hide().slice((current_page*per_page), (current_page*per_page+per_page)).fadeIn(); //show the prev page
}
}
//check if the down button will be still shown or hidden
if (length >= (current_page+1)*per_page) $('#down').show();
else $('#down').hide();
//check if the up button will be still shown or hidden
if ((current_page-1) >= 0) $('#up').show();
else $('#up').hide();
});
});
</script>
<img src="./up.jpg" id="up" class="arrow">
<img src="./down.jpg" id="down" class="arrow">
<ul id="items">
<li>1</li>
....
<li>30</li>
</ul>
0
嘗試類似這樣的東西。不是很花哨,但它的工作。
var topIndex = 0;
var step = 5;
var itemsCount = $('li').size();
showItemsStartingFrom(topIndex);
$('a.next').click(function()
{
showItemsStartingFrom(topIndex);
return false;
})
function showItemsStartingFrom(index)
{
$('li').hide(0)
.slice(index, index+step)
.show();
topIndex += step;
if (topIndex >= itemsCount)
$('a.next').fadeOut(300);
}
0
有很多jquery的分頁插件。它會通過爲您的項目調整一些代碼來節省您的時間。
http://blog.ajaxmasters.com/jquery-pagination-plugin/
http://tympanus.net/codrops/2009/11/17/jpaginate-a-fancy-jquery-pagination-plugin/
http://web.enavu.com/tutorials/making-a-jquery-pagination-system/
,或者如果你想自己做,它會是這樣的
<img src="./up.jpg" id="up" class="arrow">
<img src="./down.jpg" id="down" class="arrow">
<ul id="items">
<li>1</li>
....
<li>30</li>
</ul>
<script type="text/javascript">
$(document).ready(function() {
$('#items li').hide().slice(0, 4).show();
$(".arrow").click(function(){
var arrow = $(this).attr("id");
if(arrow == 'down') {
$('#items li:visible').last().nextAll().slice(0, 4).show("slow");
//or $('#items li:hidden').slice(0, 4).show("slow");
}
else if(arrow == 'up') {
$('#items li:visible').slice(-1, -4).hide("slow");
}
if ($('#items li:visible').length > 0) $('#up').fadeOut();
else $('#up').show();
if ($('#items li:hidden').length > 0) $('#down').show();
else $('#down').fadeOut();
});
});
</script>
PS:我無法測試它
相關問題
- 1. 如何在jQuery或Javascript中將箭頭綁定到「向上滾動」和向下箭頭以「向下滾動」?
- 2. jQuery:向左滑動並向右滑動
- 3. 向下滑動jQuery
- 4. 如何通過按上,使滑塊移動和向下箭頭
- 5. 在jquery中切換右箭頭向下箭頭[accordion-like]
- 6. html向上向下箭頭
- 7. jquery sticked向上和向下箭頭向上和向下
- 8. 滑動導航箭頭
- 9. 使用箭頭鍵向上和向下平滑滾動x像素
- 10. 如何使向下滾動用jquery點擊我的箭頭?
- 11. 禁用向上和向下箭頭
- 12. 箭頭向下彈跳動畫css
- 13. 硒IDE向下箭頭鍵
- 14. 向下箭頭的HTML
- 15. jQuery向下滑動導航
- 16. jQuery向下滑動面板
- 17. 如何禁用向上滑動並向下滑動
- 18. 如何樣式jQuery光滑箭頭
- 19. 有兩個箭頭的jQuery滑塊
- 20. jQuery自動完成禁用textarea中的向上/向下箭頭。爲什麼?
- 21. 向上/向下箭頭滾動不起作用
- 22. 向左滑動移動菜單,然後向下滑動JQuery
- 23. 粘頭向下滑動效果
- 24. 活動菜單標籤下的滑動箭頭
- 25. 在鍵盤上自動滾動向上/向下箭頭
- 26. 向上和向下箭頭的JavaScript
- 27. jQuery的向上滑動和向下滑動負一里一直滑下
- 28. 按下左箭頭時Jquery滑塊出現問題
- 29. Android的0nTouch事件問題[向下滑動並向上滑動]
- 30. 更改向上和向下箭頭與jquery
我似乎無法使它工作。我不能點擊向下的箭頭。它是否意味着具有與以前相同的HTML代碼?我很抱歉,我仍然對使用jquery非常陌生。 – Jacinto 2010-10-22 01:33:13
okey我寫了完整的代碼,並測試了它 – jargalan 2010-10-22 04:57:43
html代碼與之前的代碼相同 – jargalan 2010-10-22 04:58:36