我正在尝试制作一个水平滑块,其中我有一个“p”标签,我想为其分配当前时间和日期,但如果我在 jquery 准备好的时候这样做,也就是我们的页面或文档已经加载时,只会显示那一刻的日期,但不会继续更新,希望你理解我,我留下我拥有的代码。
MYHTML
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="css/protexa.css">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Grupo Protexa</title>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="wrapper">
<div class="slides" id="slide1"></div>
<div class="slides" id="slide2"></div>
<div class="slides" id="slide3"></div>
<div class="hora"><p class="h1">Aqui vamos a poner la hora</p></div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="js/protexa.js"></script>
</body>
</html>
我的 CSS
body {
overflow:hidden;
}
.wrapper {
min-height: 100%;
height: auto !important;
width: 100%;
margin: auto;
background: #000000
position: absolute;
overflow: hidden;
}
.hora{
position:relative;
top:10px;
}
.slides {
position: absolute;
min-height: 100%;
height: auto !important;
width: 100%;
}
#slide1{
background: transparent url("http://www.protexa.com.mx/wp-content/uploads/2018/03/Edificio_Mty.jpg") no-repeat center;
background-size: cover;
animation: left1 15s infinite;
}
#slide2 {
background: transparent url("http://www.protexa.com.mx/wp-content/uploads/2015/05/Sol.jpg") no-repeat center;
background-size: cover;
animation: left2 15s infinite;
left: 100%;
}
#slide3 {
background: transparent url("http://www.protexa.com.mx/wp-content/uploads/2018/03/protexa.jpg") no-repeat center;
background-size: cover;
animation: left3 15s infinite;
left: 200%;
}
@keyframes left1 {
0% {left: 0%; right:0%;}
26.8% {left: 0%; right:0%;}
33.5% {left: -100%; right: 100%;}
93.8% {left: -200%; right: 200%;}
100% {left: 0%; right: 0%;}
}
@keyframes left2 {
0% {left: 100%; right:-100%;}
26.8% {left: 100%; right:-100%;}
33.5% {left: 0%; right: 0%;}
60.3% {left: 0%; right: 0%;}
67% {left: -100%; right: 100%;}
93.8% {left: -100%; right: 100%;}
100% {left: 100%; right: -100%;}
}
@keyframes left3 {
0% {left: 200%; right:-200%;}
26.8% {left: 200%; right:-200%;}
33.5% {left: 100%; right: -100%;}
60.3% {left: 100%; right: -100%;}
67% {left: 0%; right: 0%;}
93.8% {left: 0%; right: 0%;}
100% {left: 200%; right: -200%;}
}
这就是我计划将 javascript 代码设置为 DOM 元素的当前时间的地方。
JS文件
// Shorthand for $( document ).ready()
$(function() {
console.log( "ready!" );
var fecha = new Date();
//alert("Día: "+fecha.getDate()+"\nMes: "+(fecha.getMonth()+1)+"\nAño: "+fecha.getFullYear());
//alert("Hora: "+fecha.getHours()+"\nMinuto: "+fecha.getMinutes()+"\nSegundo: "+fecha.getSeconds()+"\nMilisegundo: "+fecha.getMilliseconds());
});
想象一下,这个元素是您想要显示数据的地方。
您唯一需要做的就是在 JavaScript 中使用 setInterval。