简体   繁体   English

如何在我的网页上设置旋转器/旋转木马的焦点

[英]how can i set focus of a rotator / carousel on my web page

How can I set focus of a rotator / carousel on my web page 如何在网页上设置旋转器/转盘的焦点

According to my code both the rotators get slided simultaneously but I want only that rotator to slide which is focussed . 根据我的代码,两个旋转器同时滑动,但我只希望聚焦的那个旋转器滑动。

I added the tabindex attribute on the "carousel_inner" div and as well as I also added the tabindex on the "#home_rotator" div and then called the focus function but they are focused simultaneously in my webpage 我在"carousel_inner" div上添加了tabindex属性,并且还在"#home_rotator" div上添加了tabindex,然后调用了focus函数,但是它们同时在我的网页中聚焦

My code is: 我的代码是:

$("#carousel_inner").attr("tabindex",0).focus(function() {
    $(document).keydown(function(eventObject) {
        if(eventObject.which==37) {//left arrow
            $("#carousel_inner").focus();               
            $('#left_scroll').click();//emulates click on prev button 
        } else if(eventObject.which==39) {//right arrow
            $("#carousel_inner").focus();               
            $('#right_scroll').click();//emulates click on next button
        }   
    });
});

// Add keyboard navigation //添加键盘导航

$("#home_rotator").attr("tabindex",-1).focus(function() { 
    $(document).keydown(function(eventObject) {
        if(eventObject.which==37) {//left arrow
            $("#home_rotator").focus();             
            base.goBack();//emulates click on prev button 
        } else if(eventObject.which==39) {//right arrow
            $("#home_rotator").focus();             
            base.goForward();   //emulates click on next button 
        }           
    });
});

You're binding events inside the focus function. 您将事件绑定到焦点函数中。 Once an event is bound, it's bound. 一旦事件绑定,就绑定了。 Unfocusing the element won't unbind the previous events, unless you explicitly tell it to by writing an .unbind() statement in a .blur() event. 使元素失去焦点不会解除绑定先前的事件,除非您通过在.blur()事件中编写.unbind()语句来明确告知它。

But, you'd be better off binding the .keydown() event once , outside of the focus() event, then checking which element has focus and performing the required action. 但是,最好不要在.keydown()事件之外绑定一次 .keydown() focus()事件,然后检查哪个元素具有焦点并执行所需的操作。

$(document).keydown(function(e) { 
    if ( document.activeElement.id === 'carousel_inner' ) {
        if ( e.which === 37 ) {
            // event for left key on carousel_inner
        }
        else if ( e.which === 39 ) {
            // event for right key on carousel_inner
        }
    }
    else if ( document.activeElement.id === 'home_rotator' ) {
        if ( e.which === 37 ) {
            // event for left key on home_rotator
        }
        else if ( e.which === 39 ) {
            // event for right key on home_rotator
        }
    }
});

jsFiddle: http://jsfiddle.net/9ErRF/ jsFiddle: http : //jsfiddle.net/9ErRF/

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

相关问题 jQuery Rotator按字母顺序排列,如何在我的主页上使其随机化? - jQuery Rotator Is Alphabetical, how can I make it randomized on my homepage? 如何将焦点设置在我的网页的一部分然后向下滚动 - How to set focus on a section of my web page then scroll down 如何将焦点设置到页面上的slickgrid控件以启用键盘导航? - How can I set focus to the slickgrid control on my page to enable keyboard navigation? 如何将焦点设置在包含图像或 div 的轮播上,以便我可以使用箭头键导航? - How to set focus on carousel containing image or a div so that i can navigate using arrow keys? 如何使用Javascript在Mozilla的页面开头设置搜索焦点? - How can i set the search focus at the start of the page in Mozilla with Javascript? 如何从页面中找到焦点元素 - How can I find focus element from my page 如何将+按钮移到网页右侧? - How can I move my + button to the right of my web page? 如何使用 jQuery 将焦点设置为 CKEditor 5? - How can I set focus to CKEditor 5 with jQuery? 设置焦点后如何将我的 cursor 放入输入框? - How can I get my cursor into the input box after I set the focus? 如何为网页上的链接设置Accept Header? - How can I set the Accept Header for a link on a web page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM