I have put this code into a file named "dynlayer.js" and will be calling that file throughout this tutorial.
// Dynamic Layer Object
// Copyright 1999 Dan Steinman
// Available at the Dynamic Duo (http://www.dansteinman.com/dynduo/)
// Last Updated Feb 16, 1999.
// In order to use this code you must keep this disclaimer
ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false
function DynLayer(id,nestref,frame) {
if (!DynLayerInit.set && !frame) DynLayerInit()
if (ns4) {
if (!frame) {
if (!nestref) var nestref = DynLayerInit.nestRefArray[id]
if (!DynLayerTest(id,nestref)) return
this.css = (nestref)? eval("document."+nestref+".document."+id) : document.layers[id]
}
else this.css = (nestref)? eval("parent."+frame+".document."+nestref+".document."+id) : parent.frames[frame].document.layers[id]
this.x = this.css.left
this.y = this.css.top
}
else if (ie4) {
this.css = (frame)? parent.frames[frame].document.all[id].style : document.all[id].style
this.x = this.css.pixelLeft
this.y = this.css.pixelTop
}
this.id = id
this.w = (ns4)? this.css.clip.width : this.css.pixelWidth
this.h = (ns4)? this.css.clip.height : this.css.pixelHeight
this.doc = (ns4)? this.css.document : document
if (ns4) this.event = this.css
if (ie4) this.event = (frame)? parent.frames[frame].document.all[id] : document.all[id]
this.obj = id + "DynLayer"
eval(this.obj + "=this")
this.moveTo = DynLayerMoveTo
this.moveBy = DynLayerMoveBy
this.show = DynLayerShow
this.hide = DynLayerHide
this.slideInit = DynLayerSlideInit
this.clipInit = DynLayerClipInit
this.write = DynLayerWrite
}
function DynLayerMoveTo(x,y) {
if (x!=null) {
this.x = x
this.css.left = this.x
}
if (y!=null) {
this.y = y
this.css.top = this.y
}
}
function DynLayerMoveBy(x,y) {
this.moveTo(this.x+x,this.y+y)
}
function DynLayerShow() {
this.css.visibility = (ns4)? "show" : "visible"
}
function DynLayerHide() {
this.css.visibility = (ns4)? "hide" : "hidden"
}
function DynLayerSlideInit() {
this.slideTo = DynLayerSlideTo
this.slideBy = DynLayerSlideBy
this.slideStart = DynLayerSlideStart
this.slide = DynLayerSlide
this.onSlide = new Function("")
this.onSlideEnd = new Function("")
}
function DynLayerSlideTo(endx,endy,inc,speed,fn) {
if (endx==null) endx = this.x
if (endy==null) endy = this.y
var distx = endx-this.x
var disty = endy-this.y
this.slideStart(endx,endy,distx,disty,inc,speed,fn)
}
function DynLayerSlideBy(distx,disty,inc,speed,fn) {
var endx = this.x + distx
var endy = this.y + disty
this.slideStart(endx,endy,distx,disty,inc,speed,fn)
}
function DynLayerSlideStart(endx,endy,distx,disty,inc,speed,fn) {
if (this.slideActive) return
if (!inc) inc = 10
if (!speed) speed = 20
var num = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/inc
if (num==0) return
var dx = distx/num
var dy = disty/num
if (!fn) fn = null
this.slideActive = true
this.slide(dx,dy,endx,endy,num,1,speed,fn)
}
function DynLayerSlide(dx,dy,endx,endy,num,i,speed,fn) {
if (!this.slideActive) return
if (i++ < num) {
this.moveBy(dx,dy)
this.onSlide()
if (this.slideActive) setTimeout(this.obj+".slide("+dx+","+dy+","+endx+","+endy+","+num+","+i+","+speed+",\""+fn+"\")",speed)
else this.onSlideEnd()
}
else {
this.slideActive = false
this.moveTo(endx,endy)
eval(fn)
this.onSlideEnd()
}
}
function DynLayerClipInit(clipTop,clipRight,clipBottom,clipLeft) {
this.clipTo = DynLayerClipTo
this.clipBy = DynLayerClipBy
this.clipValues = DynLayerClipValues
if (ie4) {
if (arguments.length==4) this.clipTo(clipTop,clipRight,clipBottom,clipLeft)
else this.clipTo(0,this.w,this.h,0)
}
}
function DynLayerClipTo(t,r,b,l) {
if (t==null) t = this.clipValues('t')
if (r==null) r = this.clipValues('r')
if (b==null) b = this.clipValues('b')
if (l==null) l = this.clipValues('l')
if (ns4) {
this.css.clip.top = t
this.css.clip.right = r
this.css.clip.bottom = b
this.css.clip.left = l
}
else if (ie4) this.css.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)"
}
function DynLayerClipBy(t,r,b,l) {
this.clipTo(this.clipValues('t')+t,this.clipValues('r')+r,this.clipValues('b')+b,this.clipValues('l')+l)
}
function DynLayerClipValues(which) {
if (ie4) var clipv = this.css.clip.split("rect(")[1].split(")")[0].split("px")
if (which=="t") return (ns4)? this.css.clip.top : Number(clipv[0])
if (which=="r") return (ns4)? this.css.clip.right : Number(clipv[1])
if (which=="b") return (ns4)? this.css.clip.bottom : Number(clipv[2])
if (which=="l") return (ns4)? this.css.clip.left : Number(clipv[3])
}
function DynLayerWrite(html) {
if (ns4) {
this.doc.open()
this.doc.write(html)
this.doc.close()
}
else if (ie4) {
this.event.innerHTML = html
}
}
function DynLayerTest(id,nestref) {
var ref = new Array()
if (nestref) ref = nestref.split('.document.')
ref[ref.length] = id
var refstr = (ns4)? 'document.'+ref[0] : 'document.all.'+ref[0]
for (var i=1; i<=ref.length; i++) {
if (eval(refstr)) {
if (ref.length==i) return true
else refstr += (ns4)? '.document.'+ref[i] : '.all.'+ref[i]
}
else {
var str ='DynLayer Error:'
for (j in ref) {
str += '\n'+ref[j]
if (j==i-1) str += ' <-- this layer cannot be found'
}
if (DynLayerTest.count++<5) alert(str)
else alert("Too many DynLayer errors, quitting.")
return false
}
}
return false
}
DynLayerTest.count = 0
function DynLayerInit(nestref) {
if (!DynLayerInit.set) DynLayerInit.set = true
if (ns4) {
if (nestref) ref = eval('document.'+nestref+'.document')
else {nestref = ''; ref = document;}
for (var i=0; i<ref.layers.length; i++) {
var divname = ref.layers[i].name
DynLayerInit.nestRefArray[divname] = nestref
var index = divname.indexOf("Div")
if (index > 0) {
eval(divname.substr(0,index)+' = new DynLayer("'+divname+'","'+nestref+'")')
}
if (ref.layers[i].document.layers.length > 0) {
DynLayerInit.refArray[DynLayerInit.refArray.length] = (nestref=='')? ref.layers[i].name : nestref+'.document.'+ref.layers[i].name
}
}
if (DynLayerInit.refArray.i < DynLayerInit.refArray.length) {
DynLayerInit(DynLayerInit.refArray[DynLayerInit.refArray.i++])
}
}
else if (ie4) {
for (var i=0; i<document.all.tags("DIV").length; i++) {
var divname = document.all.tags("DIV")[i].id
var index = divname.indexOf("Div")
if (index > 0) {
eval(divname.substr(0,index)+' = new DynLayer("'+divname+'")')
}
}
}
return true
}
DynLayerInit.nestRefArray = new Array()
DynLayerInit.refArray = new Array()
DynLayerInit.refArray.i = 0
DynLayerInit.set = false
function css(id,left,top,width,height,color,vis,z) {
if (id=="START") return '<STYLE TYPE="text/css">\n'
else if (id=="END") return '</STYLE>'
var str = (left!=null && top!=null)? '#'+id+' {position:absolute; left:'+left+'px; top:'+top+'px;' : '#'+id+' {position:relative;'
if (arguments.length>=4 && width!=null) str += ' width:'+width+'px;'
if (arguments.length>=5 && height!=null) str += ' height:'+height+'px; clip:rect(0px '+width+'px '+height+'px 0px);'
if (arguments.length>=6 && color!=null) str += (ns4)? ' layer-background-color:'+color+';' : ' background-color:'+color+';'
if (arguments.length>=7 && vis!=null) str += ' visibility:'+vis+';'
if (arguments.length>=8 && z!=null) str += ' z-index:'+z+';'
str += '}\n'
return str
}
function writeCSS(str,showAlert) {
str = css('START')+str+css('END')
document.write(str)
if (showAlert) alert(str)
}
| Home | Next Lesson: Geometric Objects |