简体   繁体   English

我希望我的引导程序轮播在按下键盘左右箭头时滑动

[英]I want my bootstrap carousel to slide when keyboard left and right arrows are pressed

I have a carousel with images inside a modal window.我在模态窗口中有一个带有图像的轮播。 I want the images to slide back and forth when I press the left/right arrow keys on the keyboard.当我按下键盘上的左/右箭头键时,我希望图像来回滑动。 According to the bootstrap carousel documentation the data-keyboard attribute default value is true, but nothing happens when I press the arrow keys!根据 bootstrap carousel 文档,data-keyboard 属性默认值为 true,但是当我按下箭头键时没有任何反应! So I tried putting the attribute in the code and still nothing happens when I press left/right arrow keys.所以我尝试将属性放入代码中,但当我按左/右箭头键时仍然没有任何反应。

Question - Should the pics slide back and forth automatically with a left/right keyboard press?问题 - 图片是否应该通过左/右键盘按压自动来回滑动? If yes, then I have something messed up, if no, do I need some JS carousel event captured and then slide manually with js/jquery?如果是,那么我搞砸了,如果不是,我是否需要捕获一些 JS 轮播事件,然后使用 js/jquery 手动滑动?

Here is my code!这是我的代码!

 <div class="modal" id="profileImageModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close">@*<span aria-hidden="true">&times;</span>*@<i class="fa fa-flag-o" aria-hidden="true"></i> </button> <h4 class="modal-title">Image Removal</h4> </div> <div class="modal-body" style="text-align: center;"> <div id="carousel-example-generic" data-interval="false" data-keyboard="true" class="carousel slide"> <!-- Indicators --> <ol class="carousel-indicators"> @for (int i = 0; i < Model.YogaProfileImages.Count(); i++) { var profileImageId=@ Model.YogaProfileImages.ElementAt(i).YogaProfileImageId; <li data-target="#carousel-example-generic" data-slide-to="@i" class="@(ViewBag.SelectedImageId == profileImageId ? " active " : " ")"> </li> } </ol> <!-- Wrapper for slides --> <div class="carousel-inner" role="listbox"> @for (int i = 0; i < Model.YogaProfileImages.Count(); i++) { var profileImageId=@ Model.YogaProfileImages.ElementAt(i).YogaProfileImageId; <div id=@profileImageId class="item @(ViewBag.SelectedImageId == profileImageId ? " active " : " ")" data-profileId="@Model.Id"> <img style="display: block; margin: auto; border-radius: 6px;" src="data:image/jpg;base64, @(Html.Raw(Convert.ToBase64String(Model.YogaProfileImages.ElementAt(i).CroppedImage)))" alt="profile image"> </div> } </div> </div> </div> <div class="modal-footer" style="text-align: center; border-top: none;"> <input id="deleteModalWarningClose" type="button" class="btn btn-lg btn-primary" value="Close" /> </div> </div> </div> </div>

Please make your code example work to get a better answer.请让您的代码示例工作以获得更好的答案。 But upto now it seems like this could help you:但到目前为止,这似乎可以帮助您:

$(document).keydown(function(e) {
    if (e.keyCode === 37) {
       // Previous
       $(".carousel-control.left").click();
       return false;
    }
    if (e.keyCode === 39) {
       // Next
       $(".carousel-control.right").click();
       return false;
    }
});

I would use some jquery event listenner such as .on('keyup', somefunction()) see documentation here and in this function if right arrow was pressed I would increment an index that would "lead" to the image I would want my carousel to show.我会使用一些 jquery 事件监听器,比如 .on('keyup', somefunction())在这里查看文档,在这个函数中,如果按下右箭头,我会增加一个索引,该索引会“导致”图像我想要我的轮播显示。 The same thing applies to left arrow being pressed but in this situation I would decrease the index.同样的事情适用于被按下的左箭头,但在这种情况下我会减少索引。 If I didn't get your problem correctly let me know.如果我没有正确解决您的问题,请告诉我。

For me this worked =>对我来说这有效 =>

<script>
    $(".carousel").carousel({
        interval: 6000,
        keyboard: true
    });
</script>

But it only worked when i have added left & right control arrows on the image and pressed one of the arrows at least once.但它仅在我在图像上添加左右控制箭头并至少按下其中一个箭头一次时才起作用。 After that i could use the left and right arrows on the keyboard to slide the images.之后我可以使用键盘上的左右箭头来滑动图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM