简体   繁体   English

如何向这些轮播投资组合图片添加透明的悬停叠加层

[英]How to add transparent hover overlay to these carousel portfolio images

https://jsfiddle.net/dmaltron87/odpeLLv6/ https://jsfiddle.net/dmaltron87/odpeLLv6/

Trying to make it so a simple overlay with text appears when I hover over the images, but I can't get anything to work with this code. 当我将鼠标悬停在图像上时,会尝试使文本覆盖起来很简单,但是与此代码无关。 I found this in a demo tutorial and edited for my site, but my javascript understanding/skills are still pretty basic. 我在一个演示教程中找到了这个,并为我的网站进行了编辑,但是我对JavaScript的理解/技能仍然很基础。 I'm not too picky about the end result, just want ideas. 我对最终结果不太挑剔,只想要想法。

tried something like this, and added a css overlay with opacity=0 then hover at .75. 尝试过这样的事情,并添加了一个具有opacity = 0的css覆盖,然后将鼠标悬停在.75处。 straightforward, but didn't work. 简单,但没有用。

<div class="container">
  <img src=".." alt="Avatar" class="image">
  <div class="overlay">
    <div class="text">Hello World</div>
  </div>
</div>

You'll need to capture the mouseenter() and mouseleave() events and add some dynamic content. 您需要捕获mouseenter()mouseleave()事件并添加一些动态内容。

Here's an example: 这是一个例子:

 $(".carousel img").on("mouseenter", function() { //start hover let me = $(this); let position = me.position(); //create overlay let overlay = $('<div class="overlay"></div>'); //set position and size overlay.css({ left: position.left, top: position.top, height: me.height(), width: me.width() }); //append overlay me.after(overlay); }); //remove hover when the overlay is de-hovered $(".carousel").on("mouseleave", ".overlay", function() { //end hover $(this).remove(); }); 
 .overlay { background: url(http://via.placeholder.com/200x200/00AA00?text=Overlay) no-repeat center; opacity: 0.7; position: absolute; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="carousel"> <img src="http://lorempixel.com/200/200/sports/1/" /> <img src="http://lorempixel.com/200/200/sports/2/" /> <img src="http://lorempixel.com/200/200/sports/3/" /> </div> 

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

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