From 7c198a04f176ecce5efe46e98a38088d434bd637 Mon Sep 17 00:00:00 2001 From: Anthony Williams Date: Mon, 22 Oct 2012 15:26:59 +0100 Subject: [PATCH] Prevent page scrolling when carousel is focused Fixes an issue where the page scrolled on key presses, despite the fact that the carousel had focus. --- Carousel/abitgone-carousel.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Carousel/abitgone-carousel.js b/Carousel/abitgone-carousel.js index 419afb5..7ba8ed7 100644 --- a/Carousel/abitgone-carousel.js +++ b/Carousel/abitgone-carousel.js @@ -120,6 +120,7 @@ // Keyboard Events if (!$carousel.is("[data-carousel-nokeyboard]") && !no_pagers) { $carousel.attr("tabindex", "0"); + $carousel.on('keydown', $.proxy(this.keydown, this)); $carousel.on('keyup', $.proxy(this.keymove, this)); } @@ -236,9 +237,9 @@ var $carousel = $(e.target).parents('[data-carousel]'); } , change_position: function($carousel, $items, item_index) { - + var $container = $carousel.find("[data-carousel-items]"); - + $items.removeClass("active-item"); $($items[item_index]).addClass("active-item"); $carousel.attr("data-carousel-item", item_index); @@ -259,10 +260,23 @@ $($carousel_pages[item_index]).addClass("active-page"); } } + , keydown: function (e) { + switch(e.keyCode) { + case 37: + case 38: + case 39: + case 40: + e.preventDefault(); + break; + default: + return; + break; + } + } , keymove: function (e) { var $carousel = this.$carousel; var $items = this.$items; - + var item_index = parseInt($carousel.attr("data-carousel-item")) , item_count = parseInt($carousel.attr("data-carousel-items")) , item_index_increment;