简体   繁体   English

监听 iframe 内的鼠标点击和按键事件

[英]Listen for mouse click and keypress events within iframe

I would like to add some explanations to each slide of an embedded swipe.to presentation.我想为嵌入式swipe.to演示文稿的每张幻灯片添加一些解释。 Therefore I am trying to count the times a button within the iframe is pressed or certain keystrokes are done.因此,我试图计算按下 iframe 中的按钮或完成某些击键的次数。 The goal is to determine on which slide the user is in order to display the appropriate slide explanation.目标是确定用户在哪张幻灯片上,以便显示适当的幻灯片说明。

If the user clicks on the link with id #next or presses space bar or right arrow an integer should increment.如果用户单击 ID #next的链接或按空格键或向右箭头,则 integer 应该递增。 If the user clicks on the link with id #previous or presses left arrow the integer should decrease.如果用户单击 ID #previous的链接或按向左箭头,则 integer 应该减少。

Regarding the mouse click events this answer did help me a lot.关于鼠标点击事件,这个答案对我帮助很大。 It works like a charm.它就像一个魅力。 However I am still having a hard time getting the keypress events to work.但是,我仍然很难让按键事件正常工作。

This is what I have got:这就是我得到的:

embedding code嵌入代码

<figure class="swipe">
   <iframe src="https://www.swipe.to/embed/0882x" allowfullscreen></iframe>
</figure>
<style>figure.swipe{display:block;position:relative;padding-bottom:56.25%;height:0;overflow:hidden;}figure.swipe iframe{position:absolute;top:0;left:0;width:100%;height:100%;border:none;}</style>

code for determining slide count确定幻灯片计数的代码

<script>
        $('body iframe').load(function(){
            var i = 0;
            $('body iframe').contents().find('#next').bind('click',function(e) {
                    i++;
                    alert(i);
            });

            $('body iframe').contents().bind('keypress',function(e) {
                    if(e.keyCode == 32){
                        i++;
                        alert(i);
                    }
            });

            $('body iframe').contents().bind('keypress',function(e) {
                    if(e.keyCode == 39){
                        i++;
                        alert(i);
                    }
            });

            $('body iframe').contents().find('#previous').bind('click',function(e) {
                    i--;
                    alert(i);
            });

            $('body iframe').contents().bind('keypress',function(e) {
                    if(e.keyCode == 37){
                        i--;
                        alert(i);
                    }
            });


        });
</script>

It is possible this way:可以这样:

//left arrow
$(document.getElementById('frame-id').contentWindow.document).keydown(function(e){
                if(e.keyCode == 37){
                    i--;
                };
});

//right arrow and space bar
$(document.getElementById('test').contentWindow.document).keydown(function(e){
                if(e.keyCode == 32 || e.keyCode == 39){
                    i++;
                };
});

This should be embedded within $('body iframe').load(function(){ }这应该嵌入$('body iframe').load(function(){ }

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

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