【问题标题】:Toggle on off click state after function execution jquery在函数执行jquery后切换到关闭点击状态
【发布时间】:2014-03-02 08:49:34
【问题描述】:

我对按钮的行为有疑问。该按钮在 mouseover mouseleave 状态下切换,并在单击时触发另一个功能,该功能会滑动以打字机方式动画的文本。

我想做的是在播放动画时禁用/切换点击状态(如果您多次点击,您可以看到字符串会发生什么情况)并在动画完成后重新启用它。

我搜索了类似的问题,并且已经尝试实现其中的一些逻辑,但似乎无法使其发挥作用。也许问题不在于按钮,而在于字符串的执行方式,我非常感谢任何帮助,这让我发疯了!

真诚的感谢和最好的问候!

$(document).ready(function () {
$('#btn-icon').on("mouseover mouseleave", expand);

$('#btn-icon').on("click", function (evt) {
    $('#btn-icon').off("mouseover mouseleave", expand);
    expandAll();
});

});

function expand(evt) {
$('#btn-icon').toggleClass("expand");
$('#btn-text').fadeToggle('2000');

console.log('expand');

}

完整代码/jsfiddle:

http://jsfiddle.net/shinobisan/vCVVZ/1/

【问题讨论】:

    标签: jquery html button mouseevent toggle


    【解决方案1】:

    您可以使用变量来指示动画当前是否正在运行 (see the fiddle):

    var isActive = false;
    $(document).ready(function () {
        $('#btn-icon').off("mouseover mouseleave", expand);
        if( !isActive) {isActive = true; expandAll(); }
    });
    
     // and inside expandAll when animation is done, 
     // you set the isActive to false again.
     $.each(para_text.split(''), function (i, letter) {
        setTimeout(function () {
        }, 100 * i, function () {
            isActive = false;
        });
    });
    

    用 .stop 取消动画也是个好主意。

    【讨论】:

    • 改用mouseenter
    • 效果很好!只是它在最后一个函数上没有返回 false,可能是因为循环?所以我把它放在当 div 向上滑动时被调用的函数中,现在它可以工作了!! link 非常感谢!
    • @Buljan 是对的,提到了 mouseenter,将 mouseover 替换为 mouseenter,因为您使用的是 mouseleave。
    • 感谢您指出@Buljan,@Michel,我没有意识到这种区别,据我所知,mouseenter 与 mouseover 的不同之处在于它仅由选定元素触发,而不是由它的子元素触发. (如果我错了,请纠正我)
    • @user3330769 是的,就是这样,所以最好使用 mouseover/mouseout 或 mouseenter/mouseleave 否则可能会出现一些奇怪的副作用,具体取决于您是否有子元素或不是。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    相关资源
    最近更新 更多