- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于另一个项目,我们正在尝试淡入 RGB 颜色空间(从一种颜色到另一种颜色)。作为概念证明,我们用 JavaScript 构建逻辑。最终结果是,div 的背景应逐步更改为给定的颜色。但在我们的示例中,div 只是设置为最终颜色,并且不显示开始颜色和结束颜色之间的步骤。
既然我们无法让它工作,我们的问题是:这里出了什么问题?是逻辑有问题还是我们的 JS 技能有问题:)?
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<script>
$(function() {
var colors = [];
$('#go').click(function() {
console.log($('#red1').val()+" "+$('#green1').val()+" "+$('#blue1').val()+" "+$('#red2').val()+" "+$('#green2').val()+" "+$('#blue2').val());
slidecolor($('#red1').val(), $('#green1').val(), $('#blue1').val(), $('#red2').val(), $('#green2').val(), $('#blue2').val());
readArray();
});
function slidecolor(StartsR, StartsG, StartsB, aimR, aimG, aimB) {
StartsR = parseInt(StartsR);
StartsG = parseInt(StartsG);
StartsB = parseInt(StartsB);
aimR = parseInt(aimR);
aimG = parseInt(aimG);
aimB = parseInt(aimB);
if(aimR >= StartsR)
{
var directionR = 1;
console.log("größer");
var distanceR = aimR - StartsR;
}
else
{
var directionR = 0;
console.log("kleiner");
var distanceR = StartsR - aimR;
}
if(aimB >= StartsB)
{
var directionB = 1;
var distanceB = aimB - StartsB;
}
else
{
var directionB = 0;
var distanceB = StartsB - aimB;
}
if(aimG >= StartsG)
{
var directionG = 1;
var distanceG = aimG - StartsG;
}
else
{
var directionG = 0;
var distanceG = StartsG - aimG;
}
if((distanceR >= distanceB) && (distanceR >= distanceG)) { var distance = distanceR; }
if((distanceG >= distanceR) && (distanceG >= distanceB)) { var distance = distanceG; }
if((distanceB >= distanceR) && (distanceB >= distanceG)) { var distance = distanceB; }
var stepsR = Math.round(distance/distanceR);
var stepsG = Math.round(distance/distanceG);
var stepsB = Math.round(distance/distanceB);
console.log(distance+" "+distanceR);
console.log(stepsR+" "+stepsG+" "+stepsB);
var tmpstepsR = 0;
var tmpstepsG = 0;
var tmpstepsB = 0;
for(i=0; i<=distance; i++) {
console.log(i);
if(i==0)
{
console.log("FIRST RUN");
if(directionR == 1) {
var tmpR = StartsR + 1;
tmpstepsR = stepsR;
}
else
{
var tmpR = StartsR - 1;
tmpstepsR = stepsR + 1;
}
if(directionG == 1) {
var tmpG = StartsG + 1;
tmpstepsG = stepsG;
}
else
{
var tmpG = StartsG - 1;
tmpstepsG = stepsG;
}
if(directionB == 1) {
var tmpB = StartsB + 1;
tmpstepsB = stepsB;
}
else
{
tmpstepsB = stepsB;
var tmpB = StartsB - 1;
}
}
else
{
console.log("NEXT RUN");
if(((stepsR == i) || (tmpstepsR == i)) && tmpR != aimR)
{
tmpstepsR = tmpstepsR + stepsR;
if(directionR == 1) {
var tmpR = tmpR + stepsR;
}
else
{
var tmpR = tmpR - stepsR;
}
}
if(((stepsG == i) || (tmpstepsG == i)) && tmpG != aimG)
{
tmpstepsG = tmpstepsG + stepsG;
if(directionG == 1) {
var tmpG = tmpG + stepsG;
}
else
{
var tmpG = tmpG - stepsG;
}
}
if(((stepsB == i) || (tmpstepsB == i)) && tmpB != aimB)
{
tmpstepsB = tmpstepsB + stepsB;
if(directionB == 1) {
var tmpB = tmpB + stepsB;
}
else
{
var tmpB = tmpB - stepsB;
}
}
}
console.log('rgb('+ tmpR +','+ tmpG +','+ tmpB +')');
colors.push('rgb('+ tmpR +','+ tmpG +','+ tmpB +')');
}
}
function readArray(){
colors.forEach(function(entry){
timeOut(entry);
$('#color').css("background-color", entry);
});
}
function timeOut(entry){
setTimeout(function(){$('#color').css("background-color", entry);}, 3000);
}
});
</script>
<h1>Farbe 1</h1>
red: <input id="red1">
green: <input id="green1">
blue: <input id="blue1">
<h1>Farbe 2</h2>
red: <input id="red2">
green: <input id="green2">
blue: <input id="blue2">
<button id="go">LET'S GO</button>
<div id="color" style="width:500px;height:500px"></div>
</body>
</html>
请注意,由于这只是第一次尝试,某些部分可能会有点问题或难看。最后一部分我们添加了一些时间,这是最后的手段,可能不是最佳实践......
编辑:jsfiddle
最佳答案
在 readArray
,您正在迭代 colors
中的所有条目大批。这个迭代只需要很少的时间(比你注意到的要快)。在该迭代期间,所有设置了3秒超时。它们没有排序,只是设置为在 forEach
之后约 3 秒执行。完成了。
您需要正确排序回调:
function readArray(){
timeOut(colors, 0);
}
function timeOut(array, index){
var entry = colors[index];
$('#color').css("background-color", entry);
var nextIndex = index + 1;
if(nextIndex < array.length){
setTimeout(function(){
timeOut(array, nextIndex);
}, 30);
}
}
基本上,在执行当前“步骤”时,您可以设置下一步的超时时间。
<强> Example Fiddle
请注意,我将超时设置为 30
每步毫秒,因此您实际上可以注意到淡入淡出。
(当然,您可以使用 readArray();
而不是 timeOut(colors, 0);
,这样您就可以完全删除 readArray
)
关于javascript - RGB 空间淡入淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28406429/
我正在尝试剪切和淡出 mp3 文件的最后 4 秒: avconv -i SPEX_pilot_02.mp3 -t 0:0:25 -filter:a fade=t=out:st=21:d=4 prete
我有一个连续的 HTML 元素列表。每个列表项都包含超链接内的图像,如下所示: 当您将鼠标悬停在列表项上时,我本质上想使用 jQuery 淡入包含链接标题的工具提示。因此,当您将
我有一个视频文件和一个音频文件。 我想将它们合并在一起,最终输出的视频将在视频的长度内,并将包含背景中的音频。 我做了: ffmpeg -i output.avi -i bgmusic.mp3 -fi
我有一个生成的超大图表,我将其放入 ScrollView,以便用户可以向右滚动并查看所有值。我想通过淡出 ScrollView 向用户表明右侧还有“更多内容”。通过应用 CAGradientLayer
我目前正在使用此代码来尝试使用具有 .info 类的按钮作为淡入和淡出文本的切换开关。现在动画正在与此代码连续运行。有没有一种方法可以让我单击按钮一次并使文本淡入,而不会在几秒钟后淡出?当您再次单击该
当我的 iPhone 界面旋转时,我想对 UIViewController 的特定 UIView 进行淡入/淡出...就像... - (void)willRotateToInterfaceOrient
所以事情就是这样。我看到这个网站:http://laneandassociates.co/english-mustard-scottish-oats我完全不明白他们是如何做到淡入淡出的。 其淡入淡出效
现在我有这个(只是增加标签的 alpha,中间有小中断): ae.getErrorLabel().setVisible(true); ae.getErr
我在 jQuery 中做了一些简单的 .hover(function() 语句。当我将鼠标悬停在文本上时,我只想要一个 #div.fadeIn,并且在非悬停时淡出。它有效。但这只是如果我发送垃圾邮件悬
我是 jQuery 新手,因此我正在不断学习。 在我创建的网站上,有两个功能似乎相互冲突:第一个功能是当用户开始滚动时网站标题会淡出,第二个功能是在 anchor 之间平滑滚动这一页。第二个脚本使淡出
我使用标题值在单击按钮时显示/隐藏一些 div。第一个按钮将仅显示具有 ab 值的 div,第二个按钮将显示所有 div。一切(有点)都有效,但是当显示所有 div 时,fadeOut/In 会产生令
我试图让一些文本淡出 1000 毫秒,等待 1000 毫秒,将文本更改为数组中的随机条目,然后淡入 1000 毫秒。文本应该在淡出之前不间断地停留 5 秒。 我已经设法更改文本,但我还没有找到如何使其
我收到一条错误消息,如果提交表单并返回错误,则会显示该消息。 表单检查.php jQuery('#error', window.parent.document).html( "There was
我有大约 20 张不同的图像,我想在 4 个框中淡入淡出。我需要它从图像列表中随机选择一个图像并显示它。 框示例 photo1、photo2、photo3、photo4 是它们的名称。它们需要根据其绝
我有一个 ID 为“blog-container”的包含 div,以及其中的一组子 div,其类为“blog-item”。 我想要做的是将“博客容器”中的所有“博客项目”一一淡出,一个接一个,然后以相
我找到并修改了一种创建文本到图像翻转的好方法,在这里:http://jsfiddle.net/pkZAW/12/ $( function() { $("#imglink").hover(
函数检查 session (){ $.ajax({url: "session.php", 成功: 函数(数据){ 如果(数据== 1){ var postFilen = 'msg.php'; $.po
需要一些建议:我想创建一个 fadeIn/fadeOut 脚本,它可以在页面滚动时响应地工作。我想做什么: 滚动时,一旦到达隐藏的 div,它就会淡入。 一旦滚动到达页面顶部,它就会淡出。 任何 fu
当 scrollToTop 超过 1000px 时,我有以下内容应该使 .secondLogo 出现(通过淡入) var secondLogo = $(".secondLogo"); $(window
我有一个音乐脚本,但是当我按下空格又名暂停按钮时,我希望它暂停它已经播放的音乐,但我希望它像 spotify 一样以淡入/淡出的方式播放 这是我到目前为止的代码: var play = true; v
我是一名优秀的程序员,十分优秀!