【问题标题】:css to open details box on thumbnail hover or clickcss 在缩略图悬停或单击时打开详细信息框
【发布时间】:2016-02-22 21:23:29
【问题描述】:

我已经搜索了一段时间,但找不到我要找的东西。想象一排水平的缩略图:如果用户悬停或单击其中一个,则在缩略图行上方打开一个页面范围的 div,以显示所选项目的较大图像和/或文本详细信息等内容。如果用户退出悬停或在拇指行之外单击,则详细信息框会消失。这是整体概念的图像:

据我所知,以下代码是:

<style>
.item_container{float:left;  padding-left: 5px;}
.item_details {border:1px solid #000000;}
.hidden>div {
    display:none;
}

.visible>div {
    display:block;
}

</style>
<script src="jquery/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('.item_container').addClass("hidden");

    $('.item_container').hover(function() {
        var $this = $(this);

        if ($this.hasClass("hidden")) {
            $(this).removeClass("hidden").addClass("visible");

        } else {
            $(this).removeClass("visible").addClass("hidden");
        }
    });
});
</script>

<div class="item_container">
         <div class="item_details">
<img src="images/item/One.jpg" `height="300px" id="pic" />Details for item One </div> <!--div that we want to hide-->` 


        <h3>One</h3>
</div><!--end div_item_container-->
<div class="item_container">
         <div class="item_details">
<img src="images/item/Two.jpg" height="300px"  id="pic" />Details for item Two</div> <!--div that we want to hide-->  
        <h3>Two</h3>
</div><!--end div_item_container-->

问题是 item_details div 在拇指/按钮 div 中打开,而不是在整行上方的大框中。我怀疑这将涉及为每个项目 div 使用不同的 ID(这很好......但我还不知道如何做到这一点)。

感谢您的任何建议!

【问题讨论】:

  • 我对上面的代码做了一个codepen:link

标签: javascript jquery css hover visible


【解决方案1】:

我这里做了一个例子:https://jsfiddle.net/ne57366v/2/

建议是您有缩略图和信息标签的地方,例如:

<div id="info1" class="info">Info 1.</div>
<div id="thumbnail1" class="thumb"> Thumb 1. </div>

然后代码将它们关联起来,例如:

$(".thumb").mouseenter(function() {
  var id = this.id.slice(this.id.length - 1, this.id.length);
  id = "#info" + id;
    $(".info").each(function() {
    $(this).hide();
  });
  $(id).show();
});

抱歉解释不好,但请检查 Fiddle 以完整查看:

  • 您可以点击查看/取消查看信息。
  • 悬停可查看信息。
  • 悬停会隐藏信息。

【讨论】:

  • 这几乎是完美的。如果我从#infoHolder CSS 中取出高度/宽度尺寸,当我没有选择任何内容时,该区域就会消失。无法让代码在 jsfiddle 之外工作......现在正在寻找错别字。
  • 我将选择此作为接受的答案,因为我对代码的理解足以为我的目的修改它。感谢您,也感谢其他所有人的建议!我想我会尝试进一步修改它以弄清楚如何单击打开和关闭而不是依赖悬停。
  • jsfiddle 的右下窗格在 Chrome 中是可以的,但在 Firefox v86 中可悲的是 404,即使我的弹出插件关闭。
【解决方案2】:

如果您对 CSS 版本以及 li 项目和 figure/figcaption 的使用感到好奇。


tabindex 在 HTML + :focus:hover 在 CSS 中。

padding为图片+描述的房间,flex方便传播物品。


.item_container ul {
  width: 640px;
  padding: 500px 10px 10px;
  border: solid;
  margin: 0;
  display: flex;
  position: relative;
  /* scary makeup */
  background: linear-gradient(to bottom, rgba(0, 0, 0, 0.2) 50%, rgba(0, 0, 0, 0.3) 50%) no-repeat 10px 10px, linear-gradient(to right, rgba(0, 0, 0, 0.2) 50%, transparent 50%) no-repeat 10px 10px turquoise;
  background-size: 640px 480px;
}
.item_container {
  margin: 1em;
}
figure {
  position: absolute;
  display: none;
  top: 10px;
  left: 10px;
  ;
  overflow: hidden;
  margin: 0;
}
li {
  flex: 1;
  display: inline-block;
  margin: 0;
  padding: 0;
  text-align: center;
  box-shadow: inset 0 0 0 2px, 0 0 0 1px white;
}
li > img {
  margin: 5px 0 0;
}
li:hover figure,
li:focus figure {
  display: block;
}
li:focus {
  pointer-events: none;
  background:white;
}
figcaption {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  box-shadow: 0 0 3px;
  line-height: 2em;
  padding: 1em;
  background: rgba(255, 255, 255, 0.5);
}
/* set description else where ? */

