【问题标题】:Feedback on using jquery mouseover to dynamically change divs关于使用 jquery mouseover 动态更改 div 的反馈
【发布时间】:2013-07-02 17:22:46
【问题描述】:

基本上,我有许多图标,当有人将鼠标悬停在图标上时,我希望这些图标上方的内容发生变化。我目前的计划是使用鼠标悬停来添加/删除一个更改 div 不透明度的类,并让 div 以绝对定位相互重叠。因为我是一名自学成才的开发人员,所以回想起来,我发现我通常会让自己的事情变得比他们需要的更难。 :) 我想我会向我所在的一些社区询问他们对此设置的意见。也许有更好的方法我完全错过了。我需要它兼容回 IE8。

编辑:我使用的是 jQuery 1.8.3。很抱歉漏掉了。

【问题讨论】:

  • 你用的是什么版本的jQuery?
  • 另外,请提醒我们您正在使用的任何其他库/插件(例如 CSS3Pie)
  • 您是否考虑过显示/隐藏或切换?
  • 我刚刚编辑显示。很抱歉把它漏掉了。 jQuery 1.8.3 和 jQuery UI 1.9.2。除此之外,没有其他图书馆。我没有考虑过显示/隐藏切换。我会调查的。我对它不太熟悉。谢谢!

标签: jquery mouseover


【解决方案1】:

你的问题有点含糊。您使用了标签“jquery”,所以我假设您在您的网站上使用了某些版本的 jquery。

我为你做了一个快速的 jsfiddle:http://jsfiddle.net/pMCxP/2/

我没有使用mouseover 事件,而是使用了hover 事件。

$(this).hover(function(){
        $("#content div").hide();
        var contentBlock = $(this).attr('class');
        $("#" + contentBlock).show();
    });

在代码中,我为每个内容块指定了一个 id,然后对于相应的按钮/图标,我将其 css 类设置为与内容的 id 相同。然后我可以用它在悬停时显示块。

我希望这可以帮助您,或者至少为您指明正确的方向。

【讨论】:

  • 感谢您抽出宝贵时间回复和模拟。对此,我真的非常感激。我为含糊而道歉,我现在已经编辑了我的帖子。您是否通过将屏幕阅读器和/或 SEO 设置为 display:none 来预见任何问题?
  • @jphogan ~ 这是个好问题。我通常在封闭的网络站点上工作,因此我不必担心 SEO。快速搜索在 SE 网站 Webmasters 上给了我一个答案。只有在没有显示 div 的机制时才会发生命中,在这种情况下:webmasters.stackexchange.com/questions/1377/…
  • 感谢您的回复并链接到该信息。我很感激!
  • @jphogan ~ NP。 :) 不要忘记接受您的问题的答案之一。
【解决方案2】:

这是一种允许您为每个“图标”鼠标悬停预定义 html 和 css 的方法。

直播版:http://jsfiddle.net/gSAjV/2240/

var content1 = '<p>This is some html that will fill the content window</p>';
var content2 = '<p>This is more html that will fill the content window</p>';
var content3 = '<p>This is other html that will fill the content window</p>';
var content4 = '<p>This is even more html that will fill the content window</p>';

$('#icon1').mouseover(function(){
    $('#content').html(content1);
    $('#content').css( "background-color", "red" );
});
$('#icon2').mouseover(function(){
    $('#content').html(content2);
    $('#content').css( "background-color", "orange" );
});
$('#icon3').mouseover(function(){
    $('#content').html(content3);
    $('#content').css( "background-color", "yellow" );
});
$('#icon4').mouseover(function(){
    $('#content').html(content4);
    $('#content').css( "background-color", "green" );
});

这可行,但据我所知,在更改 div 内容时添加干净的过渡要困难得多。如果您想要淡入淡出或任何其他“A”-“B”类型的过渡,那么最好按照您的建议将所有内容预加载到多个分层 div 上。

也可以只使用两个 div 进行转换。一个 div 用于保存您当前的内容,另一个用于在鼠标悬停时加载新内容,一旦加载,就会淡入(过渡)。

【讨论】:

  • 这太棒了!感谢您的回应和嘲笑。通过使用 javascript 生成 html,您是否认为会对屏幕阅读器产生负面的 SEO 效果和/或后果?不过我喜欢这个解决方案。
  • 是的。该内容不会出现在搜索中,因为它不在页面上,并且屏幕阅读器无法阅读不存在的内容。但话又说回来,鼠标悬停并不是一种完全适合屏幕阅读器的导航方法。
  • 我正在考虑使用外部 js 文件的情况。如果 jquery 在页面本身上,那么我不确定......我相信屏幕阅读器会跳过 &lt;script&gt; 标签,但我不知道搜索引擎是否会读入它们。
  • 最后一个。一些 Javascript/屏幕阅读器建议:stackoverflow.com/questions/3670570/… 一个惊人的 javascript 函数列表以及屏幕阅读器的可访问性:northtemple.com/2008/10/07/javascript-and-screen-readers
【解决方案3】:

我会这样构建

http://jsfiddle.net/UQPe3/1/

HTML

<div class='container'>
    <div class='slide1'>slide1</div>
    <div class='slide2 hidden'>slide2</div>
    <div class='slide3 hidden'>slide3</div>
    <div class='slide4 hidden'>slide4</div>
</div>

<div class='icon-wrapper'>
    <div class='icons'>
        <button type='button' data-slide='slide1'></button>
        <button type='button' data-slide='slide2'></button>
        <button type='button' data-slide='slide3'></button>
        <button type='button' data-slide='slide4'></button>
    </div>
</div>

CSS

.container div {
    background: #999;
    height: 300px;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 20px;
    text-align: center;
    width: 600px;
}

.icon-wrapper {
    text-align: center;
}

.icons {
    display:inline-block;
}

.icons button {
    background-color: #999;
    border: none;
    cursor: pointer;
    height: 60px;
    margin-left: 30px;
    margin-right: 30px;
    width: 60px;
}

.icons button:hover {
    background-color: #333;
}

.hidden {
    display: none;
}

JS

$('.icons button').on(
{
    mouseenter: function()
    {
        $('.container div').addClass('hidden');
        var show_slide = $(this).attr('data-slide');
        $('.' + show_slide).removeClass('hidden');
    }
});

【讨论】:

  • 感谢您的回复和样机。我喜欢这个解决方案。我最初担心将 display:none 用于屏幕阅读器和 SEO 目的,但其他一些发布的有用链接表明这并不像我担心的那样有害。再次感谢!
猜你喜欢
  • 2013-05-30
  • 1970-01-01
  • 2012-05-31
  • 1970-01-01
  • 2011-08-12
  • 2010-09-11
  • 2014-10-19
  • 2011-02-20
  • 1970-01-01
相关资源
最近更新 更多