简体   繁体   English

如何在页面加载时自动向下滚动div标签?

[英]How to make automatically scroll down a div tag on page load?

Cannot make a div element automatically scroll down. 无法使div元素自动向下滚动。

$('#div').scrollTop(1000)

This doesn't work, but if set on click it works: 这是行不通的,但是如果在click上进行了设置,它将起作用:

$('btn').click(function()
{
    $('#div').scrollTop(1000);
});

I need it to scroll down without clicking any button. 我需要它向下滚动而不单击任何按钮。 What do I need to change? 我需要更改什么?

$(document).ready(function(){
    $('#div').scrollTop(1000);
  });

Try running it after the document is ready: 准备好文档后尝试运行它:

$(function() { // short for $(document).ready(function() {
    $('#div').scrollTop(1000);
});

Chances are you're running your script before the element exists (check $('#div').length -- if it's 0 , you're running it too soon). 您有可能在元素存在之前运行脚本(检查$('#div').length如果为0 ,则运行得太早了)。 You need to wait until the DOM is loaded, which wrapping your code in the above will handle. 您需要等待DOM加载完毕,然后将DOM包装在上面的代码中即可处理。

$(document).ready(function() {
   $('#div').scrollTop(1000);
});

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

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