单击图像周围的空白空间时出现Javascript幻灯片故障

时间:2015-07-12 12:26:49

标签: javascript html css function slideshow

所以我让这个幻灯片代码工作,当你点击一个图像时,它会淡入另一个图像。但是,例如,如果右侧有一个带有空白空间的垂直方向图像,如果单击该空格,整个幻灯片显示会出现故障。

在我的网站上您可以测试它:

http://danielshultz.github.io

代码:

$(document).ready(function() {
$.fn.nextOrFirst = function (selector) {
    var next = this.next(selector);
    return (next.length) ? next : this.prevAll(selector).last();
};
$("#cf2 img").click(function() {
    $(this)
    .removeClass('visible')
    .nextOrFirst()
    .addClass('visible');
  });
});

CSS:

#cf2 {
  position:relative;
  height:281px;
  width:450px;
  margin:0 auto;
}
#cf2 img {
  position:absolute;
  left:0;
  max-height: 600px;
  max-width: 600px;
  opacity: 0;
  z-index: 0;
  -webkit-transition: opacity 1s ease-in-out;
  -moz-transition: opacity 1s ease-in-out;
  -o-transition: opacity 1s ease-in-out;
  transition: opacity 1s ease-in-out;
}

#cf2 img.visible {
  opacity: 1;
  z-index: 1;
}

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="cf2" class="shadow">
  <img class="visible" src="http://static.ddmcdn.com/gif/storymaker-best-hubble-space-telescope-images-20092-514x268.jpg" alt="1"/>
  <img src="http://economictimes.indiatimes.com/thumb/msid-45891755,width-640,resizemode-4/nasas-images-of-most-remarkable-events-you-cant-miss.jpg" alt="2"/>
  <img src="http://i.dailymail.co.uk/i/pix/2013/11/03/article-2486855-192ACC5200000578-958_964x682.jpg" alt="3"/>
  <img src="http://mstatic.mit.edu/nom150/items/199-HybridImage.jpg" alt="4"/>
</div>

1 个答案:

答案 0 :(得分:2)

如果我理解,这里的问题是当您在图像外部点击但在上一张图像的正方形内部时,则幻灯片不会改变

这种方法不会改变你的javascript,但会更改html和一些选择器。

在下面的示例中,我将每个<img>包装到'<div>'中,并更改了选择器以匹配这些分区。小样式也是。

因此,如果您单击图像外部但在div上方,幻灯片会按预期更改。

&#13;
&#13;
$(document).ready(function() {
    $.fn.nextOrFirst = function (selector) {
        var next = this.next(selector);
        return (next.length) ? next : this.prevAll(selector).last();
    };
    $("#cf2 div.holder").click(function() {
        $(this)
        .removeClass('visible')
        .nextOrFirst()
        .addClass('visible');
    });
});
&#13;
body {
	font-family: verdana;
	font-size: 8pt;
	color: #000;
}

#cf2 {
    position: relative;
    height: 600px;
    width: 600px;
    margin: 0 auto;
}
#cf2 div.holder {
    position: absolute;
    left: 0;
    height: 600px;
    width: 600px;
    opacity: 0;
    z-index: 0;
    -webkit-transition: opacity 1s ease-in-out;
    -moz-transition: opacity 1s ease-in-out;
    -o-transition: opacity 1s ease-in-out;
    transition: opacity 1s ease-in-out;
}

#cf2 div.holder img {
    max-height: 600px;
    max-width: 600px;
}

#cf2 div.holder.visible {
    opacity: 1;
    z-index: 1;
}


.hidden {
    display: none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>

<td valign="center">
<div id="cf2" class="shadow">
<div class="holder visible">
<img src="//danielshultz.github.io/Images/Cute-Door-1.jpg" alt="1"/></div>
<div class="holder"><img src="//danielshultz.github.io/Images/Cute-Door-2.jpg" alt="2"/></div>
<div class="holder"><img src="//danielshultz.github.io/Images/Cute-Door-3.jpg" alt="3"/></div>
</div>
</td>
</table>
&#13;
&#13;
&#13;