search
HomeWeb Front-endJS TutorialEasing effect implementation program in javascript_javascript skills

There are four common types of animations, let’s introduce them:
linear: linear animation, that is, constant speed
easeIn: speed from small to large, that is, fade in
easeOut: speed from large to small, that is, fade out
easeInOut: The speed is from small to large at the beginning, and the speed is from large to small at the end, that is, fade in and fade out

In fact, when it comes to easing, we have to mention Robert Penner, who invented the N-multiple easing formula. For example,

Let me explain it:

Suppose the current change amount is X, then
t / d = X / c, so X = c * t / d, and then Let’s look at a slightly more complicated one:

This has a fade-in effect, which means that when the animation starts, the value changes from small to large.
You can find that the only difference between the two is t / d and (t /= d) * t. I just said that t / d is a ratio of >=0 &&

Why square it?

1. First of all, a >= Math.pow(a, 2) is certain
2. Every time the function is called, the ratio of t / d also becomes larger at a constant speed, for example The first call is 0.1 (square 0.01), the second call is 0.2 (square 0.04), etc. Then the 10th call must be 1, right. At this time c * 1 b, the animation ends here Ended
3. Point 2 proves that the smaller the ratio, the smaller the change in value, and the larger the ratio, the greater the change in value. If square is not used but cubed, the fade-in effect will be more obvious. .

The style, structure and public functions are as follows:




#container {width:500px;height:100px;border:1px #d1d1d1 solid;position:relative;}
#drag {width:100px;height:100px;background :#369;position:absolute;left:0;top:0;}








First, start with the simplest: set the start position, end position and step size, each time Increase the fixed value until the termination condition


