- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我有一个布局,它有最大宽度,当屏幕宽度大于最大宽度时,它水平居中。布局包括一个固定的标题和菜单;当屏幕宽度小于最大值时,菜单的left
位置为0
,当屏幕宽度超过最大值时,菜单的left
位置需要与布局其余部分的左边缘齐平。
它应该是这样的:
*{box-sizing:border-box;margin:0;padding:0;}
header{
align-items:center;
background:#eee;
border-bottom:1px solid #999;
display:flex;
height:100px;
left:0;
justify-content:center;
padding:0 10px;
position:fixed;
right:0;
top:0;
}
h1{
height:60px;
position:relative;
width:calc(100% - 40px);
max-width:740px;
z-index:2;
}
img{
height:100%;
width:auto;
}
nav{
background:#eee;
border-right:1px solid #999;
bottom:0;
left:0;
position:fixed;
top:0;
width:300px;
z-index:1;
}
@media (min-width:801px){
nav{
border-left:1px solid #999;
left:calc((100% - 800px) / 2)
}
}
nav::before{background:#ecc;bottom:0;content:"";left:0;position:absolute;top:0;width:10px;}
nav::after{background:#999;content:"";height:1px;left:0;position:absolute;right:0;top:99px;}
main{background:#ddf;border-left:1px solid #99c;border-right:1px solid #99c;height:100vh;min-height:100%;margin:0 auto;width:100%;max-width:800px;}
<header>
<h1><img src="http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a"></h1>
<nav></nav>
</header>
<main></main>
但是,当引入垂直滚动条时,由于滚动条宽度包含在媒体查询中检查的宽度中,导致出现负的 left
位置当屏幕宽度介于 800px 和 800-x
px 之间时的菜单(其中 x
是滚动条的宽度)。您可以通过将浏览器的大小调整为略小于 800 像素,在下面的代码片段中看到这一点(您可能需要全屏查看)——菜单的右边框更接近 Logo 和菜单的红色边缘菜单被裁剪。
*{box-sizing:border-box;margin:0;padding:0;}
html,body{height:101%;}
header{
align-items:center;
background:#eee;
border-bottom:1px solid #999;
display:flex;
height:100px;
left:0;
justify-content:center;
padding:0 10px;
position:fixed;
right:0;
top:0;
}
h1{
height:60px;
position:relative;
width:calc(100% - 40px);
max-width:740px;
z-index:2;
}
img{
height:100%;
width:auto;
}
nav{
background:#eee;
border-right:1px solid #999;
bottom:0;
left:0;
position:fixed;
top:0;
width:300px;
z-index:1;
}
@media (min-width:801px){
nav{
border-left:1px solid #999;
left:calc((100% - 800px) / 2)
}
}
nav::before{background:#ecc;bottom:0;content:"";left:0;position:absolute;top:0;width:10px;}
nav::after{background:#999;content:"";height:1px;left:0;position:absolute;right:0;top:99px;}
main{background:#ddf;border-left:1px solid #99c;border-right:1px solid #99c;height:100vh;min-height:100%;margin:0 auto;width:100%;max-width:800px;}
<header>
<h1><img src="http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a"></h1>
<nav></nav>
</header>
<main></main>
我了解发生了什么以及为什么会发生,但我的问题是:是否有任何方法可以单独使用 CSS 来防止它发生?我试过使用视口(viewport)单位而不是百分比,但这反过来会产生问题;在某些屏幕宽度下, Logo 向左移动一点,远离菜单的右边框。如果所有浏览器都具有相同的滚动条宽度或允许自定义滚动条样式,则很容易解决这个问题,但不幸的是,情况并非如此。
08/09/16: 我暂时接受了自己的回答,因为它是我能想到的最好的 JavaScript 解决方案,但我仍在寻找 CSS 解决方案。
最佳答案
/* Alexander Gomez's getScrollBarWidth shorten version Josh Bambrick from:
http://stackoverflow.com/questions/986937/how-can-i-get-the-browsers-scrollbar-sizes
*/
function getScrollBarWidth() {
var $outer = $('<div>').css({
visibility: 'hidden',
width: 100,
overflow: 'scroll'
}).appendTo('body'),
widthWithScroll = $('<div>').css({
width: '100%'
}).appendTo($outer).outerWidth();
$outer.remove();
return 100 - widthWithScroll;
};
/* Tested on Chrome 51.0.2704, Firefox 40.0.2, Internet Explorer 11 */
$(document).ready(function() {
var mydelta = 800; /* your 'main' divs width */
var delta_mq = mydelta + 1;
delta = mydelta - getScrollBarWidth();
var h1_logo = delta - 40;
var $appended = '<style>h1{max-width:' + h1_logo + 'px} main{max-width:' + mydelta + 'px} @media (min-width:' + delta_mq + 'px){main{left:calc((100% - ' + delta + 'px) / 2);} nav{border-left:1px solid #999;left:calc((100% - ' + delta + 'px) / 2); } }';
$('body').append($appended);
});
* {
box-sizing: border-box;
margin: 0;
padding: 0
}
/*
'*' selector is depreciated cause it's slow
use normalize.css instead
And i removed your css media and appended them to the
page using jquery after getting scrollbar width
*/
header {
align-items: center;
background: #eee;
border-bottom: 1px solid #999;
display: flex;
height: 100px;
left: 0;
justify-content: center;
padding: 0 10px;
position: fixed;
right: 0;
top: 0;
}
h1 {
height: 60px;
position: relative;
width: calc(100% - 40px);
max-width: 750px;
z-index: 2;
}
img {
height: 100%;
width: auto;
}
nav {
background: #eee;
border-right: 1px solid #999;
bottom: 0;
left: 0;
position: fixed;
top: 0;
width: 300px;
z-index: 1;
}
main { /* position with 100px top margin */
position: absolute;
top: 100px;
}
header { /* chrome fix */
z-index: 1;
}
nav::before{background:#ecc;bottom:0;content:"";left:0;position:absolute;top:0;width:10px;}
nav::after{background:#999;content:"";height:1px;left:0;position:absolute;right:0;top:99px;}
main{background:#ddf;border-left:1px solid #99c;border-right:1px solid #99c;height:100vh;min-height:100%;margin:0 auto;width:100%;max-width:800px;}
<header>
<h1 class=logo>
<img src="http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a">
</h1>
<nav></nav>
</header>
<main></main>
关于css - 定位元素时考虑滚动条宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39162935/
我必须从我的网站中删除()一些iem,然后将它们追加()回来,但是当我追加它们时,它们出现在不同的地方,而我希望它们完全显示在它们以前的同一个地方是。 有什么解决办法吗? 这是一个沙箱,请随意更新(注
一个。图片 (960x7)b. div(宽度:960,填充:10) 我想定位 (a),使其距顶部 50 像素,居中。我想将 (b) 放置在 (a) 的正下方,没有空格。 我的 CSS 如下: @cha
放置某物的正确方法是什么?我有一个在中心显示博客文章的 div。 "" rel="bookmark"> BY LOUIS MOORE ON " pubdate>
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭1
我已经成功地使用了 position:fixed 设置 CSS/CSS3 并且工作得很好! 我几天前看到了这个,想知道他们是如何实现向下滚动时发生的效果的,菜单栏在滚动前处于一个位置,然后转到顶部并自
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 7 年前。 Improv
接口定义 能够对于文字、段落乃至任何元素的精准定位 并做出增删改查,都是在开发一款富文本编辑器时一项最基本也是最重要的功能之一。让我们先来看看Slate中对于如何在文档树中定位元素是怎么定义的
例如,使用 WPF 在选项卡控件的最左上角定位三个 tabitem 和在最右上角定位一个 tabitem 的正确方法是什么? 我尝试通过更改边距将第四个 tabitem 向右移动,但这并没有产生好的结
我正在尝试使用 Javascript 创建一个跟随鼠标在页面上移动的东西。我希望它是米老鼠,我希望他的眼睛跟随鼠标移动他的眼球...这是我到目前为止的代码(从网络上的各个地方收集,因此归功于编写该部分
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 已关闭 9 年前。 Improve
我试图将两个按钮放置在左上角。但它们始终位于顶部中心。 我已经尝试过这个: jp = new JPanel(); jp.setLayout(new GridBagLayout()); GridBagC
我在使用 JQuery 向下滑动功能时遇到问题。我可以让它正常工作,但是我向下滑动的元素的位置会根据视口(viewport)的大小而变化。我想做的是将它与它滑动的元素联系起来。 This JSfidd
我正在尝试创建一个棋盘,并将其放置在屏幕中间,但到目前为止我无法将它直接放在中间。我不想将位置硬编码到屏幕上,因为我要处理不同的屏幕尺寸。 var winsize = cc.director.
我正在尝试从 mysql 中的 2 个字符串点之间提取数据,我的示例脚本是 'otherdata&p1=textneeded&otherdata' 我需要拉出“textneeded”位,“P1=”是起
如何在 JavaFX 中设置按钮的位置?我的代码: bZero = new Button(); bZero.setPrefSize(45, 20); mainPane.getChildren().ad
我有一个 iPhone 应用程序,我可以在其中显示一系列图像。当用户点击图像时,我需要将该图像带到第一个位置,表明它是所选图像。我可以通过子类化实现 uiscrollview 中的点击。但是我无法将
在下图中,它显示了一个image、textbox 和一个css menu image 我的 CSS 菜单非常完美。我终于按照我需要的方式得到了它。我的问题是我需要导航栏中央的文本框,然后我需要我的图像
我必须创建一个看起来像这样的 div id为2的div应该出现在图片的右下角,图片的大小不固定id=2的div应该应用什么css id =1 的 div 没有定义位置,所以使用默认值,图像也是
如何将我的文本和图像对齐在同一行? 每当我使用 padding 或 margins 时,它就会崩溃到我正在使用的圆形图像中。 #alignPhoto { padding-right: 50px;
简单的问题,如何定位具有整个页面引用的元素? 在我的例子中,我在标题中得到了一个 float 图像,然后是 2 组标题。当我使用时: text-align: center; 它使用图像宽度端和页面其余
我是一名优秀的程序员,十分优秀!