jQuery - 如果element有类这样做

Adr*_*scu 103 javascript jquery

我需要一个jQuery脚本,它将查看是否有任何元素具有特定的类并执行更改位置等操作.

这是方法,但我认为这不会奏效.

$("a.contact").toggle(function() {
    $("#contact").animate({
        right: '0'
    }, 2000);

    if ($("#about").hasClass("opened")) {
        $("#about").animate({
            right: -700 + "px"
        }, 2000);
    }
}, function() {
    $("#contact").animate({
        right: -700 + "px"
    }, 2000);
});
Run Code Online (Sandbox Code Playgroud)

Ken*_*ler 177

首先,你在条件中遗漏了一些括号:

if ($("#about").hasClass("opened")) {
  $("#about").animate({right: "-700px"}, 2000);
}
Run Code Online (Sandbox Code Playgroud)

但您也可以将其简化为:

$('#about.opened').animate(...);
Run Code Online (Sandbox Code Playgroud)

如果#about没有opened该类,它将不会动画.

如果问题出在动画本身,我们需要更多地了解你的元素定位(绝对?绝对内部相对父级?父级是否有布局?)