简体   繁体   English

div交叉淡入淡出箭头键

[英]div crossfade on arrow keys

I'm familiar with HTML/CSS but am new to the world of Javascript and jQuery, so I've been stumped on this problem. 我熟悉HTML / CSS,但对Javascript和jQuery还是陌生的,所以我对这个问题感到困惑。

I'm trying to fade out the current div which contains an image, and fade in the div that is directly next, here is an example . 我正在尝试淡出包含图像的当前div,然后淡入紧邻的div,这是一个示例 This will be eventually applied to a Tumblr photo blog so I can't target the images individually. 这最终将应用于Tumblr图片博客,因此我无法单独定位图片。

I've tried several different methods and the jQuery code below is the one that I've come closest with however it still skips divs and gets stuck. 我尝试了几种不同的方法,下面的jQuery代码是我最接近的一种,但是它仍然会跳过div并卡住。
All help is greatly appreciated :) 非常感谢所有帮助:)



HTML: HTML:

<div class="content">
    <div class="post"><img src="1.jpg" alt="1"></div>
    <div class="post"><img src="2.jpg" alt="2"></div>
    <div class="post"><img src="3.jpg" alt="3"></div>
</div>


CSS: CSS:

.post{
    position: absolute;
    width: 480px;
    height: auto;
    top: 120px;
    left: 50%;
    margin-left: -240px;
    display: none;
}


jQuery: jQuery的:

$(document).ready(function(){

        $(document).keydown(function(e) {
            switch(e.which) {

                case 37: // left
                        $('.post').fadeOut().prev().fadeIn();
                break;

                case 39: // right
                        $('.post').fadeOut().next().fadeIn();                                   
                break;

             default: return; 
            }
             e.preventDefault(); 
        });

});

See a working demo here 在这里查看有效的演示

You could use indexNum-- and indexNum++ to navigate through images. 您可以使用indexNum-- and indexNum++来浏览图像。

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

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