简体   繁体   English

使用CSS悬停类将鼠标悬停在链接上并在页面上的其他位置显示覆盖图像

[英]Using CSS hover class to hover over a link and display an overlay image elsewhere on the page

I was trying to figure out if there was a way to use CSS to hover over a link and make an overlay of an image show elsewhere on the page. 我试图弄清楚是否有一种方法可以使用CSS将鼠标悬停在链接上并使图像覆盖层显示在页面的其他位置。 I am making a website that has a map, and what I want to accomplish is that when they hover over a link to the corresponding location the map would sort of highlight the location of what it is they are hovering over to get a better understanding of where the place is and not have to search for it. 我正在制作一个有地图的网站,我要做的是,当他们将鼠标悬停在指向相应位置的链接上时,地图会突出显示他们正在悬停的内容的位置,以便更好地了解该位置在哪里,而不必搜索它。 What I am getting now is that I have to hover over the location on the image and then I get the effect but I want the effect when I hover over the link. 我现在得到的是我必须将鼠标悬停在图像上的位置上,然后才能获得效果,但是当我将鼠标悬停在链接上时,我想要该效果。

Any suggestions? 有什么建议么?

Thank you 谢谢

I apologize for the lack of code you guys. 抱歉缺少你们的代码。

Here are the divs that I am using to create some simple overlays. 这是我用来创建一些简单叠加层的div。 Just some color blocks. 只是一些色块。

<div class="roomOverlay blue" id="conf_sanchez"></div>
    <div class="roomOverlay red" id="conf_CIO"></div>
    <div class="roomOverlay green" id="conf_10F"></div>
    <div class="roomOverlay brown" id="Fuster"></div>

and here is the css 这是CSS

.blue:hover { background-color: Blue; }
.red:hover { background-color: red; }
.green:hover { background-color: green; }
.brown:hover { background-color: brown; }

#conf_sanchez {
height: 104px;
width: 96px;
left: 876px;
top: 14px;}

#conf_CIO {
height: 146px;
width: 69px;
left: 7px;
top: -92px;}

#conf_10F {
height: 67px;
width: 115px;
left: 194px;
top: 193px;}

#Fuster {
height: 139px;
width: 129px;
left: 180px;
top: -17px;}

FIXED What I had to do was a script since it became difficult positioning with the HTML and CSS. 已修正因为脚本很难用HTML和CSS定位,所以我要做的是编写脚本。 I used the data-overlay tag in my HTML as so. 我在HTML中使用了data-overlay标记。

 <dl> <dt><a href="#" **data-overlay="#Fuster"**>Fuster Conference Room - W Side behind small break room</a></dt>
        <dd>-Projector</dd>
        <dd>-Computer</dd>
        <dd>-Polycom</dd>
        <dd>-Seats 8-10 people</dd>
        <dd>-4 White Boards</dd>
    </dl>

JQuery Script: jQuery脚本:

  $(function () {
    $('.roomOverlay').css("visibility", "hidden");
    $('a').hover(function () {
        var overlay = $(this).data("overlay");
        $(overlay).css("visibility", "visible");
    }, function () {
        var overlay = $(this).data("overlay");
        $(overlay).css("visibility", "hidden");
    });
});

Here is the page showing the result for it. 这是显示结果页面 It does not show image for security purposes but the effect works on the empty image. 为了安全起见,它不会显示图像,但是效果适用于空白图像。 :) :)

For a CSS-only solution, you may want to look into child selectors . 对于仅CSS的解决方案,您可能需要研究子选择器

This will allow you to hover over the parent and show the child. 这将使您可以将鼠标悬停在父项上并显示子项。

For instance 例如

HTML 的HTML

<div id="parent">
   <div id="child">
   </div>
</div>

CSS 的CSS

#parent { properties }
#child {properties; height: 0}
#parent:hover > #child { height: 200px; properties}

Idea based off of provided code: http://jsfiddle.net/derekstory/nDyWF/1/ 基于提供的代码的想法: http//jsfiddle.net/derekstory/nDyWF/1/

