gpt4 book ai didi

javascript - 响应式倒计时与背景图片对齐

转载 作者:行者123 更新时间:2023-11-28 02:50:10 24 4
gpt4 key购买 nike

我想寻求帮助,了解如何让我的倒数计时器响应。每次缩放窗口大小时,它仍会在中心的笔记本背景图像内对齐。我还不太熟悉媒体查询。另外,我发现很难将两者对齐,因为纸质设计嵌入在背景图像中。

(在编辑中添加) 我也忘了问和提。在此代码中,它工作正常(几乎)。我只需要提及我在我的 css 上声明了其他 p{}。所以我也想问我如何专门针对 .bg p{} ?因为看起来,我的 css 中的前一个 p{} 也会影响我正在处理的倒计时

这是代码html

<div class="bg">
<p id="countdown"> </p>
</div>

CSS

.bg {
background-image: url('http://thefeministmegaphone.com/wp-content/uploads/2016/12/893146-notebook-wallpaper.jpg');
background-size: 100%;
background-repeat: no-repeat;
position:fixed;
width:100%;
height:100%;
left:0;
top:0;
text-align:center;
}
p#countdown {
transform: rotate(-5deg);
display:block;
color: #333;
font-size: 65px;
font-family:times;
margin: auto;
width: 60%;
padding: 10px;
top:50%;
}
@media screen and (width: 800px) {
.bg p {
font-size: .8em;
}

js

 // Set the date we're counting down to
var countDownDate = new Date("Oct 14, 2017 10:00:00").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

// Get todays date and time
var now = new Date().getTime();

// Find the distance between now an the count down date
var distance = countDownDate - now;

// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// Output the result in an element with id="countdown"
document.getElementById("countdown").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";

// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("countdown").innerHTML = "EXPIRED";
}
}, 1000);

最佳答案

我在下面创建了一个可待因。

CODEPEN

这可能是更好的方法,但我使用百分比来获得正确的对齐方式并使用 em 测量字体。

您可以使用多个断点(1024 像素等)来调整大小以适应。

@media screen and (max-width: 768px) {
.bg p {
margin-top: 20%;
font-size: 2em;
}

关于javascript - 响应式倒计时与背景图片对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46702211/

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