解决方案
如果你想隐藏的数据,新增visibility:hidden;
与应保持隐藏在开始内容的元素。执行我的页面滚动代码,最后将visibility:hidden;
更改为visibility:visible
。
会发生什么?
- CSS隐藏了这些元素的内容:
visibility:hidden
- 页面加载
- 的JavaScript导致页面滚动到中心:
window.scrollTo(..., ...)
- 的JavaScript显示机密内容(浏览器的视野之外):
visibility=visible
- 用户滚动到左/右,并且能够读取秘密
小提琴:http://jsfiddle.net/2Dse3/5/
滚动码
滚动页面的中心不能用纯CSS/HTML来实现。这只能通过使用JavaScript来完成。为了这个简单的目的,我宁愿使用本地JavaScript而不是包含JQuery插件(不必要的服务器负载)。
JavaScript代码:
window.scrollTo(
(document.body.offsetWidth -document.documentElement.offsetWidth)/2,
(document.body.offsetHeight-document.documentElement.offsetHeight)/2
);
/*
* window.scrollTo(x,y) is an efficient cross-broser function,
* which scrolls the document to position X, Y
* document.body.offsetWidth contains the value of the body's width
* document.documentElement contains the value of the document's width
*
* Logic: If you want to center the page, you have to substract
* the body's width from the document's width, then divide it
* by 2.
*/
你必须调整CSS(见Fiddle):
body {width: 500%}
#viewContainer {width: 100%}
最后要注意的。当用户禁用JavaScript时你期望什么?他们是否被允许查看网页的内容?如果是,请加上:
<noscript>
<style>
.page{
visibility: visible;
}
</style>
</noscript>
否则,请加<noscript>JavaScript is required to read this page</noscript>
。
只要是明确的......你要水平滚动的容器,但你不想使用JavaScript来控制滚动,只是一个HORIZ滚动条? – MrWhite
不要认为有一个纯粹的html5/css解决方案。 - $(“#myDiv”)。Focus(); – Jawad
哦不。我以后使用javascript/jquery来影响滚动。我不想要的是该网站加载,是在左侧,并将加载滚动到页面中间的实际起始区域后。我想避免让用户看到为以后深入查看网站保留的内容。 – alex