编辑:好的,我想要制作一个通用的解决方案(通过动画包装器边距)。更清晰的代码和更可定制 =>
http://jsfiddle.net/steweb/rWbFw/
标记:
<div id="mask">
<div id="wrapper">
<div class="full" id="div1">hey there I'm the first div</div>
<div class="full" id="div2">hey there I'm the second div</div>
<div class="full" id="div3">hey there I'm the third div</div>
</div>
</div>
CSS:
#mask{
width:100%;
overflow:hidden;
position:relative;
}
#wrapper{
width:100%;
height:300px;
}
.full{
float:left;
height:300px;
}
#div1{
background:green;
}
#div2{
background:white;
}
#div3{
background:red;
}
JS:
var utils = {
maskWidth : $('#mask').width(),
currIndex : 0,
setWidths : function(){
utils.maskWidth = $('#mask').width();
$('#wrapper').css('width',$('.full').length * utils.maskWidth);
$('.full').each(function(index){
$(this).css('width',utils.maskWidth);
});
$('#wrapper').css('margin-left',-(utils.currIndex*utils.maskWidth));
}
}
$('.full').click(function(){
utils.currIndex = $(this).index()+1;
if($(this).next().size() == 0){
utils.currIndex = 0;
$('#wrapper').animate({'margin-left':0});
}else{
$('#wrapper').animate({'margin-left':-(utils.currIndex*utils.maskWidth)});
}
});
$(window).resize(function() {
utils.setWidths();
});
utils.setWidths();
-- NEW --
您可以从类似于以下链接的示例开始学习: http://jsfiddle.net/steweb/dsHyf/
标记:
<div id="wrapper">
<div class="full" id="div1"></div>
<div class="full" id="div2"></div>
</div>
CSS:
#wrapper{
width:100%;
overflow:hidden;
position:relative;
height:300px;
}
.full{
position:absolute;
width:100%;
height:300px;
}
#div1{
background:#FF0000;
left:0px;
}
#div2{
display:none;
background:#FFFF00;
}
js:
$('#div2').css('left',-$('#wrapper').width()).show();
$('#div1').click(function(){
$(this).animate({'left':$('#wrapper').width()});
$('#div2').animate({'left':0});
});
$('#div2').click(function(){
$(this).animate({'left':-$('#wrapper').width()});
$('#div1').animate({'left':0});
});