A preliminary study of JavaScript--image movement

Source: Internet
Author: User

When we open the page, most of the pages we see are dynamic, the previous time to learn VB to achieve some dynamic picture of the first is to use Flash, now learn the BS things, it is open vision, just learned an animation effect, to show you, a small function, It achieves the effect of making the page move.

In a lot of times, we in order to achieve a number of effects, such as gradient, moving, if the picture with Flash, no doubt for the browser to add too much load, if you use more than one picture of a display of the picture, this is too rigid, not conducive to our changes, so the best way is to encapsulate a method, Let our pictures according to the effect we want to move, at the same time using an object-oriented approach to achieve, that is, to split a method, we only need to set parameters to achieve the method we want to move, is not very convenient, nonsense, not much to say, the code plus

Set Animation Base.prototype.animate = function (obj) {for (var i = 0; i < this.elements.length; i + +) {var element = This.elem ents[i];//long and high transform animations, just add the width and height values. var attr = obj[' attr '] = = ' x '? ' Left ': obj[' attr ' = = ' y '? ' Top ': obj[' attr ' = = ' W '? ' Width ': obj[' attr '] = = ' h '? ' Height ': ' Left '; var start = obj[' start ']! = undefined? obj[' Start ': GetStyle (element, attr);//optional, default is the starting position of the CSS var t = obj[' t ']! = undefined? obj[' t ']: 30;//optional, default 30 ms execution once var step = obj[' Step ']! = undefined? obj[' Step ': 10;//selectable, each run 10 pixels//provides the alter increment and Target target Volume two schemes var alter = obj[' alter '];var target = obj[' target '];//buffer movement var s peed = obj[' Speed ']! = undefined? obj[' Speed ']: 6;//selectable, default buffering rate is 6var type = obj[' type '] = = 0? ' Constant ': obj[' type ' = = 1?  ' Buffer ': ' buffer ';//optional, 0 means constant speed, 1 for buffering, default buffer if (alter! = undefined && target = = undefined) {target = alter + start;} else if (alter = = undefined && target = = undefined) {throw new Error (' alter increment or target volume must pass one! ');} if (Start > target) sTEP =-step;element.style[attr] = start + ' px '; clearinterval (window.timer); timer = setinterval (function () {//positive value using Math. Ceil rounding, the number of parts into a bit. When negative values are used Math.floor, the decimal part is divided into one place. This will not lead to the end of the movement when the feeling of non-smooth and abrupt. if (type = = ' buffer ') {step = (Target-getstyle (element, attr))/Speed;step = step > 0? Math.ceil (STEP): Math.floor (step);} *///a better solution to the method if (step = 0) {settarget ();} else if (step > 0 && math.abs (getStyle (element, attr) of several pixels or less Target) <= step) {settarget ();} else if (step < 0 && (GetStyle (element, attr)-target) <= Math.Abs (step) ) {settarget ();} else {//put in else never and stop motion notification execution, there will be no 303 cut to 300 at the same time//But there will be a problem that does not cut to 300 at the same time, resulting in abrupt element.style[attr] = GetStyle (element, attr) + step + ' px ';} document.getElementById (' AAA '). InnerHTML + = GetStyle (element, attr) + ' <br/> ';}, T); function settarget () { ELEMENT.STYLE[ATTR] = target + ' px '; clearinterval (timer);}} return this;};
<span style= "White-space:pre" ></span> function call
$ (function () {$ (' #button '). Click (function () {$ (' #box '). Animate ({' attr ': ' x ', ' Start ': +, ' alter ': +, ' target ': 0, ' st EP ': 7});});

HTML code, we just need to set an ID on it, that is, in the body to find the one to add the following code

<div id= ' box ' >box</div><input type= ' button ' id= ' button ' value= ' buttons '/><div id= ' aaa ' ></div >

The first time to learn JavaScript, feel he is really very magical, used to learn HTML, static Web page is possible, but small relative to JavaScript, he is more like a process-oriented, what needs, is a step-by-step to the end, and JavaScript more like object-oriented, Some of the methods are well encapsulated for better use. No wonder it's called JavaScript, and there's a Java word that's a bit of an object-oriented meaning.

A preliminary study of JavaScript--image movement

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.