A preliminary study of JavaScript--image movement

Source: Internet
Author: User

When we open the page, we see most of the pages are dynamic, have been learning VB to achieve some of the dynamic picture of the first is to use Flash, now learn the BS things, is an open vision, just learned an animation effect, to show you. A small function that implements the effect of making the page move.

In a very many time. We want to achieve a few effects, such as gradients. Move. Suppose the picture with Flash, no doubt for the browser to add too much load, assuming that with more than one picture of a display of the picture. This is too inflexible to help us change, so the best way to do this is to encapsulate a method that lets us move the image according to the effect we want. At the same time, the object-oriented approach is used to implement. is to split a method. We just need to set the number of parameters to be able 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-height transform animation. Just increase 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 Run once var step = obj[' Step ']! = undefined? obj[' Step ': 10;//optional. 10 pixels per run//provide alter increment and target target Volume two schemes var alter = obj[' alter '];var target = obj[' target '];//buffer movement var speed = 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 (fun Ction () {//positive value is used Math.ceil rounding, and the decimal part is divided into one 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 notifications run, there will be no 303 cut to 300 problem at the same time//But there will be a different time cut to 300 of the problem, 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 to be able to, that is, in the body to find that added such as the following code can be

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

The first time I learned JavaScript, I felt like he was really amazing. I used to learn HTML. Static Web page is able, 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 methods are very good encapsulation, so that better use. No wonder it's called JavaScript. There's a Java word that's a bit of an object-oriented idea.

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.