简体   繁体   English

选择下一个和上一个列表项

[英]Selecting next and previous list items

I am working on a simple image gallery with a larger picture displayed above a line of thumbnails below. 我正在使用一个简单的图像库,其中较大的图片显示在下面的缩略图行上方。 I have set the initial opacity of each thumbnail to 0.3 by using a css rule targeting the li. 我已通过使用针对li的css规则将每个缩略图的初始不透明度设置为0.3。 Using javascript I want to change the opacity of the thumbnail that is currently selected to 1, but keep the rest set at 0.3. 使用javascript,我想将当前选择的缩略图的不透明度更改为1,但将其余部分设置为0.3。

I have managed to change the current thumbnail's opacity from 0.3 to 1 but what I cannot figure out how to do is change the previous (or next) thumbnails opacity back to 0.3. 我设法将当前缩略图的不透明度从0.3更改为1,但是我不知道该怎么做,就是将前一个(或下一个)缩略图的不透明度更改回0.3。

For example, if thumbnail #3 is currently selected I want all the remaining 5 thumbnails to revert back to their orignal opacity setting of 0.3 例如,如果当前选择了缩略图#3,则我希望其余5个缩略图全部恢复为原始不透明度设置0.3

I have put some of my code into the below link so you can get an idea of what I going on about. 我已经将一些代码放入下面的链接中,以便您可以大致了解我的情况。

div class="thumbnails"> div class =“ thumbnails”>

<ul>
    <li><a href='#' class='thumb' id="thumb_1"></a></li>
    <!-- MORE FOLLOW -->
</ul>

$("#nextBtn").click(function() {
    currentPic++;
    $(".thumbnails ul li:nth-child(" + currentPic + ")").animate({
        "opacity": "1"
    });
});

Full code sample: http://jsfiddle.net/nqKJw/ 完整的代码示例: http : //jsfiddle.net/nqKJw/

In the function where you are setting the opacity of the desired thumbnail to 1, first set all thumbnails to an opacity of .3: 在将所需缩略图的不透明度设置为1的功能中,首先将所有缩略图的不透明度设置为.3:

$("#nextBtn").click(function() {
    $(".thumbnails ul li").animate({
        "opacity": "0.3"
    });
    currentPic++;
    $(".thumbnails ul li:nth-child(" + currentPic + ")").animate({
        "opacity": "1"
    });
});

Try it: http://jsfiddle.net/nqKJw/1/ 试试看: http : //jsfiddle.net/nqKJw/1/

Simply change the opacity value of the current item before changing the current item 只需在更改当前项目之前更改当前项目的不透明度值

$("#nextBtn").click(function() {
  $(".thumbnails ul li:nth-child(" + currentPic + ")").animate({
      "opacity": ".3"
  });

  currentPic++;

  $(".thumbnails ul li:nth-child(" + currentPic + ")").animate({
      "opacity": "1"
  });
});

And similarly for prevBtn 同样对于prevBtn

Updated JSFiddle 更新了JSFiddle

Here 'sa nice solution using the .current class 是一个使用.current类的不错的解决方案

CSS: CSS:

.current{ opacity:1 !important; }

Jquery: jQuery:

$("#nextBtn").click(function() {
    var nextThumb = $('.thumbnails .current').removeClass('current').next();
    nextThumb = nextThumb.length?nextThumb:$('.thumbnails li:first');
    $(nextThumb).addClass('current');
});

$("#prevBtn").click(function() {
    var nextThumb = $('.thumbnails .current').removeClass('current').prev();
    nextThumb = nextThumb.length?nextThumb:$('.thumbnails li:last');
    $(nextThumb).addClass('current');
});​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM