如何取消jquery div点击效果

发布于 2024-08-16 15:08:12 字数 600 浏览 5 评论 0原文

我对 div 施加了悬停效果。它所做的基本上就是增加 mouseenter 的高度,减少 mouseleave 的高度。

作为一个单独的功能,我将点击效果应用于其中一个 div:

$(function(){
    $( '#div1 a' ).click(function() {
        $( '.theRestofTheDivs' ).each(function() {
            $(this).animate({height: "30px"}, 200);
        })  
    });
});

它效果很好,除了当我鼠标离开时,它会折叠。显然这是因为我有之前的悬停效果。

单击后,我不想让它能够折叠,直到单击另一个带有锚点的 div。有什么想法吗?

编辑

提供的代码已简化,要在完整的上下文中查看它,请访问http:// /www.raceramps.com/v2

将鼠标悬停在“浏览全部”上,然后单击它。我不想让它崩溃。

I have a hover effect applied to a div. All it does is basically increase the height mouseenter, decrease the height mouseleave.

As a separate function, I have a click effect applied to one of the divs:

$(function(){
    $( '#div1 a' ).click(function() {
        $( '.theRestofTheDivs' ).each(function() {
            $(this).animate({height: "30px"}, 200);
        })  
    });
});

It works great, except when I mouseleave, it collapses. Obviously that is because I have the previous hover effect.

After it's been clicked, I don't want to allow it to be able to collapse until another div with an anchor is clicked. Any ideas?

EDIT

The code provided is simplified, to see it in the full context go to http://www.raceramps.com/v2

Mouseover "Browse All", then click it. I don't want it to collapse.

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

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

发布评论

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

评论(2

我家小可爱 2024-08-23 15:08:12

您可以将一个类放在单击的div上,并且仅在该类不存在的情况下折叠div

当单击另一个div时,您从前一个div中删除该类并将其添加到单击的div中

$(function(){
    $( '#div1 a' ).click(function() {
        $(".KeepOpen").removeClass(".KeepOpen");
        $(this).addClass("KeepOpen");  
        $( '.theRestofTheDivs' ).each(function() {
            $(this).animate({height: "30px"}, 200);
        })  
    });
});

并在折叠部分添加此

if ( !$(this).hasClass("KeepOpen") )
    // Collapse

You can put a class on the one clicked and only collapse divs if the class doesnt exist

When another div is clicked you remove the class from the previous div and add it to the one clicked

$(function(){
    $( '#div1 a' ).click(function() {
        $(".KeepOpen").removeClass(".KeepOpen");
        $(this).addClass("KeepOpen");  
        $( '.theRestofTheDivs' ).each(function() {
            $(this).animate({height: "30px"}, 200);
        })  
    });
});

And on your collapsing part add this

if ( !$(this).hasClass("KeepOpen") )
    // Collapse
懷念過去 2024-08-23 15:08:12

假设您的基本标记看起来像这样:

<div id="div1" class="myClass"><a href="#">Div1</a></div>
<div id="div2" class="myClass"><a href="#">Div2</a></div>
<div id="div3" class="myClass"><a href="#">Div3</a></div>

您可以这样做:

// Mouseover event
$(".myClass a").live("mouseover", function () {
    // If it already has the "selected" class applied to it, do nothing
    if ( $(this).is(".selected") ) {
        return;
    }
    else {
        // Do your hover effect
        $(this).animate({height: "30px"}, 200);
    }
});

// Mouseout event
$(".myClass a").live ("mouseout", function () {
    // If it already has the "selected" class applied to it, do nothing
    if ( $(this).is(".selected") {
        return;
    }
    else {
        // Do your mouseout effect (return to default state or whatever else)
        $(this).animate({height: "20px"}, 200);
    }

});
// Click Event
$(".myClass a").live("click", function () {
    // If it already has the "selected" class applied to it, do nothing
    if ( $(this).is(".selected") ) {
        return;
    }
    else {
        // Remove the selected class from any element that already has it
        $(".selected").removeClass(".selected");
        $(this).addClass("selected");
    }
});

类似这样的东西,或者像这样建模的东西应该可以工作。

Assuming your basic markup looks something like this:

<div id="div1" class="myClass"><a href="#">Div1</a></div>
<div id="div2" class="myClass"><a href="#">Div2</a></div>
<div id="div3" class="myClass"><a href="#">Div3</a></div>

You could do this:

// Mouseover event
$(".myClass a").live("mouseover", function () {
    // If it already has the "selected" class applied to it, do nothing
    if ( $(this).is(".selected") ) {
        return;
    }
    else {
        // Do your hover effect
        $(this).animate({height: "30px"}, 200);
    }
});

// Mouseout event
$(".myClass a").live ("mouseout", function () {
    // If it already has the "selected" class applied to it, do nothing
    if ( $(this).is(".selected") {
        return;
    }
    else {
        // Do your mouseout effect (return to default state or whatever else)
        $(this).animate({height: "20px"}, 200);
    }

});
// Click Event
$(".myClass a").live("click", function () {
    // If it already has the "selected" class applied to it, do nothing
    if ( $(this).is(".selected") ) {
        return;
    }
    else {
        // Remove the selected class from any element that already has it
        $(".selected").removeClass(".selected");
        $(this).addClass("selected");
    }
});

Something like this, or modeled like this should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文