简体   繁体   English

Javascript自动上下滚动,可随时取消

[英]Javascript auto-scroll up and down, cancel at any time

I have a website with a button. 我有一个带有按钮的网站。 When i press the button, i want the page (body) to auto-scroll up and down forever. 当我按下按钮时,我希望页面(正文)永远自动向上和向下滚动。

UNTIL i try to scroll the page myself, then the auto-scroll must stop IMMEDIATELY until restarted with the button. 直到我尝试自己滚动页面,然后自动滚动必须立即停止,直到使用按钮重新启动为止。

I've found alot of posts on how to do auto-scroll - but not in this context, being able to stop it. 我已经找到了很多有关如何进行自动滚动的帖子-但是在这种情况下却无法停止自动滚动。

Any ideas? 有任何想法吗?

hi you can inspire from this example link 您好,您可以从此示例链接中获得启发

<div id="listofstuff">
    <div class="anitem">
       <span class="itemname">Item1</span>
       <span class="itemdescruption">AboutItem1</span>
    </div>
    <div class="anitem">
       <span class="itemname">Item2</span>
       <span class="itemdescruption">AboutItem2</span>
    </div>
    <div class="anitem">
       <span class="itemname">Item3</span>
       <span class="itemdescruption">AboutItem3</span>
    </div>
    <div class="anitem">
       <span class="itemname">Item4</span>
       <span class="itemdescruption">AboutItem5</span>
    </div>
    <div class="anitem">
       <span class="itemname">Item5</span>
       <span class="itemdescruption">AboutItem5</span>
    </div>
</div>

and

$(document).ready(function() {
    var wrapper = $('#listofstuff'),
    element = wrapper.find('.anitem'),
    lastElement = wrapper.find('.anitem:last-child'),
    lastElementTop = lastElement.position().top,
    elementsHeight = element.outerHeight(),
    scrollAmount = lastElementTop - 2 * elementsHeight;

    $('#listofstuff').animate({
        scrollTop: scrollAmount
    }, 1000, function() {
        lastElement.addClass('current-last');
    });
});

and

.anitem {
    height:58px;
    background:#eee;
    border-top: 1px solid #bbb;
    border-bottom: 1px solid #fff;
    padding:20px;
    color:000;
    font-family: Helvetica,Arial, Verdana,sans-serif;
    -webkit-transition: all 1000ms;
}
.anitem.current-last{
    background: #99d;
}
#listofstuff {
    width:300px;
    height:300px;
    overflow:auto;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM