简体   繁体   English

在浏览器中居中一页水平滚动网站(不以div为中心)

[英]Center a one page horizontally scrolling site in browser (not centering a div)

When the browser opens up my page, the browser should already be in the center of the whole page. 当浏览器打开我的页面时,浏览器应该已经位于整个页面的中心。

Meaning: I have a horizontal one-page, which instead of starting from the left and going to the right (eg this ), will start in the middle, and be able to scroll to the content on both the right AND the left. 含义:我有一个水平的单页,而不是从左边开始向右(例如这个 ),将从中间开始,并能够滚动到右侧和左侧的内容。

Here is a visual: 这是一个视觉:

在此输入图像描述

If possible, I'd like to avoid dom-ready scrolling via JavaScript or jQuery (I'm using it for navigational aid and stuff anyways), and use only pure html5/css or at least focusing the screen pre-dom-ready through JavaScript/jQuery. 如果可能的话,我想避免通过JavaScript或jQuery进行dom-ready滚动(我正在使用它进行导航辅助和东西),并且只使用纯html5 / css或者至少将屏幕重点放在预先准备好的屏幕上的JavaScript / jQuery的。

Two ideas on my part: 我的两个想法:

a) moving the container to the left eg using margin-left:-x , but that usually cuts it off. a)将容器向左移动,例如使用margin-left:-x ,但通常会将其切断。

b) moving the screen to the right from the start. b)从一开始就将屏幕向右移动。

Alas, I have been too unsavvy to achieve this on my own. 唉,我自己太不自然了。

ps here my jsfiddle for consistency: http://jsfiddle.net/alexdot/2Dse3/ ps这里我的jsfiddle一致性: http//jsfiddle.net/alexdot/2Dse3/

Solution
If you want to hide data, add visibility:hidden; 如果要隐藏数据,请添加visibility:hidden; to the elements with a content which should be kept hidden at the start. 具有应该在开始时隐藏的内容的元素。 Execute my page scrolling code, and finally change visibility:hidden; 执行我的页面滚动代码,最后更改visibility:hidden; to visibility:visible . visibility:visible

What's going to happen? 会发生什么事?

  1. CSS hides the contents of these elements: visibility:hidden CSS隐藏了这些元素的内容: visibility:hidden
  2. The Page loads 页面加载
  3. JavaScript causes the page to scroll to the center: window.scrollTo(..., ...) JavaScript导致页面滚动到中心: window.scrollTo(..., ...)
  4. JavaScript shows the secret content (outside the view of the browser): visibility=visible JavaScript显示秘密内容(在浏览器视图之外): visibility=visible
  5. The user scrolls to the left/right, and is able to read the secret 用户向左/向右滚动,并且能够读取秘密

Fiddle: http://jsfiddle.net/2Dse3/5/ 小提琴: http//jsfiddle.net/2Dse3/5/


Scrolling code 滚动代码
Scrolling the page to the center cannot be achieved with pure CSS/HTML. 使用纯CSS / HTML无法实现将页面滚动到中心。 This can only be done by using JavaScript. 这只能通过使用JavaScript来完成。 For this simple purpose, I'd rather use native JavaScript than including a JQuery plugin (unnecessary server load). 出于这个简单的目的,我宁愿使用本机JavaScript而不是包含JQuery插件(不必要的服务器负载)。

JavaScript code: 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.
 */

You have to adjust the CSS (see Fiddle ): 你必须调整CSS(见小提琴 ):

body {width: 500%}
#viewContainer {width: 100%}

A final note. 最后一点。 What do you expect when the user has disabled JavaScript? 当用户禁用JavaScript时,您期望什么? Are they allowed to see the contents of the page? 他们是否可以看到页面内容? If yes, add this: 如果是,请添加以下内容:

<noscript>
  <style>
     .page{
       visibility: visible;
     }
  </style>
</noscript>

Otherwise, add <noscript>JavaScript is required to read this page</noscript> . 否则,添加<noscript>JavaScript is required to read this page</noscript>

An easy jQuery solution 一个简单的jQuery解决方案

$(function () {
    scrollTo(($(document).width() - $(window).width()) / 2, 0);
});

What's wrong with DOM-ready? DOM-ready有什么问题?

$(function() {
    $('body').scrollLeft($('#viewContainer').width() * 2/5);
});

I'm pretty sure it can't be done with CSS alone. 我很确定单凭CSS无法完成。

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

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