【问题标题】:troubles with fadeIn()/fadeOut(); jqueryfadeIn()/fadeOut() 的问题; jQuery
【发布时间】:2012-11-02 23:35:22
【问题描述】:

通过“oncklick”函数获得了名为“notebutt”的按钮

功能很简单,用户点击按钮,div为fadeIn(),再次点击,div必须fadeout();

        notebutt.bind("click", function () {
        var notediv = $(this).parent().find("div.notediv");
        // checking is notediv exist already, if not, creating one and do fadeIn(150);
        if (!notediv) {
        notediv = $('<div class="notediv" contenteditable="true"></div>');
        notediv.appendTo($(this).parent());
        notediv.offset({top: posT-47}).fadeIn(150);
        } else {
        // if got notediv created before, i must show or hide it with hideorshow(notediv);
                hideorshow(notediv);
        }

});
    // func that check's is div was showned or not
function hideorshow(div){
if ($(div).is(':visible')) {
    //hide if visible
    div.fadeOut();
} else {
        div.offset({top: posT-47});
        div.fadeIn();
}
};

乍一看它应该可以正常工作,但是在单击按钮 5 到 10 次后都会出错, div 在单击时随机闪烁,就像fadeIn 和fadeOut 同时运行或彼此运行一样 有什么方法可以让一些适当的触发器来检查 div 状态吗?

【问题讨论】:

  • 使用 toggle() - api.jquery.com/toggle
  • 我需要类似 var status = 0;
  • 稍后使用 if (status == 1) { do something and hide div} else { do other things} 但我不知道如何使其工作

标签: jquery fadein fadeout eventtrigger


【解决方案1】:

在您的示例中,notediv 已经是一个 jquery 对象:

function hideorshow(div){
if (div.is(':visible')) { //replace $(div) by div
    //hide if visible
    div.fadeOut();
} else {
        div.offset({top: posT-47});
        div.fadeIn();
}
};

【讨论】:

  • 不工作同样的麻烦,首先工作正常,但随后开始出现故障
猜你喜欢
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-20
  • 1970-01-01
相关资源
最近更新 更多