Jquery animation-4. Image corridor with upgraded masked effects-Automatic Running Effect

Source: Internet
Author: User

I upgraded the plug-in the previous chapter to achieve automatic running and completeCodeSee the demo.

The main changes are as follows: Abstract The click events of the 'Next' and 'upload' to a function shownext. Add setinterval to add the hover event of the selector element. Okay, let's look at it one by one.

Shownext function:

 //  Display function  Function  Shownext (FLAG ){  //  Hide navigation Parameters (config.selector).find('a'hangzhou.css ('display', 'none' );  //  Create a mask  $. Tranzify. createoverlay (config );  // Obtain the currently displayed image.      VaR Currimg = $ ('.' + Config. visibleclass, $ (config. selector ));  If (Flag = True  ){  //  The current image is not the first image          If (Currimg. Prev (). Filter ('img '). length> 0 ){  //  Set the previous image to display Currimg. removeclass (config. visibleclass). Prev (). addclass (config. visibleclass );}  Else  {  //  Set the last image to display  Currimg. removeclass (config. visibleclass); $ (config. selector). Find ( 'Img '). eq (imglength-1 ). Addclass (config. visibleclass );}}  Else  {  //  The current image is not the last one.          If (Currimg. Next (). Filter ('img '). length> 0 ){  //  Set the next image to display  Currimg. removeclass (config. visibleclass). Next (). addclass (config. visibleclass );}  Else  {  //  Set the first image to display  Currimg. removeclass (config. visibleclass); $ (config. selector). Find ( 'Img '). eq (0 ). Addclass (config. visibleclass );}}  // Run the mask effect  $. Tranzify. runtransition (config );} 

The reason why we need to extract it is because it is also used in the setinterval below, and we do not want to have too many repeated code, so we need to extract it for unified management. Next let's take a look at setintervale.

 
//Set cyclic FunctionsConfig. interval = setinterval (shownext, (config. multi * 150) + 3000 );

In fact, it is to add a loop function, how many seconds each time, run the shownext function once, and display the next image. Note that the interval is calculated. Do you still remember the interval set for the animate object in the runtransition function in the previous chapter? Set the horizontal display to slow, that is, 600 Ms. The vertical display time of each frame is set to 150 ms, a total of config. multi frames. (Config. multi * 150) must be greater than 600, so here we take (config. multi * 150) as the time standard. To prevent the animation from being run, we add an additional 3 seconds to display the next image.

After adding the cyclic function, we add a hover event for the selector object. When the mouse moves over the object, interval is removed, and interval is added when the mouse moves out.

//Move the mouse over the object to remove the cyclic function; move the mouse out to add the cyclic Function$ (Config. selector). Hover (Function() {Clearinterval (config. interval );},Function() {Config. Interval= Setinterval (shownext, (config. multi * 150) + 3000);});

Here, we should note that the object created by setinterval is passed to config. interval. In this way, to ensure that the cyclic function created in the previous step is the same object as the deleted cyclic function. It is very taboo to pass the object created by setinterval directly to the undeclared interval, like this:

 
Interval = setinterval (shownext, (config. multi * 150) + 3000 );

In this way, the window object's interval is passed, which can easily cause code conflicts. For example, if another code or plug-in also creates a setinterval object, it is also passed to interval (equivalent to window. interval), then our plug-in clearinterval will affect the normal operation of other people's code, similarly, other people's code will also affect us.

Finally, for code robustness, we added the following code to createoverlay to ensure that there is only one mask layer at the same time:

//Determine whether there is a completed animation. If there is a completed animation, immediately end the animation and remove the object.VaRTransoverlay = $ ('#' +Config. containerid );If(Transoverlay) {transoverlay. Stop (True,True); Transoverlay. Remove ();}

All right, the objects to be explained are all finished. Let's look at the demo directly to see the effect.

Demo: jquery.animation.tranzify_improve.js.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.