Copy code The code is as follows:var timer = null ;
var begin = 0, end = 400, step = 5;
var drag = $("drag");
function run() {
if((iLeft = parseInt(getStyle( drag,"left")))                                                      drag.style.left = iLeft step "px"; 🎜> }
var timer = setInterval(run, 20);


The above method is constant speed, and the distance of each movement is fixed. Let’s look at another implementation method:




Copy code

The code is as follows:


var timer = null;
var begin = 0, end = 400;
var drag = $("drag");
function run() {
if(( iLeft = parseInt(getStyle(drag,"left"))) var step = Math.ceil((400 - iLeft)/7);
drag.style.left = iLeft step " px";
                                                                                                                                                              >


The above method is to calculate the step length of the displacement based on the distance between the current position and the target.
There is a tween class in flash that specifically handles easing. The code converted into javascript is:


Copy code

The code is as follows:

var Tween = {
 Linear: function(t,b,c,d){ return c*t/d b; },
 Quad: {
  easeIn: function(t,b,c,d){
   return c*(t/=d)*t b;
  },
  easeOut: function(t,b,c,d){
   return -c *(t/=d)*(t-2) b;
  },
  easeInOut: function(t,b,c,d){
   if ((t/=d/2)    return -c/2 * ((--t)*(t-2) - 1) b;
  }
 },
 Cubic: {
  easeIn: function(t,b,c,d){
   return c*(t/=d)*t*t b;
  },
  easeOut: function(t,b,c,d){
   return c*((t=t/d-1)*t*t 1) b;
  },
  easeInOut: function(t,b,c,d){
   if ((t/=d/2)    return c/2*((t-=2)*t*t 2) b;
  }
 },
 Quart: {
  easeIn: function(t,b,c,d){
   return c*(t/=d)*t*t*t b;
  },
  easeOut: function(t,b,c,d){
   return -c * ((t=t/d-1)*t*t*t - 1) b;
  },
  easeInOut: function(t,b,c,d){
   if ((t/=d/2)    return -c/2 * ((t-=2)*t*t*t - 2) b;
  }
 },
 Quint: {
  easeIn: function(t,b,c,d){
   return c*(t/=d)*t*t*t*t b;
  },
  easeOut: function(t,b,c,d){
   return c*((t=t/d-1)*t*t*t*t 1) b;
  },
  easeInOut: function(t,b,c,d){
   if ((t/=d/2)    return c/2*((t-=2)*t*t*t*t 2) b;
  }
 },
 Sine: {
  easeIn: function(t,b,c,d){
   return -c * Math.cos(t/d * (Math.PI/2)) c b;
  },
  easeOut: function(t,b,c,d){
   return c * Math.sin(t/d * (Math.PI/2)) b;
  },
  easeInOut: function(t,b,c,d){
   return -c/2 * (Math.cos(Math.PI*t/d) - 1) b;
  }
 },
 Expo: {
  easeIn: function(t,b,c,d){
   return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) b;
  },
  easeOut: function(t,b,c,d){
   return (t==d) ? b c : c * (-Math.pow(2, -10 * t/d) 1) b;
  },
  easeInOut: function(t,b,c,d){
   if (t==0) return b;
   if (t==d) return b c;
   if ((t/=d/2)    return c/2 * (-Math.pow(2, -10 * --t) 2) b;
  }
 },
 Circ: {
  easeIn: function(t,b,c,d){
   return -c * (Math.sqrt(1 - (t/=d)*t) - 1) b;
  },
  easeOut: function(t,b,c,d){
   return c * Math.sqrt(1 - (t=t/d-1)*t) b;
  },
  easeInOut: function(t,b,c,d){
   if ((t/=d/2)    return c/2 * (Math.sqrt(1 - (t-=2)*t) 1) b;
  }
 },
 Elastic: {
  easeIn: function(t,b,c,d,a,p){
   if (t==0) return b;  if ((t/=d)==1) return b c;  if (!p) p=d*.3;
   if (!a || a    else var s = p/(2*Math.PI) * Math.asin (c/a);
   return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) b;
  },
  easeOut: function(t,b,c,d,a,p){
   if (t==0) return b;  if ((t/=d)==1) return b c;  if (!p) p=d*.3;
   if (!a || a    else var s = p/(2*Math.PI) * Math.asin (c/a);
   return (a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) c b);
  },
  easeInOut: function(t,b,c,d,a,p){
   if (t==0) return b;  if ((t/=d/2)==2) return b c;  if (!p) p=d*(.3*1.5);
   if (!a || a    else var s = p/(2*Math.PI) * Math.asin (c/a);
   if (t    return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 c b;
  }
 },
 Back: {
  easeIn: function(t,b,c,d,s){
   if (s == undefined) s = 1.70158;
   return c*(t/=d)*t*((s 1)*t - s) b;
  },
  easeOut: function(t,b,c,d,s){
   if (s == undefined) s = 1.70158;
   return c*((t=t/d-1)*t*((s 1)*t s) 1) b;
  },
  easeInOut: function(t,b,c,d,s){
   if (s == undefined) s = 1.70158;
   if ((t/=d/2)    return c/2*((t-=2)*t*(((s*=(1.525)) 1)*t s) 2) b;
  }
 },
Bounce: {
easeIn: function(t,b,c,d){
return c - Tween.Bounce.easeOut(d-t, 0, c, d) b;
},
easeOut: function(t,b,c,d){
if ((t/=d) return c*(7.5625*t*t) b ;
} else if (t return c*(7.5625*(t-=(1.5/2.75))*t .75) b;
} else if (t return c*(7.5625*(t-=(2.25/2.75))*t .9375) b;
} else {
return c*( 7.5625*(t-=(2.625/2.75))*t .984375) b;
}
},
easeInOut: function(t,b,c,d){
if (t else return Tween.Bounce.easeOut(t*2-d, 0, c, d ) * .5 c*.5 b;
}
}
}

Each easing method corresponds to 3 types
easeIn: starting from 0 Easing of acceleration;
easeOut: easing of decelerating to 0;
easeInOut: easing of accelerating from 0 in the first half and decelerating to 0 in the second half.
Parameter description:
t: current Time
b: Initial value
c: Change amount
d: Duration
Calling method:

Copy code The code is as follows:

var timer = null;
var b=0 ,c=400,d=100,t=0;
var drag = $("drag");
function run() {
drag.style.left = Math.ceil(Tween.Circ .easeInOut(t,b,c,d)) "px";
if(t   t ;
}else{
clearInterval(timer);
        }
}
var timer = setInterval(run, 20);

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Replace String Characters in JavaScriptReplace String Characters in JavaScriptMar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Custom Google Search API Setup TutorialCustom Google Search API Setup TutorialMar 04, 2025 am 01:06 AM

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

Build Your Own AJAX Web ApplicationsBuild Your Own AJAX Web ApplicationsMar 09, 2025 am 12:11 AM

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Example Colors JSON FileExample Colors JSON FileMar 03, 2025 am 12:35 AM

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

8 Stunning jQuery Page Layout Plugins8 Stunning jQuery Page Layout PluginsMar 06, 2025 am 12:48 AM

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

What is 'this' in JavaScript?What is 'this' in JavaScript?Mar 04, 2025 am 01:15 AM

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

Improve Your jQuery Knowledge with the Source ViewerImprove Your jQuery Knowledge with the Source ViewerMar 05, 2025 am 12:54 AM

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

10 Mobile Cheat Sheets for Mobile Development10 Mobile Cheat Sheets for Mobile DevelopmentMar 05, 2025 am 12:43 AM

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft