Home > Article > Web Front-end > Use jQuery's animation function animate to achieve the pea launching effect
Peashooter, lawn and bullets are all ready-made images.
1. Is jQuery a library or a framework?
jQuery can be said to be the most popular js library now, rather than a framework.
I saw someone say something like this on Zhihu before:
You call library. Framework calls you.
I think so, the literal meaning is probably you Class libraries can be used without restrictions, but a framework needs to be used under various restrictions.
I personally think that the js library refers to an API that directly interacts with document document elements. You can directly reference the library and let it serve you. The framework is biased towards the architectural level. If you want to use the framework, you must follow its rules. For example, angular.js provides you with methods while also constraining the DOM document structure.
Take the three major frameworks of Java as an example. The same is true. If you want to use Spring, you have to follow its steps. It is like a house. The reinforced concrete has been completed. You only need to go in and decorate it. . The library is somewhat similar to StringUtils. Except for the interface it exposes, you don't need to care about anything else, just call it directly.
2. jQuery’s animate function
The animate() function is used to execute a custom animation based on css attributes
Basic usage:
$('#id').animate(css,time,callback);
css: List of styles you need to finally implement
time: Transition time
callback: callback function
The main function of the animate function is to achieve some CSS style transition effects.
3. Introduce jQuery
For example, now I have a div box.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <style type="text/css"> #box { width: 300px; height: 300px; background:greenyellow; } </style> </head> <body> <div id='box'></div> </body> <script> </script> </html>
In the above code, we introduced the jQuery file provided by Baidu.
So how to quickly determine whether the introduction is successful? The following methods are provided:
1.console.log($);
Effect:
This shows that the introduction was successful.
2. Directly use the browser to verify
Open your page, press F12, and a console like this will appear. This is what the browser comes with (I am using Google Chrome).
Enter $
and press Enter!
Hey, is this okay?
4. onmouseover event
Let’s add a mouse over event to the div box.
$('#box').on('mouseover',function(){ alert(); });
Cross it:
Well, at least, this shows that our code so far is correct, I I liked this when I first learned JS. It made me feel confident that every line of code was written.
5. Use the animate function to change the width and height of the box
We remove the alert and add the following code:
$('#box').on('mouseover',function(){ $('#box').animate({width:600},500); });
This means that when I draw the mouse When you go up, change the width to 600px and the transition time to 500 milliseconds.
What if we want to double the height after doubling the width?
By the way, use the callback function. When the first animation is completed, continue to execute the next animation:
$('#box').on('mouseover',function(){ $('#box').animate({width:600},500,function(){ $('#box').animate({height:600},500); }); });
This way there is a sequence order.
This article briefly introduces the use of jQuery animate function.
6. Appendix
Finally, the code of the initial case is attached. In addition to the animate function, the js timer setInterval method is also used:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script type="text/javascript" src="jquery-1.11.2.min.js"></script> <style type="text/css"> body { background: url(background1.jpg) no-repeat; position: fixed; } ul li { list-style: none; } .wrap { position: relative; left: 170px; top: 65px; } .plants1 { display: inline-block; position: relative; left:35px; } .plants1 .plant { position: relative; margin-bottom:20px; } .plants1 .plant .PB00 { position: absolute; top:-2px; left:15px; } .plants2 { display: inline-block; position: relative; left:2px; } .plants2 .plant { position: relative; margin-bottom:20px; } .plants2 .plant .PB00 { position: absolute; top:-2px; left:15px; } .plants3 { display: inline-block; position: relative; left:-40px; } .plants3 .plant { position: relative; margin-bottom:20px; } .plants3 .plant .PB00 { position: absolute; top:-2px; left:15px; } </style> </head> <body> <div class='wrap'> <ul class='plants1'> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > <img class='PB00' src="img/PB00.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > <img class='PB00' src="img/PB00.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > <img class='PB00' src="img/PB00.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > <img class='PB00' src="img/PB00.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > <img class='PB00' src="img/PB00.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > </li> </ul> <ul class='plants2'> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > <img class='PB00' src="img/PB00.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > <img class='PB00' src="img/PB00.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > <img class='PB00' src="img/PB00.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > <img class='PB00' src="img/PB00.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > <img class='PB00' src="img/PB00.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > </li> </ul> <ul class='plants3'> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > <img class='PB00' src="img/PB00.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > <img class='PB00' src="img/PB00.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > <img class='PB00' src="img/PB00.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > <img class='PB00' src="img/PB00.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > <img class='PB00' src="img/PB00.gif" / alt="Use jQuery's animation function animate to achieve the pea launching effect" > </li> </ul> </div> </body> <script type="text/javascript"> function randomNum(num){ return Math.floor(Math.random()*(num+1)); }; setInterval(function(){ var $this = $('.PB00').eq(randomNum(17)); $this.animate({'margin-left' : 1000},2000,function(){ $this.css({'margin-left' : 0}); }); },10); </script> </html>
Summary
The above is the entire content of this article. I hope it can be helpful to everyone’s study and work. If you have any questions, you can leave a message to communicate. Thank you for your support of the PHP Chinese website.
For more related articles using jQuery’s animation function animate to achieve the pea launching effect, please pay attention to the PHP Chinese website!