getElementById() or with a JavaScript library call such as $().
If you use a library call, you inevitably tie the animation object to that library of choice. Alternately,
you can stick with the DOM method, but it??™s a little verbose. In this case, though, you
have to do it only once, so you should use the DOM method and keep things library agnostic.
The call is assigned to a variable to make it easier to refer to the element throughout the
object.
function Animation(element, property, from, to, duration)
{
var el = document.getElementById(element);
if(!el) return false;
}
A quick error check is performed to see whether the element exists, which can prevent
unsightly errors from popping up in the user??™s browser because an element wasn??™t defined.
You can then leave it up to the developer to fail from this error gracefully.
What if you want to performan animation on an element that doesn??™t have an ID? To make
this class even more flexible, let??™s expect an element reference to be passed in instead of just an
ID string. Even better, check to see whether the element property passed in is an ID string. If it??™s
a string and not an object reference, you??™ll retrieve the element by using the DOM method. The
best of both worlds!
function Animation(element, property, from, to, duration)
{
var el = element;
if(typeof el == 'string') el = document.getElementById(element);
if(!el) return false;
}
new Animation(document.
Pages:
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201