【问题标题】:CSS Hover effect over image not working图像上的 CSS 悬停效果不起作用
【发布时间】:2018-09-16 22:39:20
【问题描述】:

我想要做的是,当用户将鼠标悬停在图像上时,带有信息图标的透明黑色叠加层会出现在图像上。

由于某种原因,我似乎找不到我的逻辑有什么问题。

我正在使用 JQuery 的 toogleClass() 来切换隐藏类。

$(document).ready(function(){
    $('.toggle_hide').hover(function(){
        $(this).toggleClass('hide');
    });
});
* {
box-sizing:border-box;
margin:0;
padding:0;
} 

a{text-decoration:none;}

.hide{display:none; transition: 0.5s ease all;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div style="width:100%; height:auto; display:flex; flex-direction:column; padding:0px 0px; ">
    <span style="width:100%; height:150px; display:flex; justify-content:center; align-items:center; font-family:Ostrich Sans Bold; font-size:65px; font-weight:900; color:rgba(0,0,0,0.9);">NUESTROS BOCATAS</span>
	<div style="width:100%; height:auto; display:flex; justify-content:space-around; flex-wrap:wrap;">
	    <div style="width:46%; height:500px; display:flex; flex-direction:column; margin:25px 0px;">
		    <div style="width:100%; height:70%; position:relative; box-shadow:4px 4px 4px rgba(0,0,0,0.2); background-size:cover; background-position:center; background-image:url('https://tallypress.com/wp-content/uploads/2017/08/top-10-western-food-catering-services-in-kl-and-selangor.jpg');">
			    <div class="toggle_hide hide" style="display:flex; width:100%; height:100%; position:absolute; top:0px; left:0px; z-index:999; background-color:rgba(0,0,0,0.6);">   
				    <i class="fa fa-info" style="margin:auto; font-size:25px; color:rgba(255,255,255,0,7);"></i>
				</div>
			</div>
			<div style="width:100%; height:30%; padding:10px; display:flex; flex-direction:column; ">
			    <a href="" style="text-align:center; font-family:Ostrich Sans Heavy Bold; font-size:22px; color:black; font-weight:600; margin-bottom:15px;">Titulo</a>
				<a href="" style="font-family:Aleo; font-size:19px; color:rgba(0,0,0,0.8); font-weight:600;">Grade A Nebraska chuck beef, on a sponge bun with lettuce, onion, tomato, and thousand island.</a>
			</div>
		</div>
		<div style="width:46%; height:500px; display:flex; flex-direction:column; margin:25px 0px;">
		    <div style="width:100%; height:70%; position:relative; box-shadow:4px 4px 4px rgba(0,0,0,0.2); background-size:cover; background-position:center; background-image:url('https://tallypress.com/wp-content/uploads/2017/08/top-10-western-food-catering-services-in-kl-and-selangor.jpg');">
			    <div class="toggle_hide hide" style="display:flex; width:100%; height:100%; position:absolute; top:0px; left:0px; z-index:999; background-color:rgba(0,0,0,0.6);">   
				    <i class="fa fa-info" style="margin:auto; font-size:25px; color:rgba(255,255,255,0,7);"></i>
				</div>
			</div>
			<div style="width:100%; height:30%; padding:10px; display:flex; flex-direction:column; ">
			    <a href="" style="text-align:center; font-family:Ostrich Sans Heavy Bold; font-size:22px; color:black; font-weight:600; margin-bottom:15px;">Titulo</a>
				<a href="" style="font-family:Aleo; font-size:19px; color:rgba(0,0,0,0.8); font-weight:600;">Grade A Nebraska chuck beef, on a sponge bun with lettuce, onion, tomato, and thousand island.</a>
			</div>
		</div>
		<div style="width:46%; height:500px; display:flex; flex-direction:column; margin:25px 0px;">
		    <div style="width:100%; height:70%; position:relative; box-shadow:4px 4px 4px rgba(0,0,0,0.2); background-size:cover; background-position:center; background-image:url('https://tallypress.com/wp-content/uploads/2017/08/top-10-western-food-catering-services-in-kl-and-selangor.jpg');">
			    <div class="toggle_hide hide" style="display:flex; width:100%; height:100%; position:absolute; top:0px; left:0px; z-index:999; background-color:rgba(0,0,0,0.6);">   
				    <i class="fa fa-info" style="margin:auto; font-size:25px; color:rgba(255,255,255,0,7);"></i>
				</div>
			</div>
			<div style="width:100%; height:30%; padding:10px; display:flex; flex-direction:column; ">
			    <a href="" style="text-align:center; font-family:Ostrich Sans Heavy Bold; font-size:22px; color:black; font-weight:600; margin-bottom:15px;">Titulo</a>
				<a href="" style="font-family:Aleo; font-size:19px; color:rgba(0,0,0,0.8); font-weight:600;">Grade A Nebraska chuck beef, on a sponge bun with lettuce, onion, tomato, and thousand island.</a>
			</div>
		</div>
		<div style="width:46%; height:500px; display:flex; flex-direction:column; margin:25px 0px;">
		    <div style="width:100%; height:70%; position:relative; box-shadow:4px 4px 4px rgba(0,0,0,0.2); background-size:cover; background-position:center; background-image:url('https://tallypress.com/wp-content/uploads/2017/08/top-10-western-food-catering-services-in-kl-and-selangor.jpg');">
			    <div class="toggle_hide hide" style="display:flex; width:100%; height:100%; position:absolute; top:0px; left:0px; z-index:999; background-color:rgba(0,0,0,0.6);">   
				    <i class="fa fa-info" style="margin:auto; font-size:25px; color:rgba(255,255,255,0,7);"></i>
				</div>
			</div>
			<div style="width:100%; height:30%; padding:10px; display:flex; flex-direction:column; ">
			    <a href="" style="text-align:center; font-family:Ostrich Sans Heavy Bold; font-size:22px; color:black; font-weight:600; margin-bottom:15px;">Titulo</a>
				<a href="" style="font-family:Aleo; font-size:19px; color:rgba(0,0,0,0.8); font-weight:600;">Grade A Nebraska chuck beef, on a sponge bun with lettuce, onion, tomato, and thousand island.</a>
			</div>
		</div>
	</div>
</div>

【问题讨论】:

  • 不要使用内联 CSS/JS - 这是一种不好的做法,会导致代码难以维护
  • 不仅如此,对于决定如何将时间花在 Stack Overflow 上的人们来说,它是压倒性的,而且难以解读。
  • 图标在哪里发挥作用?我在你的代码中没有看到它?此外,代码 sn-p...它无法正常运行,对吗?我在代码语法或代码 sn-p 示例中没有看到图标?
  • 这可能会帮助您检查此链接Here
  • 这样的功能不需要 jQuery。

标签: jquery css hover effect


【解决方案1】:

内联样式会覆盖那些来自包含的 CSS 样式表的样式,这可能是这里的问题。您应该阅读一些关于 CSS Cascade 以及它是如何计算应用于给定元素的样式的内容。

例如.hide {display: none;} 永远不会有效果,除非您删除可能在 CSS 类中的内联样式 display: flex,而不是内联。另一个我不推荐的选项是使用!important

【讨论】:

    【解决方案2】:

    只需将css 中的.hide 类替换为:

    .hide{opacity:0; transition: 1s ease all;}
    

    而不是toggleClass() 使用$(this).css({'opacity' : 1}); 以获得更好的过渡

    $(document).ready(function(){
        $('.toggle_hide').hover(function(){
            $(this).css({'opacity' : 1});
        }, function(){
           $(this).css({'opacity' : 0});
        });
    });
    * {
    box-sizing:border-box;
    margin:0;
    padding:0;
    } 
    
    a{text-decoration:none;}
    
    .hide{opacity:0; transition: 0.5s ease all;}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div style="width:100%; height:auto; display:flex; flex-direction:column; padding:0px 0px; ">
        <span style="width:100%; height:150px; display:flex; justify-content:center; align-items:center; font-family:Ostrich Sans Bold; font-size:65px; font-weight:900; color:rgba(0,0,0,0.9);">NUESTROS BOCATAS</span>
    	<div id="content" style="width:100%; height:auto; display:flex; justify-content:space-around; flex-wrap:wrap;">
    	    <div style="width:46%; height:500px; display:flex; flex-direction:column; margin:25px 0px;">
    		    <div style="width:100%; height:70%; position:relative; box-shadow:4px 4px 4px rgba(0,0,0,0.2); background-size:cover; background-position:center; background-image:url('https://tallypress.com/wp-content/uploads/2017/08/top-10-western-food-catering-services-in-kl-and-selangor.jpg');">
    			    <div class="toggle_hide hide" style=" width:100%; height:100%; position:absolute; top:0px; left:0px; z-index:999; background-color:rgba(0,0,0,0.6);">   
    				    <i class="fa fa-info" style="margin:auto; font-size:25px; color:rgba(255,255,255,0,7);"></i>
    				</div>
    			</div>
    			<div style="width:100%; height:30%; padding:10px; display:flex; flex-direction:column; ">
    			    <a href="" style="text-align:center; font-family:Ostrich Sans Heavy Bold; font-size:22px; color:black; font-weight:600; margin-bottom:15px;">Titulo</a>
    				<a href="" style="font-family:Aleo; font-size:19px; color:rgba(0,0,0,0.8); font-weight:600;">Grade A Nebraska chuck beef, on a sponge bun with lettuce, onion, tomato, and thousand island.</a>
    			</div>
    		</div>
    		<div style="width:46%; height:500px; display:flex; flex-direction:column; margin:25px 0px;">
    		    <div style="width:100%; height:70%; position:relative; box-shadow:4px 4px 4px rgba(0,0,0,0.2); background-size:cover; background-position:center; background-image:url('https://tallypress.com/wp-content/uploads/2017/08/top-10-western-food-catering-services-in-kl-and-selangor.jpg');">
    			    <div class="toggle_hide hide" style="display:flex; width:100%; height:100%; position:absolute; top:0px; left:0px; z-index:999; background-color:rgba(0,0,0,0.6);">   
    				    <i class="fa fa-info" style="margin:auto; font-size:25px; color:rgba(255,255,255,0,7);"></i>
    				</div>
    			</div>
    			<div style="width:100%; height:30%; padding:10px; display:flex; flex-direction:column; ">
    			    <a href="" style="text-align:center; font-family:Ostrich Sans Heavy Bold; font-size:22px; color:black; font-weight:600; margin-bottom:15px;">Titulo</a>
    				<a href="" style="font-family:Aleo; font-size:19px; color:rgba(0,0,0,0.8); font-weight:600;">Grade A Nebraska chuck beef, on a sponge bun with lettuce, onion, tomato, and thousand island.</a>
    			</div>
    		</div>
    		<div style="width:46%; height:500px; display:flex; flex-direction:column; margin:25px 0px;">
    		    <div style="width:100%; height:70%; position:relative; box-shadow:4px 4px 4px rgba(0,0,0,0.2); background-size:cover; background-position:center; background-image:url('https://tallypress.com/wp-content/uploads/2017/08/top-10-western-food-catering-services-in-kl-and-selangor.jpg');">
    			    <div class="toggle_hide hide" style="display:flex; width:100%; height:100%; position:absolute; top:0px; left:0px; z-index:999; background-color:rgba(0,0,0,0.6);">   
    				    <i class="fa fa-info" style="margin:auto; font-size:25px; color:rgba(255,255,255,0,7);"></i>
    				</div>
    			</div>
    			<div style="width:100%; height:30%; padding:10px; display:flex; flex-direction:column; ">
    			    <a href="" style="text-align:center; font-family:Ostrich Sans Heavy Bold; font-size:22px; color:black; font-weight:600; margin-bottom:15px;">Titulo</a>
    				<a href="" style="font-family:Aleo; font-size:19px; color:rgba(0,0,0,0.8); font-weight:600;">Grade A Nebraska chuck beef, on a sponge bun with lettuce, onion, tomato, and thousand island.</a>
    			</div>
    		</div>
    		<div style="width:46%; height:500px; display:flex; flex-direction:column; margin:25px 0px;">
    		    <div style="width:100%; height:70%; position:relative; box-shadow:4px 4px 4px rgba(0,0,0,0.2); background-size:cover; background-position:center; background-image:url('https://tallypress.com/wp-content/uploads/2017/08/top-10-western-food-catering-services-in-kl-and-selangor.jpg');">
    			    <div class="toggle_hide hide" style="display:flex; width:100%; height:100%; position:absolute; top:0px; left:0px; z-index:999; background-color:rgba(0,0,0,0.6);">   
    				    <i class="fa fa-info" style="margin:auto; font-size:25px; color:rgba(255,255,255,0,7);"></i>
    				</div>
    			</div>
    			<div style="width:100%; height:30%; padding:10px; display:flex; flex-direction:column; ">
    			    <a href="" style="text-align:center; font-family:Ostrich Sans Heavy Bold; font-size:22px; color:black; font-weight:600; margin-bottom:15px;">Titulo</a>
    				<a href="" style="font-family:Aleo; font-size:19px; color:rgba(0,0,0,0.8); font-weight:600;">Grade A Nebraska chuck beef, on a sponge bun with lettuce, onion, tomato, and thousand island.</a>
    			</div>
    		</div>
    	</div>
    </div>

    【讨论】:

      猜你喜欢
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 2013-09-12
      • 1970-01-01
      相关资源
      最近更新 更多