通常我会给你一些现有滑块脚本的链接,但我不知道有任何可以很好地调整大小。
所以我从我自己的 PHP 异常处理程序中改编了一些代码:)
$('#list').each(function(){
var list = $(this),
currentPos = 0,
itemCount = list.children().length;
$('.right, .left').click(function(){
// +100 = right, -100 = left
var direction = $(this).hasClass('right') ? 100 : -100,
nextIndex = direction > 0 ? currentPos + 1 : currentPos - 1;
// do nothing if there is animation happening,
// or if index is out of boundaries
if($('> li', list).is(':animated')
|| (direction > 0 && nextIndex > itemCount - 1)
|| (direction < 0 && nextIndex < 0)
) return;
var next = $('> li:eq(' + nextIndex + ')', list),
current = $('> li:eq(' + currentPos + ')', list);
currentPos = nextIndex;
// determine if the link needs to be hidden
$('.left, .right').removeClass('hide');
if(currentPos === 0 || currentPos === itemCount - 1)
$(this).addClass('hide');
// adjust height of the container
list.css('height', Math.max(current.outerHeight(true), next.outerHeight(true)) + 'px');
// make the current slide absolute
current.css({
position: 'absolute',
left: 0,
top: 0,
width: '100%',
height: current.outerHeight(true) + 'px'
});
// show the next slide
next.css({
position: 'absolute',
left: direction + '%',
top: 0,
width: '100%',
height: next.outerHeight(true) + 'px'
}).show().animate({left: '0%'}, 300, 'swing', function(){
next.css({
position: 'relative',
left: 'auto',
top: 'auto'
});
});
// hide the current slide
current.animate({left: (-direction) + '%'}, 300, 'swing', function(){
current.hide();
});
});
// mouse wheel action within the list area
list.mousewheel(function(event, delta){
(delta > 0) ? $('.right').click() :$('.left').click();
});
});
CSS:
.hide{
display:none;
}
#list{
position: relative;
width: 100%;
overflow: hidden;
}
#list > li{
display: none;
}
#list > li:first-child{
display: block;
}
HTML 结构应如下所示:
<a class="left"> left </a>
<a class="right"> right </a>
<ul id="list">
<li> ... </li>
...
</ul>
演示
鼠标滚轮jQuery 插件。如果您不需要鼠标滚轮支持,请删除最后一个事件。