【问题标题】:Next/Previous Slide on Button click with jQuery Cycle使用 jQuery 循环单击按钮上的下一个/上一个幻灯片
【发布时间】:2013-04-02 21:17:31
【问题描述】:

我不是 jQuery 的天才,我想知道是否有人可以给我一个关于我正在使用的代码有什么问题的指针。滑块可以工作,但我需要按左右键转到下一张和上一张幻灯片。

这很可能是我忽略的一个明显错误。目前,当我按下一个键时,什么也没有发生。

jQuery

        <script>
        $(function() {
            $('.slideshow').cycle({
                fx:'scrollHorz',
                easing:'easeInOutCirc',
                speed:700,
                timeout:5000,
                pager:'.slideshow_wrapper .slideshow-paging'
            });
            $('.slideshow_wrapper').mouseenter(function(){
                $('.slideshow').cycle('pause');
            }).mouseleave(function(){
                $('.slideshow').cycle('resume');
            }).keyup(function(e) {
            if(e.keycode == 37)
                $('.slideshow').cycle('prev');
            if(e.keycode == 39)
                $('.slideshow').cycle('next');
            })
        });
    </script>

HTML

<section id="gallery" class="slideshow_wrapper">
<div class="slideshow-paging"></div>
<div class="slideshow">
    <article class="slideshow_item">
        <div class='image'>
            <a href='#'>
                <img src='[URL HERE]' />
            </a>
        </div>
    </article>
</div>              

【问题讨论】:

  • 它甚至在没有按键事件的情况下循环吗?
  • 对不起。我应该把它包括在内。是的,确实如此。

标签: jquery html jquery-cycle


【解决方案1】:

您已将 keyup 事件处理程序附加到幻灯片包装器,因此需要有焦点才能触发处理程序。而是将其附加到文档中。另外,使用which 代替键码...

$(function() {
    $('.slideshow').cycle({
        fx:'scrollHorz',
        easing:'easeInOutCirc',
        speed:700,
        timeout:5000,
        pager:'.slideshow_wrapper .slideshow-paging'
    });
    $('.slideshow_wrapper').mouseenter(function(){
        $('.slideshow').cycle('pause');
    }).mouseleave(function(){
        $('.slideshow').cycle('resume');
    })
    $(document).keyup(function(e) {
        if(e.which == 37)
            $('.slideshow').cycle('prev');
        if(e.which == 39)
            $('.slideshow').cycle('next');
    });
});

另外,根据您使用的 jQuery 版本,您最好使用on 分配事件处理程序...

$(function() {
    $('.slideshow').cycle({
        fx:'scrollHorz',
        easing:'easeInOutCirc',
        speed:700,
        timeout:5000,
        pager:'.slideshow_wrapper .slideshow-paging'
    });
    $('.slideshow_wrapper').on("mouseenter", function(){
        $('.slideshow').cycle('pause');
    }).on("mouseleave", function(){
        $('.slideshow').cycle('resume');
    })
    $(document).on("keyup", function(e) {
        if(e.which == 37)
            $('.slideshow').cycle('prev');
        if(e.which == 39)
            $('.slideshow').cycle('next');
    });
});

【讨论】:

  • 没问题的伙伴 - 很高兴帮助:)
猜你喜欢
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-02
  • 2012-05-30
相关资源
最近更新 更多