jquery animate question
Ok so I have this form. In the form is an input field, that when you type a ? somewhere in the input value, I have a section of code that runs an onkeyup that slides out an element (.searchEnter) as well as changing some css.
<script>
$(".askInput").keyup(function() {
if ($(this).val().indexOf("?") != -1) {
开发者_如何学运维 $('.searchEnter').animate({
marginLeft: "310px"
}, 200 );
$('.form1').get(0).setAttribute('action', 'post.php');
} else {
$('.searchEnter').animate({
marginLeft: "250px"
}, 200 );
$('.form1').get(0).setAttribute('action', 'search.php');
}
});
</script>
When I just type a question mark in the input, .searchEnter slides out right away. However, the more characters that are preceding the question mark, (i.e. What is your name? (17 characters before ?) ), the longer it takes for the .animate to kick in and slide .searchEnter out. Why is this occurring, and how can I fix it?
Try $('.searchEnter').stop().animate ...
It's queueing all the animations. Call stop
before calling animate
.
So instead of this:
$(".searchEnter").animate(...
Do this:
$(".searchEnter").stop().animate(...
Also, I notice you're using get(0).setAttribute(...
. Why not just use attr
?
精彩评论