0

How do I add animations (both in and out) for the tooltips here:

$('.requiredField').each(function () {
    if ($.trim($(this).val()) == '') {
        var labelText = $(this).prev('label').text();
        $(this).parent().append('<div class="error">Please enter your ' + labelText + '!</div>');
        $(this).addClass('inputError');
        hasError = true;
    } else if ($(this).hasClass('email')) {
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,10})?$/;
        if (!emailReg.test($.trim($(this).val()))) {
            var labelText = $(this).prev('label').text();
            $(this).parent().append('<div class="error">Sorry! You\'ve entered an invalid ' + labelText + '.</div>');
            $(this).addClass('inputError');
            hasError = true;
        }
    }
}

Also, how do I add animation to this banner that pops out, once the form has been submitted? Notice, the form has a fadeOut animation. I would also like to animate the paragraph with class=info:

var formInput = $(this).serialize();
$.post($(this).attr('action'), formInput, function (data) {
    $('form#contact-us').fadeOut("fast", function () {
        $(this).before('<p class="info">Thanks! Your email has been submitted. Huzzah!</p>');
    })
});

Script: http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js

<script type="text/javascript">
    <!--//--><![CDATA[//><!--
    $(document).ready(function() {
        $('form#contact-us').submit(function() {
            $('form#contact-us .error').remove();
            var hasError = false;
            $('.requiredField').each(function() {
                if($.trim($(this).val()) == '') {
                    var labelText = $(this).prev('label').text();
                    $(this).parent().append('<div class="error">Please enter your '+labelText+'!</div>');
                    $(this).addClass('inputError');
                    hasError = true;
                } else if($(this).hasClass('email')) {
                    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,10})?$/;
                    if(!emailReg.test($.trim($(this).val()))) {
                        var labelText = $(this).prev('label').text();
                        $(this).parent().append('<div class="error">Sorry! You\'ve entered an invalid '+labelText+'.</div>');
                        $(this).addClass('inputError');
                        hasError = true;
                    }
                }
            });
            if(!hasError) {
                var formInput = $(this).serialize();
                $.post($(this).attr('action'),formInput, function(data){
                    $('form#contact-us').fadeOut("fast", function() {                  
                        $(this).before('<p class="info">Thanks! Your email has been submitted. Huzzah!</p>');
                    });
                });
            }

            return false;   
        });
    });
    //-->!]]>
</script>
4

2 回答 2

2

为了动画附加元素,你可以简单地反过来。

代替:

someElement.append('<div>myElement</div>');

做:

$('<div>myElement</div>').hide().appendTo(someElement).show('slow');

如果您仍想使用append()or before(),您可以将新元素分配给变量,如下所示:

原来的:

$(this).before('<p class="info">Thanks! ... Huzzah!</p>');

修改的:

var el = $('<p class="info">Thanks! ... Huzzah!</p>').hide();
$(this).before(el);
el.show('slow');

此方法应该适用于您的所有实例。您可以更改show()为您想要的效果,例如fadeIn().

演示

于 2013-09-02T00:18:03.970 回答
1

为要附加的 div 设置动画的最简单方法是将它们设置为 0 宽度或高度,或 display:none,然后在附加它们后进行动画处理。一个简单的例子在这里 - http://jsfiddle.net/Bquyw/1

这类似于:

$('div').append('<div class="error">Please enter your !</div>');
$('.error').animate({'width':'200px'},1000,'easeOutBounce')

您可能也对这个 codePen 感兴趣 - http://codepen.io/lukeocom/pen/Dewdo

于 2013-09-02T00:19:42.340 回答