如果用户单击正文,则滑动动画div

时间:2013-11-20 11:47:53

标签: jquery click jquery-animate

我正在创建一个反馈表单。问题的描述如下:

我希望当用户点击body任意位置以及与我们联系时,div应该向后滑动。我试过这样的。

$(document).click(function () {
    $('div.slider').animate({
        toggle: "width"
    }, 1000, function () {

    });
});

但在这种情况下,当我尝试click时,div会来回滑动。 我google了一下,但我遇到的问题是如何搜索关键字描述我的问题的方法 我不想要这个。 在此Fiddle中添加指向我的代码的链接。


Pete给出了这个问题的答案 谢谢你。皮特 链接到Fiddle is Here

1 个答案:

答案 0 :(得分:0)

以下是适合您的脚本:http://jsfiddle.net/aamir/cQswC/12/

$(document).ready(function(){
var animation = "155px";
$('div.right, .slider').click(function(e){e.stopPropagation();});
$('div.right').click(function(e){
    e.stopPropagation();
    $(this).animate({
        right:animation
    },200,function(){
        if(animation === "155px")
            animation = "-40px";
        else
            animation = "155px";
        console.log("Animation Complete");
    });
    $('div.slider').animate({
        width:"toggle"
    },200, function(){
        console.log("Slider Animation Complter");
    });
});

//catch the event and check if element clicked is not the toggle label
$('html').click(function (e) {
    if(animation =='155px') return;
    if (!$('.right').is($(e.target))) {
        $('.right').click();
    }
});

});

相关问题