jQuery 添加和删除“active”类加上悬停
我有一个带有缩略图的定义列表。 它们的不透明度为 50%,具有“拇指”类别。 悬停时不透明度为 100%。 单击时 100% 不透明度加上将“thumb”更改为“thumbactive”类
到目前为止,我的蹩脚代码可以工作,唯一的问题是我无法在单击时获得 100% 的 tn。
dl { width: 700px; } dt { clear: left; float: right; width: 400px; height:80px; margin: 0 0 10px 0; background:orange; } dd.thumb, dd.thumbActive { clear: none; float: left; margin: 0 0 10px 0; background:black; } dd { clear: right; }
jQuery.noConflict(); jQuery(document).ready(function() { /* just for debugging */ function showClassNames() { jQuery("dt").each(function() { var className = jQuery(this).next().attr('class'); jQuery(this).text(className); }); }; showClassNames(); /* resets all thumbs (50% alpha, .thumb classname) */ function resetThumbs() { jQuery("dd").each(function() { jQuery(this).removeClass("thumbActive"); jQuery(this).addClass("thumb"); jQuery("dd img").css('opacity', 0.5); }); }; // half opacity for all thumbs except the first one (active) jQuery("dd:not(.thumbActive) img").css('opacity', 0.5); jQuery("dd img").hover( function() { jQuery(this).css('opacity', 1.0); }, function() { jQuery(this).css('opacity', 0.5); } ); jQuery("a.thumbClick").click(function() { resetThumbs(); jQuery(this).parent().removeClass("thumb"); jQuery(this).parent().addClass("thumbActive"); jQuery(this).css('opacity', 1.0); showClassNames(); return false; }); }); // end document ready
<div id="album-canvas-left" style="width:930px; " >
<dl id="thumb-list" >
<dt></dt>
<dd class="thumbActive"><a href="#" class="thumbClick"><img src="gallery/album1/thumb/001.jpg" width="120" height="80" alt="living room" title="living room" /></a></dd>
<dd></dd>
<dt></dt>
<dd class="thumb"><a href="#" class="thumbClick"><img src="gallery/album1/thumb/002.jpg" width="120" height="80" alt="bedroom" title="bedroom" /></a></dd>
<dd></dd>
<dt></dt>
<dd class="thumb"><a href="#" class="thumbClick"><img src="gallery/album1/thumb/003.jpg" width="120" height="80" alt="kitchen" title="kitchen" /></a></dd>
<dd></dd>
</dl>
I have a definition list with thumbnails.
They are 50% opacity with a 'thumb' class.
When hovered 100% opacity.
When clicked 100% opacity plus change 'thumb' to 'thumbactive' class
So far my crappy code works, only thing is I can not get the tn on 100% on click.
dl { width: 700px; } dt { clear: left; float: right; width: 400px; height:80px; margin: 0 0 10px 0; background:orange; } dd.thumb, dd.thumbActive { clear: none; float: left; margin: 0 0 10px 0; background:black; } dd { clear: right; }
jQuery.noConflict(); jQuery(document).ready(function() { /* just for debugging */ function showClassNames() { jQuery("dt").each(function() { var className = jQuery(this).next().attr('class'); jQuery(this).text(className); }); }; showClassNames(); /* resets all thumbs (50% alpha, .thumb classname) */ function resetThumbs() { jQuery("dd").each(function() { jQuery(this).removeClass("thumbActive"); jQuery(this).addClass("thumb"); jQuery("dd img").css('opacity', 0.5); }); }; // half opacity for all thumbs except the first one (active) jQuery("dd:not(.thumbActive) img").css('opacity', 0.5); jQuery("dd img").hover( function() { jQuery(this).css('opacity', 1.0); }, function() { jQuery(this).css('opacity', 0.5); } ); jQuery("a.thumbClick").click(function() { resetThumbs(); jQuery(this).parent().removeClass("thumb"); jQuery(this).parent().addClass("thumbActive"); jQuery(this).css('opacity', 1.0); showClassNames(); return false; }); }); // end document ready
<div id="album-canvas-left" style="width:930px; " >
<dl id="thumb-list" >
<dt></dt>
<dd class="thumbActive"><a href="#" class="thumbClick"><img src="gallery/album1/thumb/001.jpg" width="120" height="80" alt="living room" title="living room" /></a></dd>
<dd></dd>
<dt></dt>
<dd class="thumb"><a href="#" class="thumbClick"><img src="gallery/album1/thumb/002.jpg" width="120" height="80" alt="bedroom" title="bedroom" /></a></dd>
<dd></dd>
<dt></dt>
<dd class="thumb"><a href="#" class="thumbClick"><img src="gallery/album1/thumb/003.jpg" width="120" height="80" alt="kitchen" title="kitchen" /></a></dd>
<dd></dd>
</dl>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只需将不透明度设置移至 CSS,然后在 jQuery 中添加/删除类。事实上,如果您的目标不是 IE6,您可以在 CSS 中使用
:hover
来处理不透明度。WITH IE6 SUPPORT
然后将您的
hover
更改为:WITHOUT IE6 SUPPORT
然后完全删除您的
hover
调用。I would just move your opacity settings to the CSS and just add/remove classes in jQuery. In fact, if you aren't targeting IE6 you can just use
:hover
in CSS to handle the opacity.WITH IE6 SUPPORT
Then change your
hover
to this:WITHOUT IE6 SUPPORT
And just remove your
hover
call entirely.当您单击然后将鼠标移开时,仍然会调用悬停函数,这会将不透明度设置回 0.5
您应该在悬停函数(悬停函数的第二个参数)中检查并确保对象的类是“ t 设置为thumbActive。
When you click then mouse off the hover out function is still being called which is setting the opacity back to .5
You should in the hover out function (The second argument to the hover function) check and make sure the class of the object isn't set to thumbActive.
感谢两者,我想出了这个:
Thanks to both, I came up with this: