要完成此效果需要兩個步驟
第一步:把如下代碼加入到區(qū)域中
<script language=JavaScript></script>
<script>
var defaultbg="ffffff";
// Dynamic Layer Object
// sophisticated layer/element targeting and animation object which provides the core
functionality needed in most DHTML applications
// 19990326
// Copyright (C) 1999 Dan Steinman
// Distributed under the terms of the GNU Library General Public License
// Available at http://www.dansteinman.com/dynduo/
// BrowserCheck Object
// provides browser checking variables
var px;
var py;
function BrowserCheck() {
var b = navigator.appName
if (b=="Netscape") this.b = "ns"
else if (b=="Microsoft Internet Explorer") this.b = "ie"
else this.b = b
this.version = navigator.appVersion
this.v = parseInt(this.version)
this.ns = (this.b=="ns" && this.v>=4)
this.ns4 = (this.b=="ns" && this.v==4)
this.ns5 = (this.b=="ns" && this.v==5)
this.ie = (this.b=="ie" && this.v>=4)
this.ie4 = (this.version.indexOf('MSIE 4')>0)
this.ie5 = (this.version.indexOf('MSIE 5')>0)
this.min = (this.ns||this.ie)
}
is = new BrowserCheck()
// DynLayer Constructor
function DynLayer(id,nestref,frame) {
if (!DynLayer.set && !frame) DynLayerInit()
if (is.ns) {
if (is.ns) {
if (!frame) {
if (!nestref) var nestref = DynLayer.nestRefArray[id]
if (DynLayer.useTest && !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.elm = this.event = this.css
this.doc = this.css.document
}
if (is.ns5) {
this.elm = document.getElementById(id)
this.css = this.elm.style
this.doc = document
}
this.x = this.css.left
this.y = this.css.top
this.w = this.css.clip.width
this.h = this.css.clip.height
}
else if (is.ie) {
this.elm = this.event = (frame)? parent.frames[frame].document.all[id] :
document.all[id]
this.css = (frame)? parent.frames[frame].document.all[id].style :
document.all[id].style
this.doc = document
this.x = this.elm.offsetLeft
this.y = this.elm.offsetTop
this.w = (is.ie4)? this.css.pixelWidth : this.elm.offsetWidth
this.h = (is.ie4)? this.css.pixelHeight : this.elm.offsetHeight
}
this.id = 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
}
// DynLayer Core Methods (moveTo, moveBy, hide, show)
function DynLayerMoveTo(x,y) {
if (x!=null) {
this.x = x
if (is.ns) this.css.left = this.x
else this.css.pixelLeft = this.x
}
if (y!=null) {
this.y = y
if (is.ns) this.css.top = this.y
else this.css.pixelTop = this.y
}
}
function DynLayerMoveBy(x,y) {
this.moveTo(this.x+x,this.y+y)
}
function DynLayerShow() {
this.css.visibility = (is.ns)? "show" : "visible"
}
function DynLayerHide() {
this.css.visibility = (is.ns)? "hide" : "hidden"
}
// DynLayer Slide Methods (optional)
// straight line animation methods
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)
this.onSlide()
this.onSlideEnd()
eval(fn)
}
}
// DynLayer Clip Methods (optional)
// provides layer clipping functionality
function DynLayerClipInit(clipTop,clipRight,clipBottom,clipLeft) {
this.clipTo = DynLayerClipTo
this.clipBy = DynLayerClipBy
this.clipValues = DynLayerClipValues
if (is.ie) {
if (arguments.length==4) this.clipTo(clipTop,clipRight,clipBottom,clipLeft)
else if (is.ie4) this.clipTo(0,this.css.pixelWidth,this.css.pixelHeight,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 (is.ns) {
this.css.clip.top = t
this.css.clip.right = r
this.css.clip.bottom = b
this.css.clip.left = l
}
else if (is.ie) 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 (is.ie) var clipv = this.css.clip.split("rect(")[1].split(")")[0].split("px")
if (which=="t") return (is.ns)? this.css.clip.top : Number(clipv[0])
if (which=="r") return (is.ns)? this.css.clip.right : Number(clipv[1])
if (which=="b") return (is.ns)? this.css.clip.bottom : Number(clipv[2])
if (which=="l") return (is.ns)? this.css.clip.left : Number(clipv[3])
}
// DynLayer Write Method (optional)
// rewrites the contents of the layer
function DynLayerWrite(html) {
if (is.ns) {
this.doc.open()
this.doc.write(html)
this.doc.close()
}
else if (is.ie) {
this.event.innerHTML = html
}
}
// DynLayerTest Function (optional - disable with DynLayer.useTest=false)
// makes sure the layer target is correct before assigning the DynLayer, outputs useful info if
incorrect
function DynLayerTest(id,nestref) {
var ref = new Array()
if (nestref) ref = nestref.split('.document.')
ref[ref.length] = id
var refstr = (is.ns)? '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 += (is.ns)? '.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
// DynLayerInit
// automatically retrieves nested heirarchy of the layer and defines DynLayers named with "Div"
function DynLayerInit(nestref) {
if (!DynLayer.set) DynLayer.set = true
if (is.ns) {
if (nestref) ref = eval('document.'+nestref+'.document')
else {nestref = ''; ref = document;}
for (var i=0; i
0) {
eval(divname.substr(0,index)+' = new
DynLayer("'+divname+'","'+nestref+'")')
}
if (ref.layers[i].document.layers.length > 0) {
DynLayer.refArray[DynLayer.refArray.length] = (nestref=='')?
ref.layers[i].name : nestref+'.document.'+ref.layers[i].name
}
}
if (DynLayer.refArray.i < DynLayer.refArray.length) {
DynLayerInit(DynLayer.refArray[DynLayer.refArray.i++])
}
}
else if (is.ie) {
for (var i=0; i 0) {
eval(divname.substr(0,index)+' = new DynLayer("'+divname+'")')
}
}
}
return true
}
DynLayer.nestRefArray = new Array()
DynLayer.refArray = new Array()
DynLayer.refArray.i = 0
DynLayer.set = false
DynLayer.useTest = true
// CSS function (optional)
// returns CSS syntax for generated layers
function css(id,left,top,width,height,color,vis,z,other) {
if (id=="START") return ''
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;'
if (arguments.length<9 || other.indexOf('clip')==-1) str += ' clip:rect(0px
'+width+'px '+height+'px 0px);'
}
if (arguments.length>=6 && color!=null) str += (is.ns)? '
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+';'
if (arguments.length==9 && other!=null) str += ' '+other
str += '}\n'
return str
}
function writeCSS(str,showAlert) {
str = css('START')+str+css('END')
document.write(str)
if (showAlert) alert(str)
}
//css(id,left,top,width,height,color,vis,z,other)
if (is.ns){ x=-10; y=-23;}
else if(is.ie5){ x=-1; y=-42;}
else {x=0;y=0;}
writeCSS(
css('bgcDiv',x+10,y+160,53,40,defaultbg,1,8) +
css('RDiv', x+65,y+160,256,9,null,1,1) +
css('RpointDiv',x+65,y+169,12,8,null,1,2) +
css('GDiv', x+65,y+180,256,9,null,1,3) +
css('GpointDiv',x+65,y+189,12,8,null,1,4) +
css('BDiv', x+65,y+200,256,9,null,1,5) +
css('BpointDiv',x+65,y+209,12,8,null,1,6)
)
var gcurr;
var Display;
function xch(g) {
document.setform[g + "x"].value = document.setform[g + "x"].value.toUpperCase();
if (g == gcurr) {
use(g);
}
return true;
}
function initialize(bgc) {
document.setform.bgx.value = bgc;
return true;
}
function myclip() {
var c = h2d(document.setform[gcurr + "x"].value);
var cr = (c >> 16) & 255, cg = (c >> 8) & 255, cb = (c) & 255;
R.clipTo(null,cr,null,null);
G.clipTo(null,cg,null,null);
B.clipTo(null,cb,null,null);
Rpoint.moveTo(R.x-5+cr,null);
Gpoint.moveTo(G.x-5+cg,null);
Bpoint.moveTo(B.x-5+cb,null);
}
function use(g) {
gcurr = g;
Display = window[gcurr + "c"];
var c = h2d(document.setform[gcurr + "x"].value);
var cr = (c >> 16) & 255, cg = (c >> 8) & 255, cb = (c) & 255;
change(cr, cg, cb);
myclip();
return true;
}
function change(r, g, b) {
document.setform.rd.value = r;
document.setform.gd.value = g;
document.setform.bd.value = b;
document.setform[gcurr + "x"].value = convertToHex(r, g, b);
Display.css.backgroundColor = convertToHex(r, g, b);
Display.css.bgColor = convertToHex(r, g, b);
return true;
}
function h2d(s)
{
var r = 0;
var index = "0123456789ABCDEF";
s = s.toUpperCase();
for (var i = 0; i < s.length; i++) {
r <<= 4;
r += index.indexOf(s.substr(i, 1));
}
return r;
}
function convertToHex(R, G, B) {
var Hex = "0123456789ABCDEF";
return ""
+ Hex.substr(R >> 4, 1) + Hex.substr(R & 15, 1)
+ Hex.substr(G >> 4, 1) + Hex.substr(G & 15, 1)
+ Hex.substr(B >> 4, 1) + Hex.substr(B & 15, 1);
}
function pcolor(form)
{
form.bgcolor.value = h2d(form.bgx.value);
return true;
}
function init() {
DynLayerInit()
initialize(defaultbg);
var c = h2d(document.setform.bgx.value);
Rc.value = (c >> 16) & 255;
Gc.value = (c >> 8) & 255;
Bc.value = (c) & 255;
defaultRvalue = Rc.value;
defaultGvalue = Gc.value;
defaultBvalue = Bc.value;
R.clipInit(0,defaultRvalue,10,0)
R.dragActive = false
G.clipInit(0,defaultGvalue,10,0)
G.dragActive = false
B.clipInit(0,defaultBvalue,10,0)
B.dragActive = false
R.clipTo(null,defaultRvalue+1,null,null)
G.clipTo(null,defaultGvalue+1,null,null)
B.clipTo(null,defaultBvalue+1,null,null)
Rpoint.moveTo(R.x-5+defaultRvalue,null)
Gpoint.moveTo(G.x-5+defaultGvalue,null)
Bpoint.moveTo(B.x-5+defaultBvalue,null)
use("bg");
Display.css.backgroundColor = convertToHex(defaultRvalue, defaultGvalue, defaultBvalue)
Display.css.bgColor = convertToHex(defaultRvalue, defaultGvalue, defaultBvalue)
document.onmousedown = mouseDown
document.onmousemove = mouseMove
document.onmouseup = mouseUp
if (is.ns) document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP)
}
function mouseDown(e) {
if ((is.ns && e.which!=1) || (is.ie && event.button!=1)) return true
var x = (is.ns)? e.pageX : event.x+document.body.scrollLeft
var y = (is.ns)? e.pageY : event.y+document.body.scrollTop
if (x>=R.x && x<=R.x+R.w && y>=R.y && y<=R.y+R.h+10) {
R.clipTo(null,x-R.x,null,null)
if ( x >= R.x + R.w )
{
RpointX = R.w + R.x - 6
Rc.value = RpointX + 6 - R.x -1
}
else if ( x <= R.x )
{
RpointX = R.x - 6
Rc.value = RpointX + 6 - R.x
}
else
{
RpointX = x - 6
Rc.value = RpointX + 6 - R.x
}
Rpoint.moveTo(RpointX,null)
Display.css.backgroundColor = convertToHex(Rc.value, Gc.value, Bc.value)
Display.css.bgColor = convertToHex(Rc.value, Gc.value, Bc.value)
R.dragOffsetX = x-R.x
R.dragOffsetY = y-R.y
R.dragActive = true
return false
}
else if (x>=G.x && x<=G.x+G.w && y>=G.y && y<=G.y+G.h+10) {
G.clipTo(null,x-G.x,null,null)
if ( x >= G.x + G.w )
{
GpointX = G.w + G.x - 6
Gc.value = GpointX + 6 - G.x -1
}
else if ( x <= G.x )
{
GpointX = G.x - 6
Gc.value = GpointX + 6 - G.x
}
else
{
GpointX = x - 6
Gc.value = GpointX + 6 - G.x
}
Gpoint.moveTo(GpointX,null)
Display.css.backgroundColor = convertToHex(Rc.value, Gc.value, Bc.value)
Display.css.bgColor = convertToHex(Rc.value, Gc.value, Bc.value)
G.dragOffsetX = x-G.x
G.dragOffsetY = y-G.y
G.dragActive = true
return false
}
else if (x>=B.x && x<=B.x+B.w && y>=B.y && y<=B.y+B.h+10) {
B.clipTo(null,x-B.x,null,null)
if ( x >= B.x + B.w )
{
BpointX = B.w + B.x - 6
Bc.value = BpointX + 6 - B.x -1
}
else if ( x <= B.x )
{
BpointX = B.x - 6
Bc.value = BpointX + 6 - B.x
}
else
{
BpointX = x - 6
Bc.value = BpointX + 6 - B.x
}
Bpoint.moveTo(BpointX,null)
Display.css.backgroundColor = convertToHex(Rc.value, Gc.value, Bc.value)
Display.css.bgColor = convertToHex(Rc.value, Gc.value, Bc.value)
B.dragOffsetX = x-B.x
B.dragOffsetY = y-B.y
B.dragActive = true
return false
}
else return true
}
function mouseMove(e) {
var x = (is.ns)? e.pageX : event.x+document.body.scrollLeft
var y = (is.ns)? e.pageY : event.y+document.body.scrollTop
if (R.dragActive) {
R.clipTo(null,x-R.x,null,null)
if ( x >= R.x + R.w )
{
RpointX = R.w + R.x - 6
Rc.value = RpointX + 6 - R.x -1
}
else if ( x <= R.x )
{
RpointX = R.x - 6
Rc.value = RpointX + 6 - R.x
}
else
{
RpointX = x - 6
Rc.value = RpointX + 6 - R.x
}
Rpoint.moveTo(RpointX,null)
Display.css.backgroundColor = convertToHex(Rc.value, Gc.value, Bc.value)
Display.css.bgColor = convertToHex(Rc.value, Gc.value, Bc.value)
return false
}
else if (G.dragActive) {
G.clipTo(null,x-G.x,null,null)
if ( x >= G.x + G.w )
{
GpointX = G.w + G.x - 6
Gc.value = GpointX + 6 - G.x -1
}
else if ( x <= G.x )
{
GpointX = G.x - 6
Gc.value = GpointX + 6 - G.x
}
else
{
GpointX = x - 6
Gc.value = GpointX + 6 - G.x
}
Gpoint.moveTo(GpointX,null)
Display.css.backgroundColor = convertToHex(Rc.value, Gc.value, Bc.value)
Display.css.bgColor = convertToHex(Rc.value, Gc.value, Bc.value)
return false
}
else if (B.dragActive) {
B.clipTo(null,x-B.x,null,null)
if ( x >= B.x + B.w )
{
BpointX = B.w + B.x - 6
Bc.value = BpointX + 6 - B.x -1
}
else if ( x <= B.x )
{
BpointX = B.x - 6
Bc.value = BpointX + 6 - B.x
}
else
{
BpointX = x - 6
Bc.value = BpointX + 6 - B.x
}
Bpoint.moveTo(BpointX,null)
Display.css.backgroundColor = convertToHex(Rc.value, Gc.value, Bc.value)
Display.css.bgColor = convertToHex(Rc.value, Gc.value, Bc.value)
return false
}
else return true
}
function mouseUp(e) {
var x = (is.ns)? e.pageX : event.x+document.body.scrollLeft
var y = (is.ns)? e.pageY : event.y+document.body.scrollTop
R.dragActive = false
G.dragActive = false
B.dragActive = false
change(Rc.value, Gc.value, Bc.value);
return true
}
</script>
第二步:把“onload="init();"”加在標記里
例如: