幻灯片与图库导航相冲突

时间:2015-08-04 14:29:01

标签: javascript html

所以我不是一个经验丰富的编码员,但我想把我的小画廊放在一起。我有一个自动幻灯片,每7秒更换一次图片,图片两侧的箭头可以浏览幻灯片。它的工作正常,除非我希望7秒计数器重置,如果图像被更改,我似乎无法弄明白。

我的HTML

<div class="slide" id="slide1" style="background-image: url(images/pattern.png), url(images/header.jpg);">
    <section id="header" class="dark">
        <header>
            <h1>title</h1>
                <div>
                <div style="float: left"><a href="#" onClick="MM_showHideLayers('slide3','','show','slide2','','hide','slide1','','hide')"><img src="prevImgBtn.png"></div><div style="float: right;"><img src="nextImgBtn.png"></div>
                <div style="clear: both;">
                </div>
            <p>Description text</p>
        </header>
        <footer>
            <a href="externallink.com" target="_blank" class="button">Check out</a>
        </footer>
        <div class="arrow-wrapper"><a  class="arrow" href="#work"><img border="0" width="5%" height="5%" src="images/arrow.svg" /></a></div>
    </section>
</div>
<div class="slide" id="slide2" style="background-image: url(images/pattern.png), url(images/header2.jpg);">
    <section id="header2" class="dark">
        <header>
            <h1>Title 2</h1>
                <div>
                <div style="float: left"><a href="#" onClick="MM_showHideLayers('slide1','','show','slide2','','hide','slide3','','hide')"><img src="prevImgBtn.png"></div><div style="float: right;"><img src="nextImgBtn.png"></div>
                <div style="clear: both;">
                </div>
                <p>Description.</p>
        </header>
        <footer>
            <a href="anotherlink.com" target="_blank" class="button scrolly">Know More</a>
        </footer>
        <div class="arrow-wrapper"><a  class="arrow" href="#work"><img border="0" width="5%" height="5%" src="images/arrow.svg" /></a></div>
    </section>
</div>
<div class="slide" id="slide3" style="background-image: url(images/pattern.png), url(images/header3.jpg);">
    <section id="header2" class="dark">
        <header>
            <h1>Title 3</h1>
                <div>
                <div style="float: left"><a href="#" onClick="MM_showHideLayers('slide2','','show','slide1','','hide','slide3','','hide')"><img src="prevImgBtn.png"></div><div style="float: right;"><img src="nextImgBtn.png"></div>
                <div style="clear: both;">
                </div>
            <p>yet another description</p>
        </header>
        <footer>
            <a href="biography.html" class="button scrolly">Know More</a>
        </footer>
        <div class="arrow-wrapper"><a  class="arrow" href="#work"><img border="0" width="5%" height="5%" src="images/arrow.svg" /></a></div>
    </section>
</div>

和JS

</script>
<script type="text/javascript">
jQuery(function () {
    var $els = $('div[id^=slide]'),
        i = 0,
        len = $els.length;

    $els.slice(1).hide();
    setInterval(function () {
        $els.eq(i).fadeOut(function () {
            i = (i + 1) % len
            $els.eq(i).fadeIn();
        })
    }, 7000)
})
</script>
<script type="text/javascript">
function MM_showHideLayers() { //v9.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'block':(v=='hide')?'none':v; }
    obj.display=v; }
}
</script>

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

将间隔分配给var,并在图像更改时清除间隔并再次设置:

var my7sInterval = setInterval(function () {
    $els.eq(i).fadeOut(function () {
        i = (i + 1) % len
        $els.eq(i).fadeIn();
    })
}, 7000)

//on image change
clearInterval(my7sInterval);
//.. set the interval again using the above

或......

设置一个将倒数秒的间隔,并在计数达到7时切换。在图像更改时,您只需重置计数器:

var s7Timeout = 0;
setInterval(function () {
    if (s7Timeout >= 7) {
        s7Timeout = 0;
        $els.eq(i).fadeOut(function () {
            i = (i + 1) % len
            $els.eq(i).fadeIn();
        })
    } else {
        s7Timeout++;
    }
}, 1000)

//and on image change reset the counter:
s7Timeout=0;