As others have mentioned, you may be better suited with a JS/Jquery solution. 正如其他人提到的那样,您可能更适合使用JS / Jquery解决方案。 This is a bit dirty, but it is possible. 这有点脏,但是有可能。

You could do some stuff with positioning. 您可以通过定位来做一些事情。 Let's break out the ASCII art: 让我们来突破ASCII艺术:

<div>
------------------------------------------------------
|                             |    <img class="map"> |
|  <a>This is link            |                      |
|     <span>Overlay</span>    |                      |
|  </a>                       |                      |
|                             |                      |
|  <a>This is another link    |                      |
|     <span>Overlay</span>    |                      |
|  </a>                       |                      |
|                             |                      |
------------------------------------------------------
</div>

CSS 的CSS

div {
    position: relative;
}

div img,
div a span {
    position: absolute;
    top: 0;
    right: 0;
}

div img {
    z-index: 5;
}

a span {
    display: none;
    z-index: 10;
}

a:hover span {
    display: block;
}

I don't think a:hover span will work in IE 6, but it should do in other browsers. 我认为a:hover span无法在IE 6中使用,但应该在其他浏览器中也可以。

I know you specified using css for this problem, but using JS(jQuery) will make the answer more flexible for larger projects. 我知道您已指定使用CSS解决此问题,但使用JS(jQuery)可使答案更灵活,适用于大型项目。

HTML 的HTML

<img class="image" src="http://media-cdn.tripadvisor.com/media/photo-s/00/1d/3d/32/new-york-city.jpg">
<img class="image" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRCk4R8uFRe5EnqH9KrV3u-K1VcCp3y_qSG8vezNG5uSX7G-FMHvQ">
<img class="image" src="http://images1.fanpop.com/images/image_uploads/New-York-City-new-york-1020054_1024_768.jpg">
<img class="image" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSF_1ueOgxtgkT8HFuwO3f3K5fEtUxJtC-IICVWYcXJkchmgT1c">
<div class="links">
<a href="#" title="Example link!">Option 1</a> 
<a href="#" title="Example link!">Option 2</a> 
<a href="#" title="Example link!">Option 3</a> 
<a href="#" title="Example link!">Option 4</a> 
</div>

This sets up a templete for us to display the images specified. 这为我们设置了一个模板,以显示指定的图像。 When the user hovers over a link, the corresponding image will display. 当用户将鼠标悬停在链接上时,将显示相应的图像。

CSS 的CSS

img.image {
  position: relative;
  top: 0;
  left: 0;
  display: none;
  width: 300px;
}
img.active {
  display: block;
}
.links {
  position: absolute;
  bottom: 0;
}

This just sets some basic positioning. 这只是设置一些基本的位置。 The .active is the class that will be applied to the current image. .active是将应用于当前图像的类。 It will change the display from none to block . 它将显示none更改为block jQuery jQuery的

$(function() {
  $('a').hover(function() {
    var item = $('a').index(this);
    $('.image').removeClass('active');
    $('.image').eq(item).addClass('active');
  });
});

This is where the magic happens. 这就是魔术发生的地方。 When the user hovers over a link, jQuery determines which link is hovered by taking its index . 当用户将鼠标悬停在链接上时,jQuery通过获取其索引来确定要悬停的链接。 Then jQuery takes that index and finds an image with the same index and adds the class .active to it. 然后jQuery获取该索引并找到具有相同索引的图像,并向其添加.active类。 Therefore, no matter how large a project is and how many links and images you have, it is scalable to whichever size project. 因此,无论项目有多大以及您拥有多少链接和图像,它都可以扩展到任何大小的项目。
The same concept is used by slideshows. 幻灯片使用相同的概念。

Here is an example of this in action: http://codepen.io/anon/pen/nCDhG 这是一个实际的例子: http : //codepen.io/anon/pen/nCDhG

Use JQuery: 使用JQuery:

$("#link1").hover(
  function () {
    $('#map1').show();
  },
  function () {
    $('#map1').hide();
  }
);

The first is mouse-in the second function is mouse-out. 第一个功能是将鼠标移入,第二个功能是将鼠标移出。

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

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