gpt4 book ai didi

javascript - 将键盘控件添加到 slider

转载 作者:行者123 更新时间:2023-11-30 14:06:37 25 4
gpt4 key购买 nike

我有一个用于滚动浏览产品图片的灯箱 slider ,我希望用户能够使用键盘上的箭头键滚动浏览图片,而不必单击箭头并使用 ESC键关闭灯箱。

灯箱 slider 代码:

  // T50 Lightbox
// Open the Modal
function openModal() {
document.getElementById('T50Lightbox').style.display = "block";
}

// Close the Modal
function closeModal() {
document.getElementById('T50Lightbox').style.display = "none";
}

var slideIndex = 1;
showSlides(slideIndex);

// Next/previous controls
function plusSlides(n) {
showSlides(slideIndex += n);
}

// Thumbnail image controls
function currentSlide(n) {
showSlides(slideIndex = n);
}

function showSlides(n) {
var i;
var slides = document.getElementsByClassName("T50-Slides");
var dots = document.getElementsByClassName("demo");
var captionText = document.getElementById("caption");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
captionText.innerHTML = dots[slideIndex-1].alt;
}

我有以下代码可以使用 ESC 键关闭菜单,如何将它与箭头键一起应用于灯箱?

    function closeMenu() {
$(".menu-trigger").removeClass("open");
$(".navigation").removeClass("nav-open");
$(".col").removeClass("c-in");
}

$(document).keyup(function(e) {
if (e.keyCode == 27) {
closeMenu();
}
});

最佳答案

您可以像为 esc 键添加监听器一样为箭头键添加监听器。箭头键的键码是:-

left arrow  37
up arrow 38
right arrow 39
down arrow 40

您可以编写以下代码:-

$(document).keyup(function(e) {
if (e.keycode === 27) {
closemenu();
}
if(e.keycode === 37) {
// code for showing next slide
plusSlides(n)
}
// same for other keys
}

关于javascript - 将键盘控件添加到 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55259623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com