glide icon indicating copy to clipboard operation
glide copied to clipboard

Navigating with arrow keys affects all carousels, not just the carousel in focus

Open cewen opened this issue 7 years ago • 8 comments

I'm creating a few Glide sliders on the page like so:

import Glide from '@glidejs/glide';

const sliders = document.querySelectorAll('.glide');

sliders.forEach((slider) => {
  new Glide(slider).mount();
});

The sliders all work perfectly well independently, but when I try to focus on one and navigate with the keyboard, all of the sliders are affected and change to either the next or previous slide.

Pressing the arrow keys should only affect the slider that the user is focused into.

From inspecting the source code it looks like this may be because Glide is attaching a keyup listener on the document element, rather on each individual .glide element.

cewen avatar Aug 16 '18 20:08 cewen

I'd also love to know a fix for this one!

cylott avatar Sep 16 '19 19:09 cylott

Disable keyboard events globally for Glide and then attach specific events when you mount the individual sliders.

import Glide from '@glidejs/glide';

const sliders = document.querySelectorAll('.glide');

let sliderObj = {};

sliders.forEach((slider, i) => {
    // Add glide to the object for reference, disable global keyboard
    sliderObj[i] = new Glide(slider, {
        type: 'carousel',
        keyboard: false
    });
    // Attatch event listener and instruct slide to go left and right
    slider.addEventListener('keyup', (e) => {
        if (e.keyCode == 39) this.sliderObj[i].go('>');
        if (e.keyCode == 37) this.sliderObj[i].go('<');
    });
    // Mount
    sliderObj[i].mount();
});

ghost avatar Oct 08 '19 08:10 ghost

@shane-sigma thanks for this code. It took me forever to figure out that the div doesn't get focus until you click on it. This works very well. For users wanting the focus to happen on hover, try this:

$('mydiv').hover(function () {this.focus({preventScroll: true}); });
$('mydiv').keyup(function (e) {
	if (e.keyCode == 39)= mydiv.go('>');
	if (e.keyCode == 37) mydiv.go('<');
	});

cylott avatar Oct 23 '19 08:10 cylott

However this is still a workaround, not a fix.

poveu avatar Nov 26 '19 09:11 poveu

However this is still a workaround, not a fix.

A good workaround

cylott avatar Nov 26 '19 09:11 cylott

However this is still a workaround, not a fix.

A good workaround

Haha I'm not saying it's not! :P Just pointing out that this should be fixed in future releases.

poveu avatar Nov 26 '19 10:11 poveu

Still having this issue in April 2021. The glide carousel will move regardless of where hover or focus is on the page if the arrow keys are pressed. It is conflicting with my Bootstrap 4 carousel on the same page.

tannermann avatar Apr 20 '21 16:04 tannermann

Mmmh, using keyup eventListener will let me use keyboard. I have to click on the carousel to get he keyboard enabled.

Did someone had the same problem and fixed it?

Thanks

Ceyar avatar Jul 20 '21 08:07 Ceyar