Jquery:悬停时透明覆盖

发布于 2024-08-15 22:52:25 字数 1862 浏览 6 评论 0原文

基本上我有4个图像,div设置如下:

   <div id="selector">
    <div id="1"><div class="folio_container pk"><div class="overlay"></div></div></div>
    <div id="2"><div class="folio_container ybn"><div class="overlay"></div></div></div>
    <div id="3"><div class="folio_container u"><div class="overlay"></div></div></div>
    <div id="4"><div class="folio_container more"><div class="overlay"></div></div></div>
    <div class="clearfloat"></div>
    </div>

和css:

  .folio_container{margin:10px 80px 10px 15px; float:left; position:relative; cursor:pointer; -moz-border-radius: 8px; -webkit-border-radius: 8px; background-position:top left; background-repeat:no-repeat; width:80px; height: 80px;}
.pk{ background-image:url(../images/pk_icon.png) !important;}
.ybn{ background-image:url(../images/ybn_icon.png) !important;}
.u{ background-image:url(../images/u_icon.png) !important;}
.more{ background-image:url(../images/more_icon.png); margin:10px 0px 10px 15px !important;}

.overlay{ background-image:url(../images/overlay.png); background-position:top left; -moz-border-radius: 8px; -webkit-border-radius: 8px; background-repeat:no-repeat; width:80px; height:80px; position:absolute; top:0px; left:0px; display:none;}

我怎样才能得到我的Jquery,以便它在每个单独的图像悬停时在覆盖层中淡入淡出,我总是对这个想法感到困惑,因为div类都是一样的,不会同时触发吗?

到目前为止的 jquery 如下:

$('.overlay').css('display', 'block');
$('.overlay').css('opacity', 0.0);
$('folio_container', this).hover(function() {
    $(this).children('.overlay', this).stop().animate({opacity:1.0},500);
},
function() {
    $(this).children('.overlay', this).stop().animate({opacity:0.0},500);
});

basically I have 4 images with divs set up like this:

   <div id="selector">
    <div id="1"><div class="folio_container pk"><div class="overlay"></div></div></div>
    <div id="2"><div class="folio_container ybn"><div class="overlay"></div></div></div>
    <div id="3"><div class="folio_container u"><div class="overlay"></div></div></div>
    <div id="4"><div class="folio_container more"><div class="overlay"></div></div></div>
    <div class="clearfloat"></div>
    </div>

And the css:

  .folio_container{margin:10px 80px 10px 15px; float:left; position:relative; cursor:pointer; -moz-border-radius: 8px; -webkit-border-radius: 8px; background-position:top left; background-repeat:no-repeat; width:80px; height: 80px;}
.pk{ background-image:url(../images/pk_icon.png) !important;}
.ybn{ background-image:url(../images/ybn_icon.png) !important;}
.u{ background-image:url(../images/u_icon.png) !important;}
.more{ background-image:url(../images/more_icon.png); margin:10px 0px 10px 15px !important;}

.overlay{ background-image:url(../images/overlay.png); background-position:top left; -moz-border-radius: 8px; -webkit-border-radius: 8px; background-repeat:no-repeat; width:80px; height:80px; position:absolute; top:0px; left:0px; display:none;}

How can I get my Jquery so that it fades in the overlay on hover of each separate one, I've always got confused with this idea seeing as the div classes are the same, will it not trigger them all at the same time?

Heres the jquery thus far:

$('.overlay').css('display', 'block');
$('.overlay').css('opacity', 0.0);
$('folio_container', this).hover(function() {
    $(this).children('.overlay', this).stop().animate({opacity:1.0},500);
},
function() {
    $(this).children('.overlay', this).stop().animate({opacity:0.0},500);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七禾 2024-08-22 22:52:26

在幕后,jQuery 迭代与选择器匹配的每个 div,并将 overout 函数绑定到每个单独的元素。

顺便说一句,我建议同时调用 stop可选参数,clearQueue(在功能之间切换时删除任何待处理的动画)和 gotoEnd(完成当前动画)设置为 true - 这通常是人们想要的行为! (例如,请参阅此问题。)

Behind the scenes, jQuery iterates each of the divs matched by your selector and binds the over and out functions to each individual element.

As an aside, I recommend calling stop with both of the optional parameters, clearQueue (to remove any pending animations when you switch between functions) and gotoEnd (to finish the current animation) set to true - this is usually the behavior people want! (See for example, this question.)

七秒鱼° 2024-08-22 22:52:26

我得到了答案,我从来没有意识到 Jquery 足够聪明,只能将操作分别应用于具有相同类的触发器的单独元素!完美:

无论如何都是jquery!

    $('.overlay').css('display', 'block');
$('.overlay').css('opacity', 0.0);
$('.folio_container').hover(function() {
    $('.overlay', this).stop().animate({opacity:1.0},500);
},
function() {
    $('.overlay', this).stop().animate({opacity:0.0},500);
});

I got the answer, I never realised Jquery was clever enough to only apply an action onto the separate elements respectively of the trigger with the same classes! Perfect:

heres the jquery either way!

    $('.overlay').css('display', 'block');
$('.overlay').css('opacity', 0.0);
$('.folio_container').hover(function() {
    $('.overlay', this).stop().animate({opacity:1.0},500);
},
function() {
    $('.overlay', this).stop().animate({opacity:0.0},500);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文