Hallo miteinander,
hallo, speziell KKO!
Ich habe eine Verständnisfrage (eigentlich 2)
Klaus, Du hast mir folgendes Script geschenkt (beim scrollen wird div 2 quasi auf position:fixed gesetzt):
div {margin:0px;position:relative;}
#div1{border:1px solid blue;height:200px;}
#div2{border:1px solid red;height:100px;}
#div3{border:1px solid green;height:2000px;}
div1
div2
div3
var elem = document.getElementById(‚div2‘);
var pos= 200;
window.onscroll = function () {
var scroll = window.pageYOffset;
if( document.body ){ scroll = document.body.scrollTop;}
if ( scroll > pos ){
var neu=scroll - pos ;
elem.style.top= neu + „px“; }
window.setTimeout(„test()“, 50);
}
function test(){
var scroll = window.pageYOffset;
if( document.body ){ scroll = document.body.scrollTop;}
if ( scroll < 2 ){elem.style.top= 0 + „px“;}
}
Warum muss das Tag im Body stehen… normalerweise stehen ja Scripts im head… aber warum funktoiniert das script nicht, wenn ich es in den head Bereich auslagere?
- Frage:
var scroll = window.pageYOffset;
if( document.body ){ scroll = document.body.scrollTop;}
funktioniert bei mir gar nicht
Wenn ich aber schreibe:
var scroll = window.pageYOffset;
<!–[if IE]>
var scroll = document.documentElement.scrollTop ;
<!–<![endif]–>
dann klappt es in allen Browsern tadellos…
Bin ein wenig ratlos und hoffe auf Eure Erleuchtung!
Vielen Dank
Midir