gpt4 book ai didi

css - 简单媒体查询规则在页面刷新时发生变化

转载 作者:技术小花猫 更新时间:2023-10-29 11:22:49 26 4
gpt4 key购买 nike

我有一个奇怪的问题。我的 CSS 中的媒体查询规则会像它们应该的那样以特定宽度触发,但是如果我已经将浏览器窗口的大小调整到那个较小的宽度,然后将浏览器窗口扩展到它之外,就会发生一些奇怪的事情。在我刷新页面之前,一切看起来都很正常。布局仅在页面刷新时以意外方式更改。目标当然是让媒体查询正常运行,因为页面刷新不应更改任何规则。

我尝试使用基本框架重现问题:

//CSS media queries added to style tag here to prevent scrolling in other code blocks
var style = document.getElementsByTagName('style')[ 0 ];

style.innerHTML +=
' \
@media( max-width: 550px ) { \
body { \
background-color: #ddd; \
transition: 1s; \
} \
main { \
height: auto; \
text-align: center; \
} \
.circle, .about-container { \
float: none; \
} \
.circle { \
display: inline-block; \
transform: translateY( 0 ); \
} \
.about-container { \
margin: 0 auto; \
text-align: left; \
} \
} \
@media( min-width: 551px ) { \
.circle { \
transform: translateY( 0 ); \
} \
} \
';
/* ///////////////// ELEMENTS IN ORDER THEY APPEAR IN HTML ///////////////// */
* {
box-sizing: border-box;
}
main {
max-width: 600px;
height: 11rem;
margin: 0 auto;
padding-top: 10px;
font-size: 0.8rem;
text-align: left;
}

.circle {
width: 100px;
height: 100px;
background-color: #444;
border-radius: 50%;
float: left;
top: calc( 50% - 10px );
}

.about-container {
width: 75%;
float: right;
}

body button {
display: block;
margin: 0 auto;
padding: 8px;
}
<head>
<style>

/* ///////////// BASIC STYLES TO MAKE THIS SNIPPET LOOK BETTER ///////////// */
@import url('https://necolas.github.io/normalize.css/latest/normalize.css');

.clearfix:after {
content: "";
display: block;
clear: both;
}
.vertically-center {
position: relative;
top: 50%;
transform: translateY( -50% );
}
</style>
</head>
<body>
<main class="clearfix">
<div class="circle vertically-center"></div>
<div class="about-container">
<h3>About</h3>
<p>
This problem is wierd. When <strong>snippet is made full screen</strong>
and browser width is shrunk to less than <strong>550px</strong> <em>
( You will know you're at 550px because the background will darken )</em>
the layout changes - as expected. However when window is expanded beyond
550px again and <strong>Reload Frame</strong> is clicked the layout
unexpectedly updates itself. <br><br>What's going on?
</p>
</div>
</main>
<button onclick="location.reload()">Reload Frame</button>
</body>

虽然很奇怪,但我觉得这有一个简单的解决方案,我希望是这样吗?

编辑:我在 chrome 浏览器上看到了这些结果。将上传一个 GIF 演示我在下面看到的内容。

enter image description here

最佳答案

看起来 Chrome 对同时 float 、定位和翻译的元素有问题。同样,这是 Chrome 中的缺陷,而不是您的代码中的缺陷。
解决此问题的一种方法是不使用定位,仅使用平移来移动圆圈。

//CSS media queries added to style tag here to prevent scrolling in other code blocks

var style = document.getElementsByTagName('style')[ 0 ];

style.innerHTML +=
' \
@media( max-width: 550px ) { \
body { \
background-color: #ddd; \
} \
main { \
height: auto; \
text-align: center; \
} \
.circle, .about-container { \
float: none; \
} \
.circle { \
display: inline-block; \
transform: translateY(-40%); /* changed a bit to compensate */ \
} \
.about-container { \
margin: 0 auto; \
text-align: left; \
} \
} \
';
/* ///////////////// ELEMENTS IN ORDER THEY APPEAR IN HTML ///////////////// */

body {
transition: 0.25s;
}

main {
max-width: 600px;
margin: 0 auto;
height: 11rem;
padding-top: 10px;
font-size: 0.8rem;
text-align: left;
}

.circle {
height: 100px;
width: 100px;
background-color: #444;
border-radius: 50%;
float: left;
}
/* this was moved here */
.vertically-center {
transform: translateY(40%);
}

.about-container {
width: 75%;
float: right;
}

body button {
display: block;
margin: 0 auto;
padding: 8px;
}
<head>
<style>
/* ///////////// BASIC STYLES TO MAKE THIS SNIPPET LOOK BETTER ///////////// */
@import url('https://necolas.github.io/normalize.css/latest/normalize.css');
* {
box-sizing: border-box;
}
.clearfix:after {
content: "";
display: block;
clear: both;
}
/* moved the .vertically-center bit to before the media query */
</style>
</head>
<body>
<main class="clearfix">
<div class="circle vertically-center"></div>
<div class="about-container">
<h3>About</h3>
<p>
This problem is pretty wierd. When you
<strong>make this snippet full screen</strong> and try shrinking the browser
width to less than <strong>550px</strong> <em>( You will know you're at 550px
because the background will darken )</em> the layout changes - Which is
expected. However when you expand the window beyond 550px again and click
<strong>Reload Frame</strong> the current layout unexpectedly updates
itself. <br><br>What's going on?
</p>
</div>
</main>
<button onclick="location.reload()">Reload Frame</button>
</body>

但正如其他人所展示的那样,还有更多的解决方案。随你选吧!

关于css - 简单媒体查询规则在页面刷新时发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42331407/

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