打开键盘箭头按钮单击的下一个模式
Open next modal on keyboard arrow buttons click
我用jQuery创建了一个模态系统。
//Open modal
$('.job_inside').on('click', function(){
var id = $(this).data('id');
$('#'+id).fadeIn(300);
});
//Close modal
$(".close_btn").click(function() {
$(".job_full").fadeOut(300);
});
.html:
<!-- Open modal -->
<div class="job">
<div class="job_inside" data-id="job1"></div>
</div>
<!-- Modal -->
<div class="job_full" id="job1" style="display: none;">
<div class="job_full_inside">
<div class="close_btn">×</div>
<img src="resursai/img/sanfierro.jpg" alt="" />
<h2>SanFierro dizainas</h2>
</div>
</div>
我想说的是,如果打开了模态 id job1,在右箭头键上单击它关闭了 job1 ant打开 job2,在左箭头上单击返回作业 1。这可能吗,我怎么能做到?
对不起语法。
你可以这样做:
.HTML:
<div class="job">
<div class="job_inside" data-id="1">open</div> //notice change here data-id="job1" to "1"
</div>
<!-- Modal -->
<div class="job_full" id="job1" style="display: none;">
<div class="job_full_inside">
<div class="close_btn">×</div>
<h2>First</h2>
</div>
</div>
<div class="job_full" id="job2" style="display: none;">
<div class="job_full_inside">
<div class="close_btn">×</div>
<h2>Second</h2>
</div>
</div>
<div class="job_full" id="job3" style="display: none;">
<div class="job_full_inside">
<div class="close_btn">×</div>
<h2>Third</h2>
</div>
</div>
.JS:
var currentId = null;
$(".job_inside").on("click", function () {
var id = $(this).data("id");
currentId = id;
$("#job" + id).fadeIn(300); //small change here
});
$(document).on("keyup", function (e) {
if (e.which === 37 && currentId > 1) {
currentId--;
$(".job_full").fadeOut(300);
$("#job" + currentId).fadeIn(300);
} else if (e.which === 39 && currentId < 3) {
currentId++;
$(".job_full").fadeOut(300);
$("#job" + currentId).fadeIn(300);
}
});
这是
您要部署的基本策略(实时预览 http://codepen.io/larryjoelane/pen/YwJMPG?editors=1010)。如果您使用已经通过 JQuery 的 eq 函数提供的类名和一个索引变量,您真的不需要担心具有正确 id 的元素的淡入和淡出,当您按下左右箭头时必须递增该索引变量。 您需要使用 keyup 事件上的 what 属性来捕获箭头的键码。以下是一些指向 API 的链接,如果您想了解有关它们的更多信息。
键入事件(向下滚动以查看事件。哪个示例): http://api.jquery.com/keyup/
.HTML:
<!-- Open modal -->
<div class="job">
<div class="job_inside" data-id="job1">Click to load</div>
</div>
<!-- Modal 1-->
<div class="job_full" id="job1" style="display: none;">
<div class="job_full_inside">
<div class="close_btn">×</div>
<img src="resursai/img/sanfierro.jpg" alt="" />
<h2>SanFierro dizainas</h2>
</div>
</div>
<!--Modal 2-->
<div class="job_full" id="job2" style="display: none;">
<div class="job_full_inside">
<div class="close_btn">×</div>
<img src="resursai/img/sanfierro.jpg" alt="" />
<h2>United States</h2>
</div>
</div>
<!--Modal 3-->
<div class="job_full" id="job3" style="display: none;">
<div class="job_full_inside">
<div class="close_btn">×</div>
<img src="resursai/img/sanfierro.jpg" alt="" />
<h2>Canada</h2>
</div>
</div>
JavaScript/JQuery:
(function($) {
//store the index of the job
var index = 0;
//Open modal
$('.job_inside').on('click', function() {
$("#job1").fadeIn(300);
});
$(document).on("keyup", function(e) {
console.log(e.which);
switch (e.which) {
//left arrow
case 37:
console.log("left arrow");
//if the index is not 0
if (index !== 0) {
//decrement it
index--;
}
//close the next job
$(".job_full").eq(index + 1).fadeOut(200);
//load the previous job
$(".job_full").eq(index).fadeIn(300);
break;
//right arrow
case 39:
console.log("right arrow");
//if the index incremented by 1 is less than the length of
//collection
if ((index + 1) < $(".job_full").length) {
//increment the index
index++;
}
//close the previous job
$(".job_full").eq(index - 1).fadeOut(200);
//load the next job
$(".job_full").eq(index).fadeIn(300);
break;
}
});
//Close modal
$(".close_btn").click(function() {
$(".job_full").fadeOut(300);
});
})(jQuery);
相关文章:
- 在单击下一个按钮之前,在JavaScript代码中添加5秒的延迟
- 当我单击下一个或上一个按钮时,datatable显示旧值
- 引导程序在单击另一个下拉列表时关闭下拉列表
- 单击“下一个”和“上一个”时,如何从导航菜单中添加和删除类
- 单击下一个弹出窗口时,第一个弹出窗口不会关闭
- 在使用Jquery/Javascript单击下一个和上一个按钮时动态添加图像
- 如何停止多次单击下一个以破坏我的图像旋转器
- 单击上一个/下一个按钮时,我想突出显示下一个或上一个图像
- 当我单击下一个按钮时,页码按未知顺序升序
- 当我单击下一个/上一个按钮时,如何将活动类更改为 LI 元素
- 如何通过点击“”来获得另一个没有任何文本的重复表单;下一个“;按钮,在javascript中
- 以使下拉列表在单击另一个元素时隐藏
- 当单击下一个菜单项时,防止向下滑动的子菜单向上滑动
- 触摸设备的jQuery下拉导航.单击另一个菜单项时不能隐藏菜单项
- 垂直下拉菜单显示子菜单,当单击另一个菜单项时关闭前一个菜单项
- 单击下一个缩略图时删除内联样式
- Fancybox,如何在单击下一个或上一个时跳过元素
- 在Qualtrics调查中,单击下一个按钮时,如何将对项目的响应保存到嵌入数据中
- 单击下一个菜单标题时取消切换上一个菜单
- 如何等待幻灯片在单击下一个按钮之前切换完成