简体   繁体   English

将鼠标悬停在同级上可停止悬停效果

[英]Hovering over sibling stops hover effect

So I have two colour images that when hovered will turn the other to greyscale (there is a black and white copy of each image sitting directly behind) by changing the opacity to 0. This works fine. 因此,我有两个彩色图像,将它们悬停时可以将不透明度更改为0,从而将另一个图像变成灰度级(每个图像都有一个黑白副本直接位于后面)。这很好用。

I'm now trying to display a caption over the hovered image (when its hovered). 我现在正试图在悬停的图像上显示标题(悬停时)。 What happens is that when I hover over the image, the caption appears just fine. 发生的事情是,当我将鼠标悬停在图像上时,标题似乎很好。 If the mouse moves over the caption though, the caption just constantly disappears and reappears as I'm no longer hovering over the image itself. 如果鼠标移到标题上方,则标题会不断消失并重新出现,因为我不再将鼠标悬停在图像本身上。 When the caption disappears I'm hovering on the image again so it reappears. 当标题消失时,我再次在图像上徘徊,使其重新出现。

What I'd like to happen is that when I hover over this caption it behaves as though I'm still hovering the image. 我想发生的是,当我将鼠标悬停在此标题上时,其行为就像我仍在悬停图像一样。 I would also accept displaying the caption over the image permanently as an alternative (might even be better in the long run to be honest). 我也可以选择永久显示图片上方的标题(从长远来看,甚至可能会更好)。

I've looked into the event.stopPropagation() method but that seems to prevent effects moving up the DOM or something to that effect and this caption is a sibling. 我已经研究了event.stopPropagation()方法,但这似乎阻止了效果在DOM或达到该效果的东西的移动,并且此标题是同级的。

Code is as follows 代码如下

HTML 的HTML

<div id="leftbox">
  <div id="upper" class="cap">
    <div class="gallery">
      <ul>
        <li>
          <a href="link.html">
            <img src="images/img1_color.jpg" class="color" alt="a" id="img1>
          </a>
        </li>
        <li>
          <a href="link.html">
            <img src="images/img1_dark.jpg" class="dark" alt="a">
          </a>
        </li>
      </ul>
    </div>
    <div id="caption1">
    </div>
  </div>
  <div id="lower" class="cap">
    <div class="gallery">
      <ul>
        <li>
          <a href="link2.html">
            <img src="images/img2_color.jpg" class="color" alt="b" id="img2>
          </a>
        </li>
        <li>
          <a href="link2.html">
            <img src="images/img2_dark.jpg" class="dark" alt="b">
          </a>
        </li>
      </ul>
    </div>
    <div id="caption2">
    </div>
  </div>
</div>              

CSS 的CSS

.gallery li {
  list-style-type: none;
  position: relative;
}

img.dark {
  position: absolute;
  left: 0;
  top: 0;
}

img.color {
  position: absolute;
  left: 0; 
  top: 0;
  z-index: 1;
}

#leftbox {
  float: left;
  width: 450px;
  margin: 5px 10px 0px 0px;
}

#leftbox #upper {
  width: 450px;
  height: 250px;
  margin: 0px 0px 10px 0px;
}

#leftbox #lower {
  width: 450px;
  height: 250px;
  margin: 0px 0px 5px 0px;
}

.gallery  #capimg1{
  height: 20px;
  text-align: center;
  color: #ffffff;
  background-color: green;
  z-index:2;
  display: none;
  position: absolute;
  padding: 5px 100px 5px 100px;
}

.gallery  #capimg2{
  height: 20px;
  text-align: center;
  color: #ffffff;
  background-color: green;
  z-index:2;
  display: none;
  position: absolute;
  padding: 5px 100px 5px 100px;
}

Javascript Java脚本

window["img1"] = "#img1";
window["img2"] = "#img2";

$(document).ready(function(){
  $('img.color').hover(function(ev) {
    if (this.id == "img1") {
      $(img2).stop().animate({"opacity": "0"}, 600);
    }
    else if (this.id == "img2") {
      $(img1).stop().animate({"opacity": "0"}, 600);
    }

    $('#cap'+this.id).stop().fadeIn(400).html(this.alt);
  },
  function reAnim() {
    $([img1, img2].join(",")).stop().animate({"opacity": "1"}, "slow");
    $('#cap'+this.id).css("display", "none");       }
  );
});

Side question: Is there a way to make the #capimg1 and #capimg2 divs the same width as the gallery div? 附带问题:是否可以使#capimg1和#capimg2 div的宽度与图库div的宽度相同? If not I'll just create a div for each image and hardcode the width in. 如果没有,我将为每个图像创建一个div并对其宽度进行硬编码。

Thanks for help 感谢帮助

Change your javascript as such: 像这样更改您的javascript:

$(document).ready(function(){
  $('img.color').parents('div.cap').hover(function(ev) {
    if ($(this).find('img.color')[0].id == "img1") {
      $(img2).stop().animate({"opacity": "0"}, 600);
    }
    else if (this.id == "img2") {
      $(img1).stop().animate({"opacity": "0"}, 600);
    }

    $('#caption_'+$(this).find('img.color')[0].id).stop().fadeIn(400).html($(this).find('img.color')[0].alt);
  },
  function reAnim() {
    $([img1, img2].join(",")).stop().animate({"opacity": "1"}, "slow");
    $('#caption_'+$(this).find('img.color')[0].id).css("display", "none");
  });
});

And then change your HTML to this: 然后将您的HTML更改为此:

<div id="upper" class="cap">
  <div class="gallery">
    <ul>
      <li>
        <a href="link.html">
          <img src="images/img1_color.jpg" class="color" alt="a" id="img1>
        </a>
      </li>
      <li>
        <a href="link.html">
          <img src="images/img1_dark.jpg" class="dark" alt="a">
        </a>
      </li>
    </ul>
  </div>
  <div id="caption_img1">
  </div>
</div>

The important part of the above edit is changing the ID of the caption to something that we can easily associate with the img's ID 上面编辑的重要部分是将标题的ID更改为可以轻松与img的ID关联的内容

What this does is place the hover effect not on the image itself, but the container. 这样做是将悬停效果不是放在图像本身上,而是放在容器上。 Therefore, whenever your list item is hovered over, then the effect will be maintained, regardless what child is displayed or not displayed. 因此,只要将列表项悬停,无论显示还是不显示哪个子项,效果都将保持不变。

EDIT 编辑

Almost missed your side question. 几乎错过了您的附带问题。 If you want to make those the same size as your gallery div, you'll need to approach this one of two ways: 如果要使这些尺寸与图库div的尺寸相同,则需要采用以下两种方法之一:

  1. Set all elements below gallery to have a width of 100% 将图库下面的所有元素设置为100%的宽度

     .gallery ul, .gallery li, .gallery a, .gallery img { width: 100%; } 
  2. Set the width variable using your jQuery, like so: 使用jQuery设置width变量,如下所示:

     $('.gallery img').width($('.gallery').width()); 

EDIT 2 编辑2

Here's a working example of your first problem fixed 这是您解决的第一个问题的工作示例

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM