Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用悬停图像效果的最有效方法_Javascript_Html_Css - Fatal编程技术网

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

Javascript 使用悬停图像效果的最有效方法,javascript,html,css,Javascript,Html,Css,我知道如何回答我的问题,我只是想看看是否有更好的方法来做我已经在做的事情。 假设我正在制作一个网站,销售4种不同类型的海报。我想让用户看到一排的海报。当他们将鼠标悬停在每张图片上时,图像将发生变化,以显示海报的价格和尺寸 我是如何做到这一点的: <ul> <li> image link here using onmouseover and onmouseout for hover over effects </li> <li> same as ab

我知道如何回答我的问题,我只是想看看是否有更好的方法来做我已经在做的事情。

假设我正在制作一个网站,销售4种不同类型的海报。我想让用户看到一排的海报。当他们将鼠标悬停在每张图片上时,图像将发生变化,以显示海报的价格和尺寸

我是如何做到这一点的:

<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
  • 使用onmouseover和onmouseout进行悬停效果的图像链接
  • 同上
  • 同上
  • 同上

  • 您可以在
  • 中有另一个div,其中包含您想要的信息。将其绝对放置在图像上,然后使用
    不透明度将其悬停显示

    HTML


    我给你举了个例子:)

    您可以使用
    display:inline block将
    .image
    排列在一起

    HTML


    谢谢,我会调查的。我已经更新了演示。它使用的更少,但是CSS也应该是可见的。这允许很大的灵活性,我总是觉得最好不要依赖JS来完成这类事情。哦,这很酷,我喜欢它的淡入效果,它不像onmouseover那样突然。谢谢Hi@ @ USER 77300 1如果这个或其他答案已经解决了你的问题,请考虑通过点击复选标记接受它。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这么做。谢谢,这真的很酷。这会不会比我正在使用的onmouseover和onmouseout函数的加载时间更短?这会比加载javascript更快:)
    <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>
    
    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;
    }
    
    <div class="image">
        <img src="http://www.placehold.it/200X200" />
        <div class="text">Hello</div>
    </div>
    
    .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;
    }