点击时水平动画滚动到div

时间:2015-11-09 13:47:53

标签: jquery css accordion scrollto

所以,我需要制作一个水平的手风琴卷轴,它将打开的div居中。 以下代码将帮助您了解:

<div class="wrapper">
    <div class="product">
        <div class="product-title">
            <img src="http://goo.gl/PUr1TP">
        </div>
        <div class="product-all">
            <div style="width: 200px; background: red;"   >1</div>
            <div style="width: 100px; background: yellow;">2</div>
            <div style="width: 400px; background: pink;"  >3</div>
            <div style="width: 270px; background: blue;"  >4</div>
            <div style="width: 500px; background: aqua;"  >5</div>
            <div style="width: 100px; background: green;" >6</div>
        </div>
    </div>
    <div class="product">
        <div class="product-title">
            <img src="https://goo.gl/hRJ3Dz">
        </div>
        <div class="product-all">
            <div style="width: 200px; background: red;"   >1</div>
            <div style="width: 100px; background: yellow;">2</div>
            <div style="width: 400px; background: pink;"  >3</div>
            <div style="width: 270px; background: blue;"  >4</div>
            <div style="width: 500px; background: aqua;"  >5</div>
            <div style="width: 100px; background: green;" >6</div>
        </div>
    </div>
    <div class="product">
        <div class="product-title">
            <img src="https://goo.gl/vX3Aih">
        </div>
        <div class="product-all">
            <div style="width: 200px; background: red;"   >1</div>
            <div style="width: 100px; background: yellow;">2</div>
            <div style="width: 400px; background: pink;"  >3</div>
            <div style="width: 270px; background: blue;"  >4</div>
            <div style="width: 500px; background: aqua;"  >5</div>
            <div style="width: 100px; background: green;" >6</div>
        </div>
    </div>
    <div class="product">
        <div class="product-title">
            <img src="http://goo.gl/rXt3kE">
        </div>
        <div class="product-all">
            <div style="width: 200px; background: red;"   >1</div>
            <div style="width: 100px; background: yellow;">2</div>
            <div style="width: 400px; background: pink;"  >3</div>
            <div style="width: 270px; background: blue;"  >4</div>
            <div style="width: 500px; background: aqua;"  >5</div>
            <div style="width: 100px; background: green;" >6</div>
        </div>
    </div>
</div>
body, html {
    max-width: 100%;
    max-height: 100%;
    margin: 0;
    padding: 0;
    background: #ccc;
}

html {
    -webkit-box-sizing: border-box; 
       -moz-box-sizing: border-box;
            box-sizing: border-box;
}

*, *:before, *:after {
    -webkit-box-sizing: inherit;
       -moz-box-sizing: inherit;
            box-sizing: inherit;
}

.wrapper {
    height: 300px;
    width: 100%;
    overflow-x: scroll;
    overflow-y: hidden;
    white-space: nowrap;
    margin: 200px 0 0 0;
}

.product, .product-title, .product-all, .product-all div {
    display: inline-block;
    height: 100%;
    vertical-align: top;
}

.product-title img {
    height: 100%;
}

.product {
    position: relative;
}

.button {
    float: right;
    padding: 5px 10px;
    border: 2px solid green;
    cursor: pointer;
}
$(document).ready(function() {    
    $(".product-all").hide(100);
    $(".product-title").click(function() {
        $(".product-all").hide(500);
        $(this).parent().children(".product-all").show(500);

        var scrollTo = $(this).parent().children(".product-title").position().left;
        $('.wrapper').animate({
            'scrollLeft': scrollTo
        }, 800);
    });
});

上面是我尝试过的代码,但是滚动似乎是在我的容器的最左边,而不是放置&#34;活动&#34;左角的div。任何想法如何解决它?谢谢!

这是fiddle

2 个答案:

答案 0 :(得分:1)

我发现了你的问题并找到了解决方案。

你没有得到正确的scrollTo。您试图获得所有孩子的位置(带有班级产品标题),但您需要点击孩子的位置。

var scrollTo = $(this).parent().position().left;

http://fiddle.jshell.net/jxv7641y/1/

答案 1 :(得分:1)

所以,我想出来了!

$(document).ready(function() {

 $(".product-all").hide(100);
 $(".product-title").click(function() {

  $(".product-all").hide(500);
  $(this).parent().children(".product-all").show(500);

  //get the index of .product just clicked
  var i = 0;
  var piu = $(this).parent().index();
 //.wrapper
  var wrapper = $(this).parent().parent();
  var scroll_shit = 0;

  while (i < piu) {
   //get width of each .product-title, because the sum of all the previous .product-titles is the needed position
   product = wrapper.children().eq(i);
      scroll_shit = scroll_shit + product.children(".product-title").width();
      i++;
  }

  $('.wrapper').animate({
   'scrollLeft': scroll_shit
  }, 800);
 });

});