Domanda

I have a script that autoscrolls my text from top to bottom. But in the end it quick go to the top and you have no chance to read the last line.

I need to make the script stop at the end for about 5 seconds, and then loop the should go to the top after this pause.

Now it looks like this:

var speed = 1
var currentpos = 0,
    alt = 1,
    curpos1 = 0,
    curpos2 = -1

function initialize() {
    startit()
}

function iecompattest() {
    return (document.compatMode != "BackCompat") ? document.documentElement : document.body
}

function scrollwindow() {
    if (document.all) 
        temp = iecompattest().scrollTop
    else 
        temp = window.pageYOffset

    if (alt == 0) 
        alt = 1
    else 
        alt = 0

    if (alt == 0)
        curpos1 = temp
    else 
        curpos2 = temp

    if (curpos1 != curpos2) {
        if (document.all) 
            currentpos = iecompattest().scrollTop + speed
        else 
            currentpos = window.pageYOffset + speed
        window.scroll(0, currentpos)
    } else {
        currentpos = 0
        window.scroll(0, currentpos)
    }
}

function startit() {
    setInterval("scrollwindow()", 30)
}

window.onload = initialize

And html file its just Lorem ipsum.

È stato utile?

Soluzione

Here's what I came up with. Hope it helps!

Demo

Note that I changed window.onload = initialize to $(function () { initialize() }); and included jQuery, but that was only to make it work in http://jsfiddle.net/. Should do just fine without it outside jsfiddle.

var speed = 1
var currentpos = 0,
    alt = 1,
    curpos1 = 0,
    curpos2 = -1
    interval = 0,
    pauseTime = 3000;

function initialize() {
    startit()
}

function iecompattest() {
    return (document.compatMode != "BackCompat") ? document.documentElement : document.body
}

function scrollwindow() {
    var docHeight = getDocHeight();
    var iHeight = window.innerHeight;
    var off = docHeight - iHeight;
    if (currentpos > off) {
        window.clearInterval(interval);
        setTimeout(startit, pauseTime);
    } else {
        if (document.all) temp = iecompattest().scrollTop
        else temp = window.pageYOffset
        if (alt == 0) alt = 1
        else alt = 0
        if (alt == 0) curpos1 = temp
        else curpos2 = temp
        if (curpos1 != curpos2) {
            if (document.all) currentpos = iecompattest().scrollTop + speed
            else currentpos = window.pageYOffset + speed
            window.scroll(0, currentpos)
        } else {
            currentpos = 0
            window.scroll(0, currentpos)
        }
    }
}

function getDocHeight() {
    var D = document;
    return Math.max(
    D.body.scrollHeight, D.documentElement.scrollHeight,
    D.body.offsetHeight, D.documentElement.offsetHeight,
    D.body.clientHeight, D.documentElement.clientHeight);
}

function startit() {
    currentpos = 0;
    interval = setInterval(scrollwindow, 15);
}
$(function () {
    initialize();
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top