Jquery - 带removeClass的点击事件

发布于 2025-01-07 05:38:36 字数 2156 浏览 0 评论 0原文

我正在创建一个滑块主体,但遇到了问题。我有一个可以向前和向后滑动的下一个“按钮”。单击此按钮时,它将删除使其执行向前和向后动画的类,但是当我删除该类时,动画仍然有效。为什么?

制作内容:

    esq = 0;

    var navR,navL   = false;

$(".caixa_pequena").each(function(){
    var $element = $(this).css('left'); 
    final = parseFloat($element) + parseFloat(esq); 

    $(this).css('left', parseFloat(final) + 'px');
    esq = esq  + 200;   
});

前进按钮:

$('.nav_depois').click(function(){
    desactEsq();
    desactDir();
    var nr = $(".caixa_pequena").size()-1;
    var total = (nr * (parseInt(-200)))+ parseInt($(".caixa_pequena").css('left')) + 'px';
    var m = $(".caixa_pequena").offset().left+'px'; 


    if(total != m){
        $("#texto").animate({left: '-=200'}, 'slow', function(){
            /*actEsq();
            actDir();*/
        }); 
    }   
});

后退按钮

$('.nav_antes').click(function(){
    desactEsq();
    desactDir();
   var l = $(".caixa_pequena").offset().left+'px';
    if(l != $(".caixa_pequena").css('left')){  
       $("#texto").animate({left: '+=200'}, 'slow', function(){
        /*actEsq();
        actDir();*/    
       });
    }       
});

激活/停用

function desactDir () {
    navR = false;
    $('#nav_panel').find('.nav_depois').removeClass();
}
function desactEsq () {
    navL = false;
    $('#nav_panel').find('.nav_antes').removeClass();
}
function actDir () {
    navR = true;
    $('#nav_panel').find('#nav_next').addClass('nav_depois');
}
function actEsq () {
    navL = true;
    $('#nav_panel').find('#nav_prev').addClass('nav_antes');
}
});

html

<div id="caixa_grande">
<div id="texto">    
    <div class="caixa_pequena">SPORT LISBOA E BENFICA</div>
    <div class="caixa_pequena">SPORTING CLUBE DE PORTUGAL</div>
    <div class="caixa_pequena">FUTEBOL CLUBE DOS COXOS</div>
</div>  
</div>
<div id="nav_panel">
    <a id="nav_next" class="nav_antes"><-------|</a>
    <a id="nav_prev" class="nav_depois">|--------></a>
</div>

im creating a slider body but got a problem. I have a next previous "button" that will slide forward and backward. When this buttons is clicked it will removed the class that make it to do the animation to forward and backward, but when i remove the class, the animation is still working. why?

making the stuff:

    esq = 0;

    var navR,navL   = false;

$(".caixa_pequena").each(function(){
    var $element = $(this).css('left'); 
    final = parseFloat($element) + parseFloat(esq); 

    $(this).css('left', parseFloat(final) + 'px');
    esq = esq  + 200;   
});

the forward button:

$('.nav_depois').click(function(){
    desactEsq();
    desactDir();
    var nr = $(".caixa_pequena").size()-1;
    var total = (nr * (parseInt(-200)))+ parseInt($(".caixa_pequena").css('left')) + 'px';
    var m = $(".caixa_pequena").offset().left+'px'; 


    if(total != m){
        $("#texto").animate({left: '-=200'}, 'slow', function(){
            /*actEsq();
            actDir();*/
        }); 
    }   
});

the backward button

$('.nav_antes').click(function(){
    desactEsq();
    desactDir();
   var l = $(".caixa_pequena").offset().left+'px';
    if(l != $(".caixa_pequena").css('left')){  
       $("#texto").animate({left: '+=200'}, 'slow', function(){
        /*actEsq();
        actDir();*/    
       });
    }       
});

the active/desactivate

function desactDir () {
    navR = false;
    $('#nav_panel').find('.nav_depois').removeClass();
}
function desactEsq () {
    navL = false;
    $('#nav_panel').find('.nav_antes').removeClass();
}
function actDir () {
    navR = true;
    $('#nav_panel').find('#nav_next').addClass('nav_depois');
}
function actEsq () {
    navL = true;
    $('#nav_panel').find('#nav_prev').addClass('nav_antes');
}
});

the html

<div id="caixa_grande">
<div id="texto">    
    <div class="caixa_pequena">SPORT LISBOA E BENFICA</div>
    <div class="caixa_pequena">SPORTING CLUBE DE PORTUGAL</div>
    <div class="caixa_pequena">FUTEBOL CLUBE DOS COXOS</div>
</div>  
</div>
<div id="nav_panel">
    <a id="nav_next" class="nav_antes"><-------|</a>
    <a id="nav_prev" class="nav_depois">|--------></a>
</div>

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

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

发布评论

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

评论(1

原谅我要高飞 2025-01-14 05:38:36

click 方法绑定到元素上,当您执行此操作时,

$('.nav_depois').click(function(){
//code...
});

jQuery 将搜索具有 nav_depois 类的每个元素,并设置您传递的函数,以便在发生单击时执行该函数。

更清楚地说,它绑定了事件,因此

jQuery("element").click(f);

翻译为

jQuery("element").bind("click", f);

要删除该方法,您应该使用 unbind (或者 off 如果您使用的是 jQuery 1.7),

function desactDir () {
    navR = false;
    $('#nav_panel').find('.nav_depois').unbind("click");
}

例如重新附加它,您应该再次绑定它(使用单击或绑定方法),为此,我建议存储该函数并将其作为参数传递,例如:

var navAntesFunction = function (){
//code
}

$('.nav_antes').click(navAntesFunction);

function actEsq () {
    navL = true;
    $('#nav_panel').find('#nav_prev').click(navAntesFunction);
}

The click method is binded to the element, when you do

$('.nav_depois').click(function(){
//code...
});

jQuery will search for every element with a class of nav_depois and set the function you passed so it will be executed when a click occurs.

To be more clear it's binding the event, so

jQuery("element").click(f);

translates to

jQuery("element").bind("click", f);

To remove the method you should use unbind (or off if you're using jQuery 1.7), for example

function desactDir () {
    navR = false;
    $('#nav_panel').find('.nav_depois').unbind("click");
}

and to reattach it you should bind it again (with the click or bind methods), to do that I recommend storing the function and passing it as a parameter like:

var navAntesFunction = function (){
//code
}

$('.nav_antes').click(navAntesFunction);

function actEsq () {
    navL = true;
    $('#nav_panel').find('#nav_prev').click(navAntesFunction);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文