Flere Scripts sammen fungerer bare ikke!
Hej, jeg har endelig opdaget de fede ting man kan med javascript. Jeg har brugt toscripts fra www.dynamicdrive.com og de virker godt hver for sig, men det går galt hvis man sætter dem på samme. Koden herunder er ændret lidt, mon nogen kan se hvor fejlen ligger? Jeg har læst at man skal lave et body onload tag hvor man aktiveret "init" i begge scripts, men det er ikke lykkedes mig. Hjælp!Mvh.
CoolKiss
<HTML>
<HEAD><TITLE>Test 1</TITLE>
<style>
<body onload="init();init()">
.shakeimage{
position:absolute;
left:400px;
top:200px;
}
</style>
<script language="JavaScript1.2">
/*
Shake image script (onMouseover)-
(c) Dynamic Drive (www.dynamicdrive.com)
For full source code, usage terms, and 100's more DHTML scripts, visit http://dynamicdrive.com
*/
//configure shake degree (where larger # equals greater shake)
var rector=3
///////DONE EDITTING///////////
var stopit=0
var a=1
function init(which, sleft,stop){
stopit=0
shake=which
shake.style.left=sleft
shake.style.top=stop
}
function rattleimage(){
if ((!document.all&&!document.getElementById)||stopit==1)
return
if (a==1){
shake.style.top=parseInt(shake.style.top)+rector
}
else if (a==2){
shake.style.left=parseInt(shake.style.left)+rector
}
else if (a==3){
shake.style.top=parseInt(shake.style.top)-rector
}
else{
shake.style.left=parseInt(shake.style.left)-rector
}
if (a<4)
a++
else
a=1
setTimeout("rattleimage()",50)
}
function stoprattle(which,sleft,stop){
stopit=1
which.style.left=sleft
which.style.top=stop
}
</script>
<script type="text/javascript">
/****************************************************
* DOM Image rollover:
* by Chris Poole
* http://chrispoole.com
* Script featured on http://www.dynamicdrive.com
* Keep this notice intact to use it :-)
****************************************************/
function init() {
if (!document.getElementById) return
var imgOriginSrc;
var imgTemp = new Array();
var imgarr = document.getElementsByTagName('img');
for (var i = 0; i < imgarr.length; i++) {
if (imgarr[i].getAttribute('hsrc')) {
imgTemp[i] = new Image();
imgTemp[i].src = imgarr[i].getAttribute('hsrc');
imgarr[i].onmouseover = function() {
imgOriginSrc = this.getAttribute('src');
this.setAttribute('src',this.getAttribute('hsrc'))
}
imgarr[i].onmouseout = function() {
this.setAttribute('src',imgOriginSrc)
}
}
}
}
</script>
<body>
<BODY style="BACKGROUND: #ccccff; bgColor=#ccccff>
<img src="cph1.jpg" width="587" height="463" class="shakeimage" onMouseover="init(this,400,200);rattleimage()" onMouseout="stoprattle(this,400,200);top.focus()" onClick="top.focus()">
<img src="before.jpg" hsrc="after.jpg">
</html>