jQuery 添加和删除“active”类加上悬停

发布于 2024-08-16 06:31:55 字数 2680 浏览 7 评论 0原文

我有一个带有缩略图的定义列表。 它们的不透明度为 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 技术交流群。

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

发布评论

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

评论(3

冷默言语 2024-08-23 06:31:55

我只需将不透明度设置移至 CSS,然后在 jQuery 中添加/删除类。事实上,如果您的目标不是 IE6,您可以在 CSS 中使用 :hover 来处理不透明度。

WITH IE6 SUPPORT

dd img{ 
    opacity: 0.5;
    -moz-opacity: 0.5;
    filter:alpha(opacity=50);
}

dd.hover img, dd.thumbActive img { 
    opacity: 1.0;
    -moz-opacity: 1.0;
    filter:alpha(opacity=100);
}

然后将您的hover更改为:

jQuery("dd").hover(
    function() { jQuery(this).addClass('hover'); },
    function() { jQuery(this).removeClass('hover'); }
);

WITHOUT IE6 SUPPORT

dd img{ 
    opacity: 0.5;
    -moz-opacity: 0.5;
    filter:alpha(opacity=50);
}

dd:hover img, dd.thumbActive img{ 
    opacity: 1.0;
    -moz-opacity: 1.0;
    filter:alpha(opacity=100);
}

然后完全删除您的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

dd img{ 
    opacity: 0.5;
    -moz-opacity: 0.5;
    filter:alpha(opacity=50);
}

dd.hover img, dd.thumbActive img { 
    opacity: 1.0;
    -moz-opacity: 1.0;
    filter:alpha(opacity=100);
}

Then change your hover to this:

jQuery("dd").hover(
    function() { jQuery(this).addClass('hover'); },
    function() { jQuery(this).removeClass('hover'); }
);

WITHOUT IE6 SUPPORT

dd img{ 
    opacity: 0.5;
    -moz-opacity: 0.5;
    filter:alpha(opacity=50);
}

dd:hover img, dd.thumbActive img{ 
    opacity: 1.0;
    -moz-opacity: 1.0;
    filter:alpha(opacity=100);
}

And just remove your hover call entirely.

千年*琉璃梦 2024-08-23 06:31:55

当您单击然后将鼠标移开时,仍然会调用悬停函数,这会将不透明度设置回 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.

层林尽染 2024-08-23 06:31:55

感谢两者,我想出了这个:

dd img {
    opacity: 0.5;
    -moz-opacity: 0.5;
    filter:alpha(opacity=50);
}
/* IE6 does not support :hover */
dd.hover img { 
    opacity: 1.0;
    -moz-opacity: 1.0;
    filter:alpha(opacity=100);
}
jQuery("dd").hover(
    function() { 
        jQuery(this).addClass('hover');
        showClassNames();
    },
    function() { 
        if (!jQuery(this).hasClass('active')) jQuery(this).removeClass('hover'); 
    }
);

jQuery("a.thumbClick").click(function() {
    jQuery("dd").removeClass("hover active");
    jQuery(this).parent().addClass("hover active");     
    return false;
});
<dl id="thumb-list" >
    <dt></dt>
    <dd class="thumb hover active"><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>

Thanks to both, I came up with this:

dd img {
    opacity: 0.5;
    -moz-opacity: 0.5;
    filter:alpha(opacity=50);
}
/* IE6 does not support :hover */
dd.hover img { 
    opacity: 1.0;
    -moz-opacity: 1.0;
    filter:alpha(opacity=100);
}
jQuery("dd").hover(
    function() { 
        jQuery(this).addClass('hover');
        showClassNames();
    },
    function() { 
        if (!jQuery(this).hasClass('active')) jQuery(this).removeClass('hover'); 
    }
);

jQuery("a.thumbClick").click(function() {
    jQuery("dd").removeClass("hover active");
    jQuery(this).parent().addClass("hover active");     
    return false;
});
<dl id="thumb-list" >
    <dt></dt>
    <dd class="thumb hover active"><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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文