获取可滚动div中第一个和最后一个可见元素

17
我有一组缩略图在可滚动的 div 中,通过 next/prev 按钮进行动画处理。每次单击 "下一个" 按钮都应该与第一个可见元素的属性匹配。每次单击 "上一个" 按钮都应该给我最后一个可见元素的属性。我不知道如何通过数学方法来解决这个问题,因为当列表结束时,滚动距离是可变的。请问有人能帮我吗?
HTML
$<div id="scrollContent">
    <ul id="assetList">
        <li data-asset-id="15201"></li>
        <li data-asset-id="15202"></li>
        <li data-asset-id="15203"></li>
        ...        
    </ul>
</div>
<a class="next" href="#">next</a>
<a class="prev" href="#">prev</a>

jQuery

$('a.next').click(function() {
    var scrollheight = $("#scrollContent").scrollTop();
    $("#scrollContent").animate({scrollTop:scrollheight+375},500,function() {  
        // get "data-asset-id" of first visible element in viewport

    });
});

$('a.prev').click(function() {
    var scrollheight = $("#scrollContent").scrollTop();
    $("#scrollContent").animate({scrollTop:scrollheight-375},500,function() {
        // get "data-asset-id" of last visible element in viewport

    });
});

请查看这个fiddle: http://jsfiddle.net/desCodLov/77xjD/10/

谢谢。

3个回答

8
这是您正在寻找的吗?
var first, last;

$('a.next').click(function() {
    var scrollheight = $("#scrollContent").scrollTop();
    $("#scrollContent").animate({scrollTop:scrollheight+375},500,function() {  
        $("#assetList li").each(function() {
            if ($(this).offset().top == 1 && $(this).offset().left == 0) {
                first = $(this).attr('data-asset-id');
            }
        });
    });
});

$('a.prev').click(function() {
    var scrollheight = $("#scrollContent").scrollTop();
    $("#scrollContent").animate({scrollTop:scrollheight-375},500,function() {
        var Otop = $("#scrollContent").height() - $("#assetList li").height() - parseInt($("#assetList li").css('margin-top'));
        var Oleft = ($("#assetList li").width() + parseInt($("#assetList li").css('margin-right'))) * 3;
        $("#assetList li").each(function() {
            if ($(this).offset().top == Otop && $(this).offset().left == Oleft) {
                last = $(this).attr('data-asset-id');
            }
        });
    });
});

Fiddle : http://jsfiddle.net/77xjD/17/


是的!只需要进行一些小修改,还需要减去顶部边框。谢谢。 - Thomas

4

从可见性来看,我认为元素至少应该有右上角是可见的。如果你只想选择完全可见的元素,则需要加上或减去元素的 width()height() 值。基本代码如下:

var $first, $last,
    $container = $("#assetList"),
    $items = $container.children("li"),
    positions = container.offset(),
    rightside = positions.left + $container.width(),
    bottomside = positions.top + $container.height();
$items.each(function(){
    var $this = $(this),
        position = $this.offset();
               // If <li> top post >= <ul> top
    if ($first && position.top >= positions.top
               // If <li> left >= <ul> left
               && positions.left >= position.left) {
             /* Optionally, add two more conditions, to only accept elememts
                which are fully visible: 
               && positions.top + $this.height() <= bottomside
               && positions.left + $this.width() <= leftside
              */
        $first = this;
    }
    // See previous comments.
    if (position.top < bottomside && position.left < rightside) {
        $last = this;
    }
});
// $first = first visible element, eg [HTMLLiElement]
// $last = last visible element, eg [HTMLLiElement]

2
我刚刚更新了您fiddle中的解决方案。
在初始化时,我缓存了第一个li的顶部和最后一个li的顶部位置,并使用它们来找出在单击下一个或上一个按钮时哪个元素正在获取位置。
当然,我从Rob W的答案中复制了以下行。
var $container = $("#assetList"),
$items = $container.children("li"),

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接