


JavaScript small animation components and implementation code_PHP tutorial
How to create a common animation effect using js? Take a look at the example
setInterval (function(){
element.style.left =parseFloat(element.style.left) +(n) +'px';
},10);
[Ctrl+A to select all Note: If you need to introduce external Js, you need to refresh to execute]
Use window.setInterval animation function, every 10 milliseconds The animation will be executed once;
and set are matched with the clearInterval function, which is used to end the animation.
Every setInterval will return a value similar to the thread id;
var interval =setInterval(function(){
element.style.left =parseFloat(element.style.left) +(n) + 'px';
},10);
Use clearInterval (interval) to end animation playback.
interval = setInterval(function(){
if(parseFloat(element.style.left) >500) clearInterval(interval)
element.style.left =parseFloat(element.style.left) +2 +'px';
},10);
When it exceeds 500px, the animation will will stop and the element will no longer move.
[Ctrl+A Select all Note: If you need to introduce external Js, you need to refresh to execute]
But the above animation is relatively stiff, and then we There is another kind of timeline animation.
Look at the example:
var element = document.getElementById('test1');
var start = +new Date,dur=1000,finish = start+dur;
interval = setInterval(function( ){
var time = +new Date,
pos = time > finish ? 1 : (time-start)/dur;
element.style.left = (100*pos)+"px" ;
if(time>finish) {
clearInterval(interval);
}
},10);
start is the start time of the target animation ( +new Date is actually new Date( ).getTime() )
dur is the total time required for animation execution
finish is the end time of the target animation
pos = time > finish ? 1 : (time-start)/dur; // You can think of pos as frequency, a time ratio
(100*pos), 100 represents the distance, if the distance is 500px, set it to 500*pos;
time>finish: If it exceeds the time, stop the animation!
[Ctrl+A to select all Note: If you need to introduce external Js, you need to refresh to execute]
Very good, we already know a simple animation here How to write the effect.
Let’s look at how to write a small complete animation component:
(function($,name){
var parseEl = document.createElement('div')
,
props = ('backgroundColor borderBottomColor borderBottomWidth borderLeftColor borderLeftWidth '+
'borderRightColor borderRightWidth borderSpacing borderTopColor borderTopWidth bottom color fontSize '+
'fontWeight height left letterSpacing lineHeight marginBottom marginLeft marginRight marginTop maxHeight '+
'maxWidth minHeight minWidth opacity outlineColor outlineOffset outlineWidth paddingBottom paddingLeft '+
'paddingRight paddingTop right textIndent top width wordSpacing zIndex').split(' ')
,
normalize =function (style){
var css,
rules = {},
i = props.length,
v;
parseEl.innerHTML = '';
css = parseEl.childNodes[0].style;
while(i--) if(v = css[props[i]]) rules[props[i]] = parse(v);
return rules;
},
color = function(source,target,pos){
var i = 2, j, c, tmp, v = [], r = [];
while(j=3,c=arguments[i-1],i--)
if(s(c,0)=='r') { c = c.match(/d+/g); while(j--) v.push(~~c[j]); } else {
if(c.length==4) c='#'+s(c,1)+s(c,1)+s(c,2)+s(c,2)+s(c,3)+s(c,3);
while(j--) v.push(parseInt(s(c,1+j*2,2), 16)); }
while(j--) { tmp = ~~(v[j+3]+(v[j]-v[j+3])*pos); r.push(tmp255?255:tmp); }
return 'rgb('+r.join(',')+')';
},
parse = function(prop){
var p = parseFloat(prop), q = prop.replace(/^[-d.]+/,'');
return isNaN(p) ? { v: q, f: color, u: ''} : { v: p, f: interpolate, u: q };
},
s = function(str, p, c){
return str.substr(p,c||1);//color 用
},
interpolate =function(source,target,pos){
return (source+(target-source)*pos).toFixed(3);
},
flower = function(el, style,opts,after){
var el = document.getElementById(el), //通过id获取元素对象
opts = opts || {},
target = normalize(style),
comp = el.currentStyle ? el.currentStyle : getComputedStyle(el, null), //ie和w3c兼容,获取样式
prop,
current = {},
start = +new Date, //开始时间
dur = opts.duration||200, //执行事件,默认为200
finish = start+dur, //结束时间
interval,
easing = opts.easing || function(pos){ return (-Math.cos(pos*Math.PI)/2) + 0.5; };
for(prop in target) current[prop] = parse(comp[prop]);
interval = setInterval(function(){
var time = +new Date,
pos = time>finish ? 1 : (time-start)/dur;
for(prop in target){
el.style[prop] = target[prop].f(current[prop].v,target[prop].v,easing(pos)) + target[prop].u;
}
if(time>finish) {
clearInterval(interval); opts.after && opts.after(); after && setTimeout(after,1);
}
},10);
};
$[name] = flower;
})(window,"flower");
var parse = function(prop){
var p = parseFloat(prop), q = prop.replace(/^[-d.]+/,'');
return isNaN(p) ? { v: q, f: color, u: ''} : { v: p, f: interpolate, u: q };
}
var p = parseFloat(prop) 意思是 : 500px => 500;
q = prop.replace(/^[-d.]+/,''); 500px => px;
return isNaN(p) ? { v: q, f: color, u: ''} : { v: p, f: interpolate, u: q }; 意思是 如果取的是颜色值(因为带有#号),返回{ v: q, f: color, u: ''} u 代表代为,f是一个color函数(后面会讲到);
var s = function(str, p, c){ return str.substr(p,c||1); }
s function is used to intercept the string and return the final result. The
color function uniformly returns the color value in the form of "rgb(x,x,x)". The
normalize function returns a json object. , the object contains the name and value of the css attribute to be executed by the element
while(i--) if(v = css[props[i]]) rules[props[i]] = parse(v);
Tear apart a line of code and see how it works
while(i--){
//A = sign is used here, and the assignment operation is performed first. If it does not exist, the if will not pass, killing two birds with one stone: )
if(v = css[props[i]]){
rules[props[i]] = parse(v); //Assign to new object,
}
}
In the interpolate function, return (source+(target-source)*pos).toFixed(3);
toFixed is to solve the decimal problem, such as 0.000000001; will become 1e-9; which is not the result we want, pass toFixed can be solved, toFixed (n), where n represents the number of decimal places to retain
el.currentStyle? el.currentStyle: getComputedStyle(el, null);
This is actually compatible with multiple browsers, one line of code to get the element Specific reference: JS gets the final style [getStyle]
Flower’s 4 parameters el target object, style is the final style, opts, is the parameter options including (dur time, easing function, callbak run after the end), The fourth after is the last callbak executed;
opts.easing can use various easing algorithms to change the motion state of elements;
such as
function bounce(pos) {
if (pos return (7.5625*pos*pos);
} else if (pos return (7.5625*(pos-=(1.5/2.75))*pos + .75);
} else if (pos < ; (2.5/2.75)) {
return (7.5625*(pos-=(2.25/2.75))*pos + .9375);
} else {
return (7.5625*(pos-=( 2.625/2.75))*pos + .984375);
}
}
(function($,name){
window.flower = flower;
})(window,'flower ');
This actually makes the internal function free and exposes an interface only through this call. Otherwise, external functions cannot access the flower in anonymous correspondence;
Look at the calling example : )
<script> <BR>(function($,name){ <BR>var parseEl = document.createElement('div') <BR>, <BR>props = ('backgroundColor borderBottomColor borderBottomWidth borderLeftColor borderLeftWidth '+ <BR>'borderRightColor borderRightWidth borderSpacing borderTopColor borderTopWidth bottom color fontSize '+ <BR>'fontWeight height left letterSpacing lineHeight marginBottom marginLeft marginRight marginTop maxHeight '+ <BR>'maxWidth minHeight minWidth opacity outlineColor outlineOffset outlineWidth paddingBottom paddingLeft '+ <BR>'paddingRight paddingTop right textIndent top width wordSpacing zIndex').split(' ') <BR>, <BR>normalize =function (style){ <BR>var css, <BR>rules = {}, <BR>i = props.length, <BR>v; <BR>parseEl.innerHTML = '<div style="'+style+'"></script>
css = parseEl.childNodes[0].style;
while(i--) if(v = css[props[i]]) rules[props[i]] = parse(v);
return rules;
},
color = function(source,target,pos){
var i = 2, j, c, tmp, v = [], r = [];
while(j=3,c=arguments[i-1],i--)
if(s(c,0)=='r') { c = c.match(/d+/g); while(j--) v.push(~~c[j]); } else {
if(c.length==4) c='#'+s(c,1)+s(c,1)+s(c,2)+s(c,2)+s(c,3)+s(c,3);
while(j--) v.push(parseInt(s(c,1+j*2,2), 16)); }
while(j--) { tmp = ~~(v[j+3]+(v[j]-v[j+3])*pos); r.push(tmp255?255:tmp); }
return 'rgb('+r.join(',')+')';
},
parse = function(prop){
var p = parseFloat(prop), q = prop.replace(/^[-d.]+/,'');
return isNaN(p) ? { v: q, f: color, u: ''} : { v: p, f: interpolate, u: q };
},
s = function(str, p, c){
return str.substr(p,c||1);
},
interpolate =function(source,target,pos){
return (source+(target-source)*pos).toFixed(3);
},
flower = function(el, style,opts,after){
var el = document.getElementById(el),
opts = opts || {},
target = normalize(style),
comp = el.currentStyle ? el.currentStyle : getComputedStyle(el, null),
prop,
current = {},
start = +new Date,
dur = opts.duration||200,
finish = start+dur,
interval,
easing = opts.easing || function(pos){ return (-Math.cos(pos*Math.PI)/2) + 0.5; };
for(prop in target) current[prop] = parse(comp[prop]);
interval = setInterval(function(){
var time = +new Date,
pos = time>finish ? 1 : (time-start)/dur;
for(prop in target){
el.style[prop] = target[prop].f(current[prop].v,target[prop].v,easing(pos)) + target[prop].u;
}
if(time>finish) {
clearInterval(interval); opts.after && opts.after(); after && setTimeout(after,1);
}
},10);
};
$[name] = flower;
})(window,"flower");
(function(){
var bounce = function(pos) {
if (pos return (7.5625*pos*pos);
} else if (pos return (7.5625*(pos-=(1.5/2.75))*pos + .75);
} else if (pos return (7.5625*(pos-=(2.25/2.75))*pos + .9375);
} else {
return (7.5625*(pos-=(2.625/2.75))*pos + .984375);
}
}
flower('test2', 'left:300px;padding:10px;border:50px solid #ff0000', {
duration: 1500,
after: function(){
flower('test1', 'background:#0f0;left:100px;padding-bottom:100px;opacity:1', {
duration: 1234, easing: bounce
});
}
});
})();
参考 : http://scripty2.com/doc/scripty2%20fx/s2/fx/transitions.html

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
