简体   繁体   English

使用悬停在图像效果上的最有效方法

[英]Most efficient way to use hover over image effects

I know how to answer my question, I'm just posting to see if there's a better way to do what I'm already doing. 我知道如何回答我的问题,我只是在发帖,看看是否有更好的方法来做我已经在做的事情。

Let's say I'm making a website that sells 4 different types of posters. 假设我正在制作一个出售4种不同类型海报的网站。 I want the user to see each of the posters in a row. 我希望用户连续查看每个海报。 and when they hover over each picture the image will change to show the price, and measurements of the poster. 当他们将鼠标悬停在每张图片上时,图片将更改以显示价格和海报尺寸。

How I achieved this: 我是如何做到这一点的:

<ul>
<li> image link here using onmouseover and onmouseout for hover over effects </li>
<li> same as above </li>
<li> same as above </li>
<li> same as above </li>
</ul

Then I just styled the list to remove bullets and aligned it horizontally. 然后,我将样式设置为样式以删除项目符号并水平对齐。 Now here's my question... I am currently using onmouseover and onmouseout for hover over effects Because you need 2 images in order to achieve this you need a lot of data, especially if you're going to do this for, say, a grid of 25 images for an art portfolio. 现在这是我的问题...我目前正在使用onmouseover和onmouseout来将鼠标悬停在效果上,因为要实现此效果,您需要2张图像,因此需要大量数据,尤其是如果要为网格进行此操作时25张艺术作品集的图像。

Is this a bad way to get a hover over effect? 这是使悬停效果不好的方法吗? I'm assuming because I'm new at web-development anything I can throw onto a webpage is going to be somewhat crude and not efficient. 我假设因为我是Web开发的新手,所以我可以放到网页上的任何东西都会有些粗糙并且效率不高。

You could have another div within the <li> containing the info you wanted. 您可以在<li>包含另一个包含所需信息的div。 Have this absolutely positioned over the image and then show it on hover using opacity . 将其绝对定位在图像上,然后使用opacity在悬停时显示它。

Demo 演示

HTML HTML

<ul>
    <li><img src="http://placehold.it/350x150" alt="" /><div class="info">Info here</div></li>
    <li><img src="http://placehold.it/350x150" alt="" /><div class="info">Info here</div></li>
    <li><img src="http://placehold.it/350x150" alt="" /><div class="info">Info here</div></li>
</ul>

CSS CSS

ul,li {
   list-style: none; 
}

li {
   display: inline-block;
   position: relative;
   z-index: 1;
}
img {
   display: block;
}

.info {
   opacity: 0;
   color: white;
   position: absolute;
   top: 0;
   bottom: 0;
   width: 100%;
   z-index: 2;
   background: red;
   .transition(opacity 0.5s ease);
}

li:hover .info {
   opacity: 1;
}

I made an example for you :) 我为你做了一个例子:)

You can line .image next to each other with display: inline-block; 您可以使用display: inline-block; .image display: inline-block;

Have a fiddle! 小提琴!

HTML HTML

<div class="image">
    <img src="http://www.placehold.it/200X200" />
    <div class="text">Hello</div>
</div>

CSS CSS

.image {
    position: relative;
    width: 200px;
    height: 200px;
}

.text {
    display: none;
}

img { 
    cursor: pointer; 
}
img:hover + .text, .text:hover {
    position: absolute;
    bottom: 0;
    height: 50px;
    background: rgba(0, 0, 0, 0.5);
    display: block;
    padding: 10px;
    width: 180px;
    cursor: pointer;
}

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

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