简体   繁体   English

如何使用jquery或javascript滚动div内容?

[英]How to scroll the div content using jquery or javascript?

I want to scroll the div, containing the text, to the top when I mouseover the up arrow and stop when the mouse leaves the focus. 当我将鼠标悬停在向上箭头上时,我想将包含文本的div滚动到顶部,并在鼠标离开焦点时停止。 Same for the down arrow. 向下箭头相同。 I tried using jquery but it fails. 我尝试使用jquery,但失败。

please visit: http://jsfiddle.net/shantanubhaware/38WMF/12/ 请访问: http : //jsfiddle.net/shantanubhaware/38WMF/12/

Here is Html code 这是HTML代码

<div class="container">
    <div class="news event">
        <div class="up arrow nav"></div>
        <div class="down arrow nav"></div>
        <p class="content items"> <span class="p">text1
            <br/>
            <br/>
            <br/><br/>
            <br/><br/>
            <br/><br/><br/><br/><br/>
            text2
            <br/>
            <br/>
            <br/><br/>
            <br/><br/>
            <br/><br/><br/><br/><br/>
            text3
            <br/>
            <br/>
            <br/><br/>
            <br/><br/>
            <br/><br/><br/><br/><br/>
                text4</span>

        </p>
    </div>
</div>

I use the following jquery 我用下面的jQuery

$('.up').mouseover(function () {
    scrollToTop();
});
$('.down').mouseover(function () {
    scrollToBottom();
});

function scrollToTop() {

    var cur = $('.content').scrollTop();
    while (cur > 0) {
        cur = parseInt(cur) - 50;
        $('.content').animate({
            scrollTop: cur
        }, 800);
    }
}

function scrollToBottom() {

    var cur = $('.content').scrollTop();
    var height = $('.p').height();
    while (cur < height) {
        cur = parseInt(cur) + 50;
        $('.content').animate({
            scrollTop: cur
        }, 800);
    }

}

tell me if i am wrong anywhere or if i want to use any other technique. 告诉我是否在任何地方出错,或者是否要使用其他任何技术。 Thanks for your support. 谢谢你的支持。

you need to stop the ongoing animation before starting a new one, otherwise it will finish the ongoing animation first and only then will start the new one. 您需要先停止正在进行的动画,然后再开始制作新的动画,否则它将先完成正在进行的动画, 然后再开始制作新的动画。

its done by calling .stop() first. 首先调用.stop()完成。

also you forgot to bind on mouse leave events. 您也忘记绑定鼠标离开事件。

heres yours fixed fiddle: 这是你的固定小提琴:

http://jsfiddle.net/TheBanana/38WMF/14/ http://jsfiddle.net/TheBanana/38WMF/14/

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

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