li:nth-child(odd) figcaption {
  top: 0;
  bottom: 0;
  left: 50%;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
figure img {
  display: block;
}
<div class="item_container">
  <ul>
    <li tabindex="0">
      <img src="http://lorempixel.com/75/75/nature/1" alt="item 1" />Item 1
      <figure>
        <img src="http://lorempixel.com/640/480/nature/1" alt=" nature 1" />
        <figcaption>
          items descriptions
        </figcaption>
      </figure>
    </li>
    <li tabindex="0">
      <img src="http://lorempixel.com/75/75/nature/2" alt="item 2" />Item 2
      <figure>
        <img src="http://lorempixel.com/640/480/nature/2" alt=" nature 2" />
        <figcaption>
          items descriptions
        </figcaption>
      </figure>
    </li>
    <li tabindex="0">
      <img src="http://lorempixel.com/75/75/nature/3" alt="item 3" />Item 3
      <figure>
        <img src="http://lorempixel.com/640/480/nature/3" alt=" nature 3" />
        <figcaption>
          items descriptions
        </figcaption>
      </figure>
    </li>
    <li tabindex="0">
      <img src="http://lorempixel.com/75/75/nature/4" alt="item 4" />Item 4
      <figure>
        <img src="http://lorempixel.com/640/480/nature/4" alt=" nature 4" />
        <figcaption>
          items descriptions
        </figcaption>
      </figure>
    </li>
    <li tabindex="0">
      <img src="http://lorempixel.com/75/75/nature/5" alt="item 5" />Item 5
      <figure>
        <img src="http://lorempixel.com/640/480/nature/5" alt=" nature 5" />
        <figcaption>
          items descriptions
        </figcaption>
      </figure>
    </li>
    <li tabindex="0">
      <img src="http://lorempixel.com/75/75/nature/6" alt="item 6" />Item 6
      <figure>
        <img src="http://lorempixel.com/640/480/nature/6" alt=" nature 6" />
        <figcaption>
          items descriptions
        </figcaption>
      </figure>
    </li>
    <li tabindex="0">
      <img src="http://lorempixel.com/75/75/nature/7" alt="item 7" />Item 7
      <figure>
        <img src="http://lorempixel.com/640/480/nature/7" alt=" nature 7" />
        <figcaption>
          items descriptions
        </figcaption>
      </figure>
    </li>
    <li tabindex="0">
      <img src="http://lorempixel.com/75/75/nature/8" alt="item 8" />Item 8
      <figure>
        <img src="http://lorempixel.com/640/480/nature/8" alt=" nature 8" />
        <figcaption>
          items descriptions
        </figcaption>
      </figure>
    </li>
</div>
<!--end div_item_container-->

http://codepen.io/anon/pen/VeJJMK?editors=1100


http://codepen.io/anon/pen/QyXeER?editors=1100(如果未悬停或单击两次则包装)

【讨论】:

  • 这很好,但它始终保持显示区域完全打开(如果没有单击任何项​​目,我希望它缩小,并允许访问者单击显示区域已关闭)并且似乎无法使显示区域适应可变大小的内容。因为我几乎不知道我在用 CSS 或 JavaScript 做什么,所以我总是喜欢只用一个或另一个来减少我的困惑。不过,这可能是我可以在其他地方使用的东西。谢谢!
  • @phc_joe 如果它被默认关闭和收缩,以及下面的项目,你可能会有一些有趣的跳跃行为codepen.io/anon/pen/QyXeER?editors=1100,除非我完全误解了:)
  • 是的。我在其他几个“悬停”模型中遇到过这种情况,这确实很烦人。我认为最好的方法是悬停(或者更准确地说,鼠标悬停事件会打开完整显示,移动到另一个项目按钮会更改该项目的完整显示。“Mouseout”可能不应该关闭完整显示......也许应该有一个点击按钮来完成。
  • @phc_joe ,全屏显示可以在下面,像折叠菜单一样覆盖其他内容,或者放在一边:)
【解决方案3】:

您可以改为使用属性和传递值来更新您想要显示的信息,例如:

HTML

<div id="displayArea">
<img />
<div class="details"></div>
</div>


<div class="items">
  <div class="item" data-img="one.jpg" data-title="Details for item 1">
    One
  </div>
  <div class="item" data-img="two.jpg" data-title="Details for item number two">
    Two
  </div>
<div>

</div>
</div>

CSS

#displayArea { max-height: 50px; height: 50px; border; 1px solid #000; }

JavaScript

$(document).ready(function(){
    $('.item').hover(function() {
        $('#displayArea img').attr('src',$(this).attr('data-img'));
        $('#displayArea .details').html($(this).attr('data-title'));
        $('#displayArea img').show();
        $('#displayArea .details').show();
    }, function() {
        $('#displayArea img').hide();
        $('#displayArea .details').hide();
    });

});

如果你想在点击事件上运行它,你可以使用这样的东西:

$(document).ready(function(){
     $('.item').on('click',function() {
            $('#displayArea img').attr('src',$(this).attr('data-img'));
            $('#displayArea .details').html($(this).attr('data-title'));
     });
});

你可以在这里看到一个例子:https://jsfiddle.net/9rhLd86j/

【讨论】:

  • 是的。这是在正确的轨道上。我将尝试使用您的代码一段时间,看看 displayArea 是否会完全消失和/或根据每个项目可用的可变内容量适当调整大小。
  • @phc_joe 应该是东方来完成的,你可以添加一些CSS浮动和定位来改变定位。希望有帮助!
  • 这已经非常接近我的需要了,但我认为悬停可能是一种不好的方法(一切都在弹跳,以随意的动作打开/关闭)可以修改脚本以单击打开/关闭。
  • @phc_joe 当然,只需使用点击事件而不是悬停事件。我用一个例子更新了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-20
  • 1970-01-01
  • 1970-01-01
  • 2019-08-04
  • 1970-01-01
相关资源
最近更新 更多