【发布时间】:2020-10-15 03:24:46
【问题描述】:
谁能帮我让这段代码响应?
我在 w3school (https://www.w3schools.com/html/html_responsive.asp) 上添加了<meta name="viewport" content="width=device-width, initial-scale=1.0"> 的代码行,但我使用手机时无法检测到倒计时。
// Set the date we're counting down to
var countDownDate = new Date("Nov 27, 2020 00:00:00").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and 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);
document.getElementById("day").innerHTML = "<br />" + days + "<br />"+ "Ngày";
document.getElementById("hour").innerHTML = "<br />" + hours + "<br />" + "Giờ";
document.getElementById("minute").innerHTML = "<br />" + minutes + "<br />" + "Phút";
document.getElementById("second").innerHTML = "<br />" + seconds + "<br />" + "Giây";
}, 1000);
body, html {
height: 100%;
margin: 0;
}
.bgimg {
background-image: url('https://training.leadthechange.asia/wp-content/uploads/2020/10/MINDFUL-1536x864.png');
height: 100%;
background-position: center;
background-size: cover;
position: relative;
color: black;
font-family: "Courier New", Courier, monospace;
font-size: 25px;
}
.middle {
position: absolute;
top: 90%;
left: 50%;
transform: translate(-50%, -50%);
}
.circle {
width: 120px;
height: 120px;
border-radius: 50%;
font-size: 30px;
background: #333333;
display: inline-block;
text-align: center;
color: white;
font-weight: bold;
}
<div class="bgimg">
<div class="middle">
<div class="circle"><span id="day"></span></div>
<div class="circle"><span id="hour"></span></div>
<div class="circle"><span id="minute"></span></div>
<div class="circle"><span id="second"></span></div>
</div>
</div>
图片还可以,但是天、时、分、秒倒计时没有反应
【问题讨论】:
-
你是指倒计时的宽度和高度吗?
-
你说得对,在手机上使用不响应
-
通过使用媒体查询,我们可以使倒计时响应。试试这个: /* 这里 767px 表示视口宽度 / @media only screen and (max-width: 767px) { .circle { width: 80px;高度:80px;字体大小:18px; } / 下面的css只是为了美化 */ .middle { width: 100%;文本对齐:居中; } }
标签: javascript html css responsive