如何自动向下滚动html页面
how to automatically scroll down a html page?
我试图在主页向下大约500px后开始每个页面,类似于这个网站:http://unionstationdenver.com/
当你在主页后查看页面时,你会注意到,你会自动向下滚动,而不会发出通知,但你可以向上滚动,再次享受特色滑块。
我玩过scrolledHeight,但我不认为这是我需要的????
基本上,我在所有内容页面上都有一个特色部分,但在向上滚动之前,你不应该看到这个部分。有什么帮助吗?
您可以为此使用.scrollIntoView()
。它将把一个特定的元素带入视口。
示例:
document.getElementById( 'bottom' ).scrollIntoView();
演示:http://jsfiddle.net/ThinkingStiff/DG8yR/
脚本:
function top() {
document.getElementById( 'top' ).scrollIntoView();
};
function bottom() {
document.getElementById( 'bottom' ).scrollIntoView();
window.setTimeout( function () { top(); }, 2000 );
};
bottom();
HTML:
<div id="top">top</div>
<div id="bottom">bottom</div>
CSS:
#top {
border: 1px solid black;
height: 3000px;
}
#bottom {
border: 1px solid red;
}
您可以使用两种不同的技术来实现这一点。
第一个是使用javascript:设置可滚动元素(例如document.body.scrollTop = 1000;
)的scrollTop属性。
第二种是将链接设置为指向页面中的特定id,例如
<a href="mypage.html#sectionOne">section one</a>
然后,如果在您的目标页面中有该ID,页面将自动滚动。
这里是使用纯JavaScript 的示例
function scrollpage() {
function f()
{
window.scrollTo(0,i);
if(status==0) {
i=i+40;
if(i>=Height){ status=1; }
} else {
i=i-40;
if(i<=1){ status=0; } // if you don't want continue scroll then remove this line
}
setTimeout( f, 0.01 );
}f();
}
var Height=document.documentElement.scrollHeight;
var i=1,j=Height,status=0;
scrollpage();
</script>
<style type="text/css">
#top { border: 1px solid black; height: 20000px; }
#bottom { border: 1px solid red; }
</style>
<div id="top">top</div>
<div id="bottom">bottom</div>
使用document.scrollTop
更改文档的位置。将document
的scrollTop
设置为等于站点的特色部分的bottom
<?php
echo '<script type="text/javascript">document.scrollTop = 99999999;</script>';
ob_flush();
ob_end_flush();
flush();
ob_start();
?>
这对我很有用。
相关文章:
- 使用javascript将动态表从一个html页面打印到另一个html页
- 在chrome.tabs.onCreated之后加载HTML页面
- 如何在vs2002中调试html页面
- 使用angular重定向到html页面
- Html页面上的多个Base64图像和平滑加载
- 无法将数据从firebase获取到我的html页面
- 如何在chrome扩展中重定向到html页面
- 如何将值传递到上一个html页面
- AngularJS-需要在index.html页面中访问来自服务的数据
- 如何使用Node/Express显示有关特定错误的自定义html页面
- 如何在 html 页面中调用 jquery
- 加载存储在IndexedDB中的HTML页面
- 同一HTML页面中的两个不同版本的JQuery
- HTML页面如何提取通过表单传递的参数
- 在HTML页面上将URL解析为可读的json格式
- 使用jQuery插入HTML页面的第一个项目缺少结束标记
- 在HTML页面上显示node.js服务器中的数据
- 加载页面时更改html页面的位置
- Ajax调用返回当前html页面,而不是请求的文件
- css是从远程node.js添加的,但不适用于html页面