【问题标题】:How do I toggle back on a function I turned off如何切换回我关闭的功能
【发布时间】:2014-05-27 05:10:50
【问题描述】:

当我点击article.hope 时,我希望我之前给它们的mouseentermouseleave 功能关闭。这我得到了它的工作,但一旦我单击article.hope 兄弟姐妹之一,我就无法重新打开此功能。

这是我目前所拥有的。

$("article.hope").mouseenter(function(){
        $(this).children('div').fadeIn(0);
        $(this).children('aside').fadeOut(0);   
        });

        $("article.hope").mouseleave(function(){
            $(this).children('div').fadeOut(0);
            $(this).children('aside').fadeIn(0);

        });

$("article.rama").mouseenter(function(){
        $(this).children('div').fadeIn(0);
        $(this).children('aside').fadeOut(0);   
        });

        $("article.rama").mouseleave(function(){
            $(this).children('div').fadeOut(0);
            $(this).children('aside').fadeIn(0);

        });





$("article.hope").click(function(){
        $(this).off('mouseenter mouseleave');
        $(this).siblings('article').on('mouseenter mouseleave')
        $('section.hope').slideDown(0500, 'swing')
        $('section.hope').siblings('section').fadeOut(0)
        $(this).siblings('article').fadeIn(0);
        });

$("article.rama").click(function(){
        $(this).off('mouseenter mouseleave');
        $(this).siblings('article').on('mouseenter mouseleave')
        $('section.rama').slideDown(0500, 'swing')
        $('section.rama').siblings('section').fadeOut(0)
        $(this).siblings('article').fadeIn(0);
        });     

所以当我点击article.hopemouseentermouseleave 关闭就好了,但我似乎无法重新打开它们。

编辑:.removeclass/.addclass

HTML

<article class="hope">
   <aside><!--background-image--></aside>
        <div>
            <h2>projet personnel</h2>

            <p>First contact</p>
        </div>
  </article>

CSS

article {
    width:100%;
    height:210px;
    /*background-color:#f7383e;*/
    color:#f0f0f0;
    text-decoration: none;
    margin:0 auto;
    text-align:center;
    overflow:hidden;


}

article div {
    /*max-width:980px;*/
    margin:0 auto;
    text-align:center;
    overflow:hidden;
    position:relative;
    top: 50%;
    transform: translateY(-50%);
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    display:none;


}


article div h2 {
    letter-spacing:2px;
    font-size:130%;
    font-family:'ostrich_sansmedium', Arial, Helvetica, sans-serif;


}

article p {
    margin:10px 0;
    padding:5px;
    display:inline-block; 
    font-family:'nevis',Arial, Helvetica, sans-serif;
    text-transform:uppercase;
    font-size:150%;
    border-top:2px solid #f0f0f0;

}



    article.hope aside {
        height:210px;
        background-image:url(../images/hope.jpg);
        background-position: 50% 50%;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;
        background-repeat:no-repeat;

    }

JQUERY

$("article.hope").mouseenter(function(){
        $(this).children('div').fadeIn(0);
        $(this).children('aside').fadeOut(0);   
        });

        $("article.hope").mouseleave(function(){
            $(this).children('div').fadeOut(0);
            $(this).children('aside').fadeIn(0);

        });

$("article.hope").click(function(){
        $(this).removeclass("hope");
        $(this).siblings('article').addclass("hope");

        $('section.hope').slideDown(0500, 'swing')
        $('section.hope').siblings('section').fadeOut(0)
        $(this).siblings('article').fadeIn(0);
        });

我很确定我需要在某个地方开设一个新课程才能让它工作,但我不确定在哪里......

【问题讨论】:

  • 那是因为你用.on()附加了事件处理程序,但没有指定函数。
  • 一旦你分离了事件,你必须明确地重新绑定它,它不仅仅记得它曾经是怎样的。这就像使用橡皮擦并删除它。这是unbound。所以你需要做的就是把它变成一个函数,比如function do_mouseenter($ele),然后你就可以像do_mouseenter($(this));一样调用它。
  • 一个更简单的方法就是向相关元素添加/删除一个类。因此,当您将鼠标悬停时,请检查该类是否存在并执行所需的任何操作。您可以完全避免解除绑定/重新绑定
  • @Huangism 我试过了,由于某种原因可以让它工作。我想这是因为我的mouseenter mouseleave 和我的click 共享同一个班级。
  • @Ohgodwhy 感谢您的快速响应!这很有意义,但是你能给我一个使用我的代码的例子吗?我对此有点陌生。

标签: jquery


【解决方案1】:

我已经按照你的逻辑重写了你的脚本。我所做的更改是:

  1. 先声明enter()leave()函数,然后将它们绑定到触发悬停事件时调用的匿名函数
  2. 链接方法,因此当多行使用相同的选择器时,您不必让 jQuery 一遍又一遍地搜索元素

这里是:

// Declare functions onMouseEnter and onMouseLeave
var enter = function($ele) {
    $ele
    .children('div')
        .fadeIn(0)
        .end()
    .children('aside')
        .fadeOut(0)
        .end(); // Optional but I use it for consistency
},  leave = function($ele) {
    $ele
    .children('div')
        .fadeOut(0)
        .end()
    .children('aside')
        .fadeIn(0)
        .end(); // Optional but I use it for consistency
};

// Bind functions to correct event handlers
// We can concatenate mouseenter and mouseleave with hover
$('article.hope, article.rama').hover(function() {
    enter($(this));
}, function() {
    leave($(this));
}).click(function() {
    // Turn off mouse events when clicked
    // Turn on mouse events for siblings
    $(this)
    .off('mouseenter mouseleave')
    .siblings('article')
        .fadeIn(0)
        .hover(function() {
            enter($(this));
        }, function() {
            leave($(this));
        });

    // Now perform stuff for <section>
    $('section.'+$(this).attr('class'))
    .slideDown(500, 'swing')
    .siblings('section')
        .fadeOut(0);

});

【讨论】:

  • 谢谢。但这似乎也不起作用...@ 987654324@ background-image 在我点击article.rama 后不会重新出现...
  • 等一下。它确实有效,但它并没有完全按照我的意愿去做。在单击 article.rama 后,我需要将 article.hope 悬停一次,以便背景图像重新出现并且 mouseenter/mouseleave 再次工作。有没有办法做到这一点,所以我只需要点击 article.rama 让它正常工作吗?无需再次悬停。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多