【问题标题】:Use keyboard to hover使用键盘悬停
【发布时间】:2013-10-18 18:18:41
【问题描述】:

我正在构建一个需要通过键盘访问但我不知道如何操作的活动。活动在这里http://jsfiddle.net/shubhjagani/Gsdw2/,我已经让它按我想要的方式工作了。但它只能通过键盘访问。我正在使用图像映射。

HTML

    <img id="beatles" src="http://i.imgur.com/HG4C4f7.jpg" style="width:auto;height:auto;" usemap="#beatles-map">
<map name="beatles-map">
    <area shape="poly" data-name="paul,all" coords="250,9,249,126,371,98" href="#" />
    <area shape="poly" data-name="ringo,all" coords="325,242,173,242,250,125" href="#" />
    <area shape="poly" data-name="john,all" coords="326,242,372,96,248,125" href="#" />
    <area shape="poly" data-name="george,all" coords="128,98,250,9,249,125" href="#" />
    <area shape="poly" data-name="dog,all" coords="126,99,249,125,174,242" href="#" />
</map>
<div id="beatles-caption" style="clear:both;border: 1px solid black; width: 400px; padding: 6px; display:none;">
    <div id="beatles-caption-header" style="font-style: italic; font-weight: bold; margin-bottom: 12px;"></div>
    <div id="beatles-caption-text"></div>
</div>

JavaScript

 // javascript

// Set up some options objects: 'single_opts' for when a single area is selected, which will show just a border
// 'all_opts' for when all are highlighted, to use a different effect - shaded white with a white border
// 'initial_opts' for general options that apply to the whole mapster. 'initial_opts' also includes callbacks
// onMouseover and onMouseout, which are fired when an area is entered or left. We will use these to show or
// remove the captions, and also set a flag to let the other code know if we're currently in an area.

var inArea,
map = $('#beatles'),
    captions = {
        paul: ["Data - Bass Guitar and Vocals",
            "Paul McCartney's song, Yesterday, recently voted the most popular song " + "of the century by a BBC poll, was initially composed without lyrics. " + "Paul used the working title 'scrambled eggs' before coming up with the final words."],
        ringo: ["Organizational Structures - Drums",
            "Dear Prudence was written by John and Paul about Mia Farrow's sister, Prudence, " + "when she wouldn't come out and play with Mia and the Beatles at a religious retreat " + "in India."],
        john: ["Interests - Guitar and Vocals",
            "In 1962, The Beatles won the Mersyside Newspaper's biggest band in Liverpool " + "contest principally because they called in posing as different people and voted " + "for themselves numerous times."],
        dog: ["Woof -Woof"],
        george: ["Relationships - Lead Guitar and Vocals",
            "The Beatles' last public concert was held in San Francisco's Candlestick " + "Park on August 29, 1966."]

    },
    single_opts = {
        fillColor: '000000',
        fillOpacity: 0,
        stroke: true,
        strokeColor: 'ff0000',
        strokeWidth: 4
    },
    all_opts = {
        fillColor: 'ffffff',
        fillOpacity: 0,
        stroke: false,
        strokeWidth: 0,
        strokeColor: 'ffffff'
    },
    initial_opts = {
        mapKey: 'data-name',
        isSelectable: false,
        onMouseover: function (data) {
            inArea = true;
            $('#beatles-caption-header').text(captions[data.key][0]);
            $('#beatles-caption-text').text(captions[data.key][1]);
            $('#beatles-caption').show();
        },
        onMouseout: function (data) {
            inArea = false;
            $('#beatles-caption').hide();
        }
    };
opts = $.extend({}, all_opts, initial_opts, single_opts);


// Bind to the image 'mouseover' and 'mouseout' events to activate or deactivate ALL the areas, like the
// original demo. Check whether an area has been activated with "inArea" - IE<9 fires "onmouseover" 
// again for the image when entering an area, so all areas would stay highlighted when entering
// a specific area in those browsers otherwise. It makes no difference for other browsers.

map.mapster('unbind')
    .mapster(opts)
    .bind('mouseover', function () {
    if (!inArea) {
        map.mapster('set_options', all_opts)
            .mapster('set', true, 'all')
            .mapster('set_options', single_opts);
    }
}).bind('mouseout', function () {
    if (!inArea) {
        map.mapster('set', false, 'all');
    }
});

【问题讨论】:

  • 将 mouseover 改为 focus 或 focusin 和 mouseout 为 blur 或 focusout。
  • 这很整洁。您可以在元素上设置tabindex 吗? w3schools.com/tags/att_global_tabindex.asp
  • @dandavis 我应该将 onMouseover 更改为 focusin 吗?或者只是将鼠标悬停在焦点上?抱歉,我对此很陌生。你能告诉我我必须改变什么才能让它工作吗
  • @MonkeyZeus 我试图设置一个tabindex,但它似乎不起作用

标签: javascript jquery html css keyboard


【解决方案1】:

这绝对不是一个完整的答案,但希望它能为您指明正确的方向(抱歉,我还没有足够的声誉来发布 cmets!)。

$(document).ready(function(){
  $(document).keypress(function(data){
        inArea = true;
        $('#beatles-caption-header').text(captions[data.key][0]);
        $('#beatles-caption-text').text(captions[data.key][1]);
        $('#beatles-caption').show();
    });
});

我收到了要在按键上显示的警报,但我无法准确确定您的代码中需要哪些内容才能使标题弹出。我在 'onMouseover:' 之后找到的这段代码并拼接到 jQuery 代码中用于按键功能触发器,但它并没有像我希望的那样工作。我能够让它显示如下所示的警报,所以我知道它至少在注册按键。

$(document).ready(function(){
  $(document).keypress(function(){
          alert("test");
    });
});

【讨论】:

    猜你喜欢
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 2015-11-23
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 2021-07-26
    相关资源
    最近更新 更多