查询:2个div向右/向左滑动

时间:2013-04-20 17:36:11

标签: jquery slider

我有这个HTML

<div id="container">
    <div id="slide">
        <div id="slide1">
            Content 1
            <a href="#" id="show-slide2">Show Content 2</a>
        </div>
        <div id="slide2">
            Content 2
        </div>
    </div>
</div>

#containermax-width:1250px。仅显示Onload #slide1。现在我想点击a#show-slide2#slide2应该从右边滑入。

我需要什么CSS和JS?

1 个答案:

答案 0 :(得分:2)

LIVE DEMO

HTML

<div id="slides">
  <div>
    Content 1
    <a href="#" id="show-slide2">Show Content 2</a>
  </div>
  <div>
    Content 2
  </div>
</div>

CSS:

#slides{  
  position:relative;
  overflow:hidden;
  margin:0 auto;
  background:#cf5;
  width:400px;
  height:200px;
  white-space:nowrap;
}
#slides div{
  position:relative;
  display:inline-block;
  margin-right:-4px;
  white-space:normal;
  vertical-align:top;
  *display:inline;
  background:#eee;
  width:400px;
  height:200px;
}

jQuery的:

$(function(){

  var slideW = $('#slides').width();
  $('#show-slide2').click(function( e ){
    e.preventDefault();
    $('#slides').animate({scrollLeft: slideW }, 600);
  });

});