为平滑颜色过渡的滚动云添加日/夜循环

时间:2015-05-25 11:49:00

标签: javascript jquery html css css3

我有滚动的云,但我需要通过检测系统时钟来进行黎明/黄昏和昼/夜循环。我不确定如何用html或css检测系统时间。

我尝试过渡延迟但是没有效果。

* {
  margin: 0;
  padding: 0;
}
body {
  overflow: hidden;
}
#clouds {
  padding: 100px 0;
  background: #c9dbe9;
  background: -webkit-linear-gradient(top, #c9dbe9 0%, #fff 100%);
  background: -linear-gradient(top, #c9dbe9 0%, #fff 100%);
  background: -moz-linear-gradient(top, #c9dbe9 0%, #fff 100%);
}
.cloud {
  width: 200px;
  height: 60px;
  background-color: #fff;
  border-radius: 200px;
  -moz-border-radius: 200px;
  -webkit-border-radius: 200px;
  position: relative;
}
.cloud:before,
.cloud:after {
  content: '';
  position: absolute;
  background: #fff;
  width: 100px;
  height: 80px;
  position: absolute;
  top: -15px;
  left: 10px;
  border-radius: 100px;
  -moz-border-radius: 100px;
  -webkit-border-radius: 100px;
  -webkit-transform: rotate(30deg);
  transform: rotate(30deg);
  -moz-transform: rotate(30deg);
}
.cloud:after {
  width: 120px;
  height: 120px;
  top: -55px;
  left: auto;
  right: 15px;
}
.x1 {
  left: -250px;
  -webkit-transform: scale(0.4);
  -moz-transform: scale(0.4);
  transform: scale(0.4);
  -webkit-animation: moveclouds 120s linear infinite;
  -moz-animation: moveclouds 120s linear infinite;
  -o-animation: moveclouds 120s linear infinite;
}
.x2 {
  right: 70px;
  top: -100px;
  -webkit-transform: scale(0.5);
  -moz-transform: scale(0.5);
  transform: scale(0.5);
  opacity: 0.7;
  -webkit-animation: moveclouds 140s linear infinite;
  -moz-animation: moveclouds 140s linear infinite;
  -o-animation: moveclouds 140s linear infinite;
}
.x3 {
  left: -550px;
  top: -200px;
  -webkit-transform: scale(0.4);
  -moz-transform: scale(0.4);
  transform: scale(0.4);
  opacity: 0.8;
  -webkit-animation: moveclouds 150s linear infinite;
  -moz-animation: moveclouds 150s linear infinite;
  -o-animation: moveclouds 150s linear infinite;
}
.x4 {
  left: 400px;
  top: -250px;
  -webkit-transform: scale(0.6);
  -moz-transform: scale(0.6);
  transform: scale(0.6);
  opacity: 0.75;
  -webkit-animation: moveclouds 100s linear infinite;
  -moz-animation: moveclouds 100s linear infinite;
  -o-animation: moveclouds 100s linear infinite;
}
.x5 {
  left: -750px;
  top: -250px;
  -webkit-transform: scale(0.47);
  -moz-transform: scale(0.47);
  transform: scale(0.47);
  opacity: 0.8;
  -webkit-animation: moveclouds 110s linear infinite;
  -moz-animation: moveclouds 110s linear infinite;
  -o-animation: moveclouds 110s linear infinite;
}
@-webkit-keyframes moveclouds {
  0% {
    margin-left: 1000px;
  }
  100% {
    margin-left: -1000px;
  }
}
@-moz-keyframes moveclouds {
  0% {
    margin-left: 1000px;
  }
  100% {
    margin-left: -1000px;
  }
}
@-o-keyframes moveclouds {
  0% {
    margin-left: 1000px;
  }
  100% {
    margin-left: -1000px;
  }
}
<div id="clouds">
  <div class="cloud x1"></div>
  <div class="cloud x2"></div>
  <div class="cloud x3"></div>
  <div class="cloud x4"></div>
  <div class="cloud x5"></div>
</div>

我想要实现的目标:

下午5点30分(与本地系统一样):从#c9dbe9过渡到#E0DE3F

下午6:30:从#E0DE3F过渡到#323232

早上5点30分:从#323232过渡到#E0DE3F

早上7点:从#E0DE3F过渡到#c9dbe9

请查看上面的演示,非常感谢类似的演示,单个转换检测系统时间就足够了,我会照顾其余部分。

此外,是否有可能在所有云浮过屏幕之前再次循环云动画?在再次启动之前,动画会等待所有云层漂浮在屏幕之外。

2 个答案:

答案 0 :(得分:4)

试试这个

var dt=new Date();
var h=dt.getHours();
var m=dt.getMinutes();
var time=h+':'+m
if(h == 17){
    if(m > 30){
    $('#clouds').css({
        background:'-webkit-linear-gradient(top,  #E0DE3F 0%, #fff 100%)'
    })
}
}
else if(h > 17){
    $('#clouds').css({
        background:'-webkit-linear-gradient(top,  #E0DE3F 0%, #fff 100%)'
    })
}
else if(h == 18){
    if(m > 30 ){
    $('#clouds').css({
        background:'-webkit-linear-gradient(top,  #323232 0%, #fff 100%)'
    })
}
}
else if(h > 18){
     $('#clouds').css({
        background:'-webkit-linear-gradient(top,  #323232 0%, #fff 100%)'
    })
}
else if(h == 5){
    if(m >= 30){
    $('#clouds').css({
        background:'-webkit-linear-gradient(top,  #E0DE3F 0%, #fff 100%)'
    })
}
}
else if(h > 5 && h < 17){
    $('#clouds').css({
        background:'-webkit-linear-gradient(top,  #E0DE3F 0%, #fff 100%)'
    })
}
else if(h == 6){
    if(m >= 30){
    $('#clouds').css({
        background:'-webkit-linear-gradient(top,   #c9dbe9 0%, #fff 100%)'
    })

    }
}
else if(h >6 && h < 17){
    $('#clouds').css({
        background:'-webkit-linear-gradient(top,   #c9dbe9 0%, #fff 100%)'
    })
}
    
*{ margin: 0; padding: 0;}

body {
	overflow: hidden;
}

#clouds{
	padding: 100px 0;
	background: #c9dbe9;
	background: -webkit-linear-gradient(top, #c9dbe9 0%, #fff 100%);
	background: -linear-gradient(top, #c9dbe9 0%, #fff 100%);
	background: -moz-linear-gradient(top, #c9dbe9 0%, #fff 100%);
}

.cloud {
	width: 200px; height: 60px;
	background-color: #fff;
	
	border-radius: 200px;
	-moz-border-radius: 200px;
	-webkit-border-radius: 200px;
	
	position: relative; 
}

.cloud:before, .cloud:after {
	content: '';
	position: absolute; 
	background: #fff;
	width: 100px; height: 80px;
	position: absolute; top: -15px; left: 10px;
	
	border-radius: 100px;
	-moz-border-radius: 100px;
	-webkit-border-radius: 100px;
	
	-webkit-transform: rotate(30deg);
	transform: rotate(30deg);
	-moz-transform: rotate(30deg);
}

.cloud:after {
	width: 120px; height: 120px;
	top: -55px; left: auto; right: 15px;
}

.x1 {
	left: -250px;

 	-webkit-transform: scale(0.4);
	-moz-transform: scale(0.4);
	transform: scale(0.4);

	-webkit-animation: moveclouds 120s linear infinite;
	-moz-animation: moveclouds 120s linear infinite;
	-o-animation: moveclouds 120s linear infinite;
}


.x2 {
	right:70px; top:-100px;
	
 	-webkit-transform: scale(0.5);
	-moz-transform: scale(0.5);
	transform: scale(0.5);
	opacity: 0.7; 	

	-webkit-animation: moveclouds 140s linear infinite;
	-moz-animation: moveclouds 140s linear infinite;
	-o-animation: moveclouds 140s linear infinite;
}

.x3 {
	left: -550px; top: -200px;
	
	-webkit-transform: scale(0.4);
	-moz-transform: scale(0.4);
	transform: scale(0.4);
	opacity: 0.8; 
	
	-webkit-animation: moveclouds 150s linear infinite;
	-moz-animation: moveclouds 150s linear infinite;
	-o-animation: moveclouds 150s linear infinite;
}

.x4 {
	left: 400px; top: -250px;
	
	-webkit-transform: scale(0.6);
	-moz-transform: scale(0.6);
	transform: scale(0.6);
	opacity: 0.75;
	
	-webkit-animation: moveclouds 100s linear infinite;
	-moz-animation: moveclouds 100s linear infinite;
	-o-animation: moveclouds 100s linear infinite;
}

.x5 {
	left: -750px; top: -250px;
	
	-webkit-transform: scale(0.47);
	-moz-transform: scale(0.47);
	transform: scale(0.47);
	opacity: 0.8;
	
	-webkit-animation: moveclouds 110s linear infinite;
	-moz-animation: moveclouds 110s linear infinite;
	-o-animation: moveclouds 110s linear infinite;
}

@-webkit-keyframes moveclouds {
	0% {margin-left: 1000px;}
	100% {margin-left: -1000px;}
}
@-moz-keyframes moveclouds {
	0% {margin-left: 1000px;}
	100% {margin-left: -1000px;}
}
@-o-keyframes moveclouds {
	0% {margin-left: 1000px;}
	100% {margin-left: -1000px;}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="clouds">
	<div class="cloud x1"></div>
	<div class="cloud x2"></div>
	<div class="cloud x3"></div>
	<div class="cloud x4"></div>
	<div class="cloud x5"></div>
	</div>

答案 1 :(得分:1)

您可以设置标准CSS动画,然后使用动画延迟将其与时钟同步。

您需要设置的延迟与已经过去的时间相等,为负。

由于每天显示这一点非常无聊,我设置了一个例子,它在一分钟内循环,所以它使用了秒。为了表明它是准确的,我也在演示中显示当前的第二个

window.onload = function () {
    var date = new Date();
    var sec = - date.getSeconds();
    var ele = document.getElementById ("test");
    ele.style.animationDelay = sec + 's'; 


setInterval ( function () {
    var date = new Date();
    var ele = document.getElementById ("time");
    ele.innerText = date.getSeconds();
}, 1000);
  
}
#test {
  background: radial-gradient(circle at 4% 4%, yellow 30px, transparent 25px),
  radial-gradient(circle at 4% 92%, white 30px, transparent 25px),
  linear-gradient(to top, black 0%, gray 20%, orange 40%, orange 60%, lightblue 80%, blue 100%);
  background-size: 2000% 2000%;
  background-repeat: no-repeat;
  background-position: 0% 0%;
  height: 200px;
  width: 300px;
  animation: anim 30s infinite alternate;
}


@keyframes anim {
    0% {background-position: 0%  100%;}
  100% {background-position: 0%  0%;}
}

#time {
  width: 200px;
  height: 100px;
  left: 400px;
    position: absolute;
    top: 0px;
    font-size: 60px;
}
<div id="test"></div>
<div id="time"></div>

第二个0是午夜,第二个30是中午。

还有太阳和月亮......