自动滚动javascript后重定向

时间:2015-05-25 12:27:46

标签: javascript

我找到了这个代码,用于在页面上自动滚动div。

它按原样运行完美,但现在我想在滚动完成后5秒添加逻辑以重定向到URL。

ScrollRate = 50;

function scrollDiv_init() {
    DivElmnt = document.getElementById('MyDivName');
    ReachedMaxScroll = false;
    DivElmnt.scrollTop = 0;
    PreviousScrollTop = 0;
    ScrollInterval = setInterval('scrollDiv()', ScrollRate);
}

function scrollDiv() {
    if (!ReachedMaxScroll) {
        DivElmnt.scrollTop = PreviousScrollTop;
        PreviousScrollTop++;
        ReachedMaxScroll = DivElmnt.scrollTop >= (DivElmnt.scrollHeight - DivElmnt.offsetHeight);
    }
}

2 个答案:

答案 0 :(得分:0)

如果我理解正确,一旦滚动完成,你想去其他页面。这样做:

function scrollDiv() {

if (!ReachedMaxScroll) {
    DivElmnt.scrollTop = PreviousScrollTop;
    PreviousScrollTop++;

    ReachedMaxScroll = DivElmnt.scrollTop >= (DivElmnt.scrollHeight - DivElmnt.offsetHeight);   
}
else{
  clearInterval(ScrollInterval);
  setTimeout(function(){
   window.location.href = "path/to/your/file";
  },5000)//In 5 seconds
}
}

答案 1 :(得分:0)

只需用。替换你的代码。

function scrollDiv() {
    var ScrollRate = 50;


    var DivElmnt = document.getElementById('MyDivName');
    var ReachedMaxScroll = false;
    var PreviousScrollTop  = 0;

    divElmnt.scrollTop = 0;

    setTimeout(
        function () {
            if (!ReachedMaxScroll) {
                DivElmnt.scrollTop = PreviousScrollTop;
                PreviousScrollTop++;

                ReachedMaxScroll = DivElmnt.scrollTop >= (DivElmnt.scrollHeight - DivElmnt.offsetHeight);
            }

            setTimeout(function() {
                window.location.replace("http://stackoverflow.com");
            }, 5);
        },
        ScrollRate
    );
}