javascript - JQuery 滚动事件不起作用

标签 javascript jquery html scroll

我一直在尝试使用单击按钮使 jquery 在文本框中滚动文本,但它在 IE、Chrome 或 Firefox 中不起作用。

我希望我的文本框在单击按钮时手动向下滚动。

<!DOCTYPE html>
<html>
<head>
    <script src="http://localhost:62240/Scripts/jquery-2.1.1.js"></script>
    <script>
        x = 0;
        $(document).ready(function () {
            $("div").scroll(function () {
                $("span").text(x += 1);
            });
            $("button").click(function () {
                $("div").scroll();
            });
        });
    </script>
</head>
<body>

    <p>Try the scrollbar in the div</p>
    <div style="border:1px solid black;width:200px;height:100px;overflow:scroll;">
        In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
        <br><br>
        'Whenever you feel like criticizing anyone,' he told me, just remember that all the people in this world haven't had the advantages that you've had.'
    </div>
    <p>Scrolled <span>0</span> times.</p>
    <button>Trigger scroll event for the window</button>

</body>
</html>

Demo

最佳答案

scroll() 是当 div 滚动时触发的事件。如果您想通过单击按钮进行滚动,请尝试以下操作:

    x = 0;
    $(document).ready(function () {
        $("button").click(function () {
            $('div').animate({ scrollTop: (x+1)*20 }, 200);
            $("span").text(x += 1);
        });
    });

FIDDLE DEMO

关于javascript - JQuery 滚动事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25126302/

相关文章:

javascript - 谷歌折线图将 float 转换为整数

javascript - 嵌入式Flash电影无法播放声音

jquery - 如何在asp.net MVC 4中通过ajax请求下载文件

html - 这段CSS代码的解释

html - Bootstrap 导航问题 : Can't Change the Background Color of Different Columns

javascript - 将绝对位置转换为相对位置

javascript - stylePreprocessorOptions Angular 8

javascript - 在 AngularJS 中处理复杂的 JSON

javascript - 使用 jquery 创建行跨度

html - 当我缩放页面时,文本一直相互重叠