【问题标题】:Bootstrap: Carousel with keyboard controlsBootstrap:带键盘控制的轮播
【发布时间】:2016-07-01 04:41:05
【问题描述】:

有没有人能够使用键盘控件实现 Twitter Bootstrap 轮播?我知道这将在下一个版本中实现,但现在我想知道你们中是否有人能够让它工作。

这是我当前的代码:

<script type="text/javascript">

  jQuery(document).keypress(function(event) {

  if (event.keyCode === RIGHT_ARROW) {
  $('a.carousel-control.right').trigger('click');

  }

  if (event.keyCode === LEFT_ARROW) {

  $('a.carousel-control.left').trigger('click');

  }

  });

</script>

但我对此无能为力。有什么想法吗?

编辑:这是我正在运行的 Wordpress 代码...

                        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                        <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">


                            <?php if ( $post->post_type == 'portfolios' && $post->post_status == 'publish' ) {
                            $attachments = get_posts( array(

                            'post_type' => 'attachment',
                            'posts_per_page' => -1,
                            'post_parent' => $post->ID,
                            'exclude' => get_post_thumbnail_id(),
                            'orderby' => 'menu_order',
                            'order' => 'ASC'    
                                ) );

                            ?>

                            <?php if ( $attachments ) {  
                            $the_rest = array_slice($attachments, 0, 100);
                            ?>      

                            <div id="carousel-<?php the_ID(); ?>" class="featured-carousel carousel slide carousel-fade">

                                <div class="carousel-inner">

                                    <?php 
                                    global $post;
                                    $post_num = 0;  
                                    foreach( $the_rest as $attachment) :
                                    $image = wp_get_attachment_image_src( $attachment->ID, 'orion-thumb-900', false );
                                    $post_num++;
                                    ?>          

                                    <div class="item <?php if($post_num == 1){ echo 'active'; } ?>">
                                    <?php echo "<img src='" . $image[0] . "'>"; ?>  
                                      <div class="container">
                                      </div> <!-- /.container -->
                                    </div> <!-- /.item -->

                                    <?php endforeach; ?>          

                                    <?php } ?>

                                    <?php } ?>      

                                </div> <!-- /.carousel-inner -->

                                    <a class="left carousel-control" href="#carousel-<?php the_ID(); ?>" data-slide="prev">&lsaquo;</a>
                                    <a class="right carousel-control" href="#carousel-<?php the_ID(); ?>" data-slide="next">&rsaquo;</a>

                            </div> <!-- /.carousel -->

                                <section class="entry-content clearfix">

                                    <?php the_content(); ?>

                                    <?php orion_related_posts(); ?>

                                </section> <!-- end article section -->

                        </article> <!-- end article -->

                    <?php endwhile; ?>  

【问题讨论】:

  • 我可以看看你在哪里定义 LEFT_ARROW 和 RIGHT_ARROW 吗?
  • @flemingslone 嗨,我已经用我正在使用的当前代码更新了这个问题。旋转木马本身在工作,箭头在使用鼠标时有效,但在键盘上无效。
  • 你的变量 RIGHT_ARROW 和 LEFT_ARROW 等于什么是我认为 flemingslone 所要求的
  • @DavidChase 您好,David,这只是我从另一个站点获得的示例代码,我不知道它是否正确。
  • 你需要改变数字左箭头等于37,右箭头等于39

标签: wordpress twitter-bootstrap keyboard carousel


【解决方案1】:

谢谢你,

轮播事件和设备支持会更好 - 现在“点击”很糟糕!

    $(document).bind('keyup', function(e) {
        if(e.which == 39){
            $('.carousel').carousel('next');
        }
        else if(e.which == 37){
            $('.carousel').carousel('prev');
        }
    });

【讨论】:

    【解决方案2】:

    这是正确的代码,感谢 DavidChase 和 Flemingslone!

    <script type="text/javascript">
    
    jQuery(document).bind('keyup', function(e) {
    
      if (e.keyCode==39) {
          jQuery('a.carousel-control.right').trigger('click');
      }   
    
      else if(e.keyCode==37){
          jQuery('a.carousel-control.left').trigger('click');
      }
    
    });
    
    </script>
    

    【讨论】:

    • 由于您使用的是 jQuery 及其标准化,您可以使用 event.which == 39e.which == 39 代替 event.keyCode :)
    猜你喜欢
    • 2019-04-27
    • 2019-05-08
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    相关资源
    最近更新 更多