【问题标题】:Scroll to element next to current on滚动到当前打开旁边的元素
【发布时间】:2012-08-16 00:37:13
【问题描述】:

当我单击箭头按钮时,窗口会滚动到下一个元素,但是当我向下滚动并单击随机元素时,它会首先返回到我之前单击的元素,或者,如果我不单击,它转到第一个元素。

我希望它滚动到当前点击的元素旁边。一切都在冒泡。

我工作的网站,出现这种情况的地方 - http://dawidjarzabek.pl/test

jQuery:

var currentElement = $(".post");

$('.post #nav a.prev').click(function() {
    currentElement = currentElement.prev();
    $.scrollTo(currentElement, 800);
});

$('.post #nav a.next').click(function() {
    currentElement = currentElement.next();
    $.scrollTo(currentElement, 800);
});

HTML:

<div id="1" class="section">

    <div class="post position">
        <div id="photo"><center><img src="photos/studio/1.jpg" /></center></div>
        <div id="nav"><a class="prev"><img src="images/icon_top.png" /></a><a class="next"><img src="images/icon_bottom.png" /></a></div>
        <div id="text">
            <h1>Modelka: </h1>
            <h2></h2>
            <h1>Wizaż : </h1>
            <h2></h2>
        </div>
    </div>
    <div class="post position">
        <div id="photo"><center><img src="photos/studio/2.jpg" /></center></div>
        <div id="nav"><a class="prev"><img src="images/icon_top.png" /></a><a class="next"><img src="images/icon_bottom.png" /></a></div>
        <div id="text">
            <h1>Modelka: </h1>
            <h2></h2>
            <h1>Wizaż : </h1>
            <h2></h2>
        </div>
    </div>
</div>

[the rest .post and .section]

【问题讨论】:

    标签: jquery html scroll slide


    【解决方案1】:

    试试这个:

    $('.post #nav a.prev').click(function() {
        $.scrollTo($(this).closest('.post').prev(),800);
    });
    
    $('.post #nav a.next').click(function() {
        $.scrollTo($(this).closest('.post').next(),800);
    });
    

    而且您不必存储变量 currentElement。 ;)

    【讨论】:

      猜你喜欢
      • 2012-07-08
      • 2020-07-28
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 2016-02-18
      相关资源
      最近更新 更多