使用 jQuery,加载不透明度为 0.5 的 div
我的页面上有几张图像,其标题覆盖图像底部 50 像素。当它们加载时,它们的不透明度从 1 开始,但我希望它们从 0.5 开始。原因是有一个 hover
事件在悬停时将不透明度动画为 1,因此我希望它们从 0.5 开始。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.fade').hover(function() {
$(this).stop().animate({"opacity": 1});
},function() {
$(this).stop().animate({"opacity": .5});
});
});
</script>
</head>
<body>
<div class="image_w_caption" style="float:left;margin:5px;">
<div class="image" style="width:250px;height:188px;background:url(images/image.jpg) no-repeat 0 0;">
</div>
<div class="fade" style="width:250px;height:188px;background:url(images/image_2.png) no-repeat 0 0;position:relative;top:-203;z-index:2;">
<p style="padding:150px 5px 0 5px;z-index:3;color:white;">text</p>
</div>
</div>
<div class="image_w_caption" style="float:left;margin:5px;">
<div class="image" style="width:250px;height:188px;background:url(images/image_2.jpg) no-repeat 0 0;">
</div>
<div class="fade" style="width:250px;height:188px;background:url(images/image_2.png) no-repeat 0 0;position:relative;top:-203;z-index:2;">
<p style="padding:150px 5px 0 5px;z-index:3;color:white;">more text</p>
</div>
</div>
</body>
</html>
I have several images on a page that have captions that overlay the bottom 50px of the image. When they load, they begin at opacity of 1, but I want them start as .5. The reason is that there's a hover
event that animates the opacity to 1 on hover, so I want them to start on .5.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.fade').hover(function() {
$(this).stop().animate({"opacity": 1});
},function() {
$(this).stop().animate({"opacity": .5});
});
});
</script>
</head>
<body>
<div class="image_w_caption" style="float:left;margin:5px;">
<div class="image" style="width:250px;height:188px;background:url(images/image.jpg) no-repeat 0 0;">
</div>
<div class="fade" style="width:250px;height:188px;background:url(images/image_2.png) no-repeat 0 0;position:relative;top:-203;z-index:2;">
<p style="padding:150px 5px 0 5px;z-index:3;color:white;">text</p>
</div>
</div>
<div class="image_w_caption" style="float:left;margin:5px;">
<div class="image" style="width:250px;height:188px;background:url(images/image_2.jpg) no-repeat 0 0;">
</div>
<div class="fade" style="width:250px;height:188px;background:url(images/image_2.png) no-repeat 0 0;position:relative;top:-203;z-index:2;">
<p style="padding:150px 5px 0 5px;z-index:3;color:white;">more text</p>
</div>
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 css 中设置不透明度或稍微修改脚本:
you can set the opacity in the css or modify your script a little:
只需使用
在分配悬停之前即可。或者像 @meo 指出的那样,你可以链接它,实现同样的事情,只是少一次对 dom 的调用。
现场演示
Just use
Before you assign the hover. Or like @meo pointed out you can chain it, achieves the same thing, just one less call to the dom.
Live Demo
在 CSS 中对图像使用透明度:
Use trasparency for images in your css: