Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript how to set interval time to stop scrolling

This is the demo in jsfiddle, demo

What I want is let the scrolled items('one ', 'two', 'three', '4', '5', '6', '7') automatically scroll up like the demo showed, and stop 2 sec when it's in the middle position. But in my demo, it will shack for a while after stopping in the middle position.

Here is the place in my demo code for setting position.

if ((x == 0) || (x % 35== 0)) {
    setTimeout(function () {
        i.top = x + 'px';
    }, 1000);
} else {
    i.top = x + 'px';
}

Any one can help me? Thanks!

UPDATE: The reason why I set 35 is because I found that the scrolled items are approximately in the middle position when it equals to 0, -35,-70,-105,.... But when I console all x, I found that the value of x is between (31, -251). Do you know how to find the exact position when each items are in the middle of position? Thanks!

like image 285
typeof programmer Avatar asked Oct 21 '22 01:10

typeof programmer


1 Answers

i modified your code a bit,

i set a variable "k" that the interval is assigned to and i clear the interval on stop and start it again after the timeout

looks good for me -> http://jsfiddle.net/ato0mf7u/3/ no funny shakin anymore ;-D

window.onload = addScrollers;


var i = 1;
var arr = ['one ', 'two', 'three', '4', '5', '6', '7'];
var mid;
var k;
function addScrollers() {

    var txt = arr[0];
    while (i < arr.length) {
        txt += '<p>' + arr[i] + '</p>';
        i++;
    }
    startScroll('myscroller', txt);

}

var speed = 10; // scroll speed (bigger = faster)
var dR = false; // reverse direction

var step = 1;
function objWidth(obj) {

    if (obj.offsetWidth) return obj.offsetWidth;
    if (obj.clip) return obj.clip.width;
    return 0;
}
function objHeight(obj) {

    if (obj.offsetHeight) return obj.offsetHeight;
    if (obj.clip) return obj.clip.height;
    return 0;
}
function scrF(i, sH, eH) {
    var x = parseInt(i.top) + (dR ? step : -step);
    if (dR && x > sH) {
        x = -eH;
    } else if (x < 1 - eH) {
        x = sH;
    }
    //when x is the times of 35, the positio is in middle
    if ((x == 0) || (x % 35== 0)) {
        clearInterval(k);
        setTimeout(function () {
            i.top = x + 'px';
            k = setInterval(function () {

             scrF(i, sH, eH);
             }, 1000 / speed);
        }, 1000);

    }
    else {
        i.top = x + 'px';
    }
    return x;
}
function startScroll(sN, txt) {


    var scr = document.getElementById(sN);
    var sW = objWidth(scr);
    var sH = objHeight(scr);
    scr.innerHTML = '<div id="' + sN + 'in" style="position:absolute; left:3px; width:' + sW + ';">' + txt + '<\/div>';
    var sTxt = document.getElementById(sN + 'in');
    var eH = objHeight(sTxt);

    mid = (eH - sH) / 2;

    sTxt.style.top = (dR ? -eH : sH) + 'px';
    sTxt.style.clip = 'rect(0,' + sW + 'px,' + eH + 'px,0)';


    k = setInterval(function () {

        scrF(sTxt.style, sH, eH);
    }, 1000 / speed);

}
like image 68
john Smith Avatar answered Oct 27 '22 18:10

john Smith