admin管理员组文章数量:1401470
I would like to add some explanations to each slide of an embedded swipe.to presentation. Therefore I am trying to count the times a button within the iframe is pressed or certain keystrokes are done. 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. If the user clicks on the link with id #previous
or presses left arrow the integer should decrease.
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="" 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>
I would like to add some explanations to each slide of an embedded swipe.to presentation. Therefore I am trying to count the times a button within the iframe is pressed or certain keystrokes are done. 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. If the user clicks on the link with id #previous
or presses left arrow the integer should decrease.
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>
Share
Improve this question
edited May 23, 2017 at 12:06
CommunityBot
11 silver badge
asked Oct 12, 2014 at 10:33
NicoNico
1611 gold badge2 silver badges12 bronze badges
2
- you can't access iFrame that is on different domain – charlietfl Commented Oct 12, 2014 at 11:54
- 1 But why does it work for clicks on the next/previous buttons? – Nico Commented Oct 12, 2014 at 12:00
1 Answer
Reset to default 4It 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(){ }
本文标签: javascriptListen for mouse click and keypress events within iframeStack Overflow
版权声明:本文标题:javascript - Listen for mouse click and keypress events within iframe - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744282908a2598740.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论