本文介绍了跳到下一个上一个图像(垂直滚动)与jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个线性的图像列表(垂直),我搜索使用键盘上的&向下导航到下一个或上一个图像.我的问题是,我不知道我在滚动条上的位置以及需要滚动到下一张或上一张图像的位置.

I have a linear list of images (vertical) and I search to use the keyboard up & down to navigate to next or prev images.My problem is that I don't know where I'm on the scroll and where I need to scroll to go to the next or prev image.

我看到了此示例,但它是水平的.我试图使其垂直,但没有成功……

I saw this example but it's in horizontal. I tried to make it vertical, but without success…

我也尝试了inView jQuery插件的一些尝试,但也没有成功.一些开始:

I also tried some things with the inView jQuery plugin, without success too.Some start :

  $("body:not(.photo) #images img").each(function() {
     $(this)
        .css({cursor: "pointer"})
        .on("click", function() {
           var $n = $(this).next(),
               offset = ($n.length) ? $n.offset().top : 0;

           scrollTo(offset);
        });
  });

  // http://jsfiddle.net/JjhUN/5/
  var elems = [];
  $("#images").children().each(function() {
     elems.push(this.offsetTop);
  });

  console.log(elems);

推荐答案

我已经使用了该水平示例,并将其更改为垂直示例. http://jsfiddle.net/6gCA6/1/

I have played with that horizontal example, and change it to vertical. http://jsfiddle.net/6gCA6/1/

我希望这会有所帮助.

JS

$(function() {
    var boxLefts = [];
    $('.box').each(function(i, el){
        boxLefts.push(this.offsetTop);
    });

    $(document).keydown(function(e) {
        var dir = false,
            targetUp = -1;

        switch (e.keyCode) {
        case 38:
            dir = -1;
            break;
        case 40:
            dir = 1;
            break;
        default:
            break;
        }

        if (dir) {
            e.preventDefault();
            winUp = window.scrollY;
            $.each(boxLefts, function(i, v){
                if ((dir == 1 && winUp < v && targetUp < 0) ||
                    (dir == -1 && winUp > v)) {
                    targetUp = v;
                }
            });
            if (targetUp >= 0) {
                $('html, body').animate({scrollTop: targetUp}, 1000);
            }
        }
    });
});

CSS

#wrapper {
    white-space: nowrap;
    width: 1500px;
}

.box {
    width: 200px;
    height: 200px;
    border: 1px solid red;
    margin: 0 10px 0 0;
    /*display: inline-block;*/
}

这篇关于跳到下一个上一个图像(垂直滚动)与jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 08:53