【问题标题】:Adding a day/night cycle for scrolling clouds with smooth color transitions为具有平滑颜色过渡的滚动云添加昼夜循环
【发布时间】:2015-08-06 21:29:26
【问题描述】:

我有滚动的云,但我需要通过检测系统时钟来进行黎明/黄昏和昼/夜循环。我不确定如何使用 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

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

另外,是否可以在所有云都飘过屏幕之前再次循环播放云动画?动画会等待直到所有的云都飘过屏幕,然后再重新开始。

【问题讨论】:

标签: javascript jquery html css


【解决方案1】:

试试这个

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>

【讨论】:

  • 这似乎不起作用,不知道为什么。显然,即使在包含 document.ready() 之后,本地时间也是下午 5.59,转换没有发生。
  • @Jeevan 你想让它发生在 5.30 到 6.30 之间吗?
  • 在 5.50 和 6.30PM 之间,颜色应该保持在 #E0DE3F(前夕橙色),我看不出你的代码有什么问题,仍然不知道为什么没有发生过渡。
  • 对不起,仍然没有进展。你在自己的机器上试用过吗?
  • @Jeevan 你看到什么颜色?
【解决方案2】:

您可以设置一个标准的 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 是中午。

还有太阳和月亮……

【讨论】:

  • 您是否需要在#time 中添加top: 0px; font-size: 60px;,它们的用途是什么,您能否解释一下您使用的javascript,我完全迷路了。在变量ele中,你从#time#test导入什么。
  • @jeevan #time 元素只是用于显示时间的装饰。 ele 是对 DOM 元素的引用(从 getElementById 获得)
  • 但是日月之间好像没有过渡?
猜你喜欢
  • 2013-11-19
  • 2014-02-11
  • 2022-12-25
  • 2014-03-17
  • 1970-01-01
  • 2020-03-29
  • 2015-05-10
  • 2013-11-15
  • 1970-01-01
相关资源
最近更新 更多