gpt4 book ai didi

html - 在页面中间创建内容面板

转载 作者:太空宇宙 更新时间:2023-11-04 15:31:45 25 4
gpt4 key购买 nike

我想创建一个位于页面绝对中间的内容面板。它可以在滚动条出现之前将其宽度和高度扩展到页面的一定百分比。

我在 JSF 页面上使用 Primefaces。我相信有很多解决方案可以做到这一点。我想使用 JSF 的组合。

这是我的解决方案:

<p:layout id="layout">
<p:layoutUnit position="center">
<p:panel header="Test" style="width:50%">
<ui:insert name="content"/>
</p:panel>
</p:layoutUnit>
</p:layout>

然而,这实际上并没有做任何事情。它没有居中,我插入“内容”标签的任何内容都会扩展到整个页面。

任何人都可以帮助我实现目标吗?

编辑

我已经尝试过 Rick Calder 发布的解决方案。下面是我的代码。但是它对我不起作用。我将 css 写入了 style.css。我将 javascript 写入 resizeContentPanel.js。我在 resource.xhtml 的头部加载了 JQUery 和 resizeContentPanel.js。我相信这些都是正确的步骤。有什么我可能会遗漏的吗?

至于我到底错在哪里,我没有看到内容面板。当我调试 javascript 时,它的高度边距为 -71,宽度边距为 -160。我假设这就是我看不到它的原因。

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>
<ui:insert name="windowTitle"/>
</title>

<h:outputStylesheet library="css" name="style.css"/>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<h:outputScript library="scripts" name="resizeContentPanel.js" />
</h:head>
<h:body>
<div class="centeredPanel">
<p:layout id="layout" >
<p:layoutUnit position="center">
<ui:insert name="content"/>
</p:layoutUnit>
</p:layout>
</div>

<h:form>
<div id="stack">
<p:stack id="stackMenu" icon="/resources/images/stack/stack.png" model="#{pageBuilder.stackMenuModel}" />
</div>
</h:form>

<div id="dock">
<p:dock id="dockMenu" model="#{pageBuilder.dockMenuModel}"/>
</div>

</h:body>

样式.css

root 
{
display: block;
}

body
{
background-image: url('../../resources/images/background/background.jpg');
}

.centeredPanel{
width:25%;
height:25%;
min-width:100px;
min-height:100px;
position:absolute;
left:50%;
top:50%;
}​

resizeContentPanel.js

$(document).ready(function(){
var heightMargin = -($('.centeredPanel').height()/2);
var widthMargin= -($('.centeredPanel').width()/2);
$('.centeredPanel').css('margin-left',widthMargin);
$('.centeredPanel').css('margin-top',heightMargin);
});

$(window).resize(function(){
var heightMargin = -($('.centeredPanel').height()/2);
var widthMargin= -($('.centeredPanel').width()/2);
$('.centeredPanel').css('margin-left',widthMargin);
$('.centeredPanel').css('margin-top',heightMargin);
});

最佳答案

为了在页面中完美地居中某些内容,您确实希望它具有固定大小,以便您可以进行计算。

.centeredPanel{
width:500px;
height:500px;
position:absolute;
left:50%; //sets left edge at the midway point of it's container
top:50%; //sets top edge at the midway point of it's container
margin-left:-250px; //moves the div back half it's width to centre it.
margin-top:-250px; //moves the div up half it's height to centre it.
}

演示:http://jsfiddle.net/calder12/qNNvb/

要用百分比来做到这一点需要 jQuery。

.centeredPanel{
width:25%;
height:25%;
min-width:100px;
min-height:100px;
position:absolute;
left:50%;
top:50%;
background-color:red;
}​


$(document).ready(function(){
var heightMargin = -($('.centeredPanel').height()/2);
var widthMargin= -($('.centeredPanel').width()/2);
$('.centeredPanel').css('margin-left',widthMargin);
$('.centeredPanel').css('margin-top',heightMargin);
});

$(window).resize(function(){
var heightMargin = -($('.centeredPanel').height()/2);
var widthMargin= -($('.centeredPanel').width()/2);
$('.centeredPanel').css('margin-left',widthMargin);
$('.centeredPanel').css('margin-top',heightMargin);
});

演示:http://jsfiddle.net/calder12/qNNvb/3/

关于html - 在页面中间创建内容面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13351164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com