Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 动态字交换动画_Javascript_Html_Css Animations - Fatal编程技术网

Javascript 动态字交换动画

Javascript 动态字交换动画,javascript,html,css-animations,Javascript,Html,Css Animations,我正在尝试为页面上的文本创建一个动画,每隔几秒钟,就用列表中的另一个单词更改一个单词。示例:我有一个标题,上面写着“这很酷”,但我希望每隔几秒钟就将“酷”替换为“neat/awesome/groovy/etc” 老实说,我不确定最好的方法是什么(就使用什么技术而言),我也找不到适用于现代浏览器的代码简介。非常感谢您的帮助 在中,我会这样做: $(加载文档时函数(){// 变换器(); }); 函数变换器(){ var words=[“nifty”、“groovy”、“far out”];//

我正在尝试为页面上的文本创建一个动画,每隔几秒钟,就用列表中的另一个单词更改一个单词。示例:我有一个标题,上面写着“这很酷”,但我希望每隔几秒钟就将“酷”替换为“neat/awesome/groovy/etc”

老实说,我不确定最好的方法是什么(就使用什么技术而言),我也找不到适用于现代浏览器的代码简介。非常感谢您的帮助

在中,我会这样做:


$(加载文档时函数(){//
变换器();
});
函数变换器(){
var words=[“nifty”、“groovy”、“far out”];//添加任意数量
var idx=Math.floor(words.length*Math.random());//随机化器
$(“#change”).text(单词[idx])//替换“change”的内容
var time=Math.floor(5000*Math.random()+3000);//以毫秒为单位
设置超时(换碟机,时间);//起泡,冲洗,重复
}
...
这很酷
关键是使用一个SPAN标记,该标记带有一个可以快速识别的ID。

在中,我会这样做:


$(加载文档时函数(){//
变换器();
});
函数变换器(){
var words=[“nifty”、“groovy”、“far out”];//添加任意数量
var idx=Math.floor(words.length*Math.random());//随机化器
$(“#change”).text(单词[idx])//替换“change”的内容
var time=Math.floor(5000*Math.random()+3000);//以毫秒为单位
设置超时(换碟机,时间);//起泡,冲洗,重复
}
...
这很酷
关键是使用一个SPAN标记,该标记带有一个可以快速识别的ID。

在纯JS中


var words=[“整洁”、“很棒”、“最好”、“非常棒”];
var i=0;
var text=“这很酷”;
函数_getChangedText(){
i=(i+1)%words.length;
console.log(字[i]);
返回文本。替换(/cool/,words[i]);
}
函数_changeText(){
var txt=_getChangedText();
console.log(txt);
$(“#转换器”).text(txt);
}
setInterval(“_changeText()”,1000);
这很酷
在纯JS中


var words=[“整洁”、“很棒”、“最好”、“非常棒”];
var i=0;
var text=“这很酷”;
函数_getChangedText(){
i=(i+1)%words.length;
console.log(字[i]);
返回文本。替换(/cool/,words[i]);
}
函数_changeText(){
var txt=_getChangedText();
console.log(txt);
$(“#转换器”).text(txt);
}
setInterval(“_changeText()”,1000);
这很酷

这个问题很老了,但它在谷歌搜索中出现了。在2018年,您可以使用CSS动画轻松实现此行为,而无需任何额外的JavaScript代码

以下内容应能满足您的需求:

<!DOCTYPE html>
<html>
  <head>
    <style>
      .animated{
        display: inline;
        text-indent: 8px;
      }

      .animated span{
        animation: topToBottom 12.5s linear infinite 0s;
        -ms-animation: topToBottom 12.5s linear infinite 0s;
        -webkit-animation: topToBottom 12.5s linear infinite 0s;
        color: red;
        opacity: 0;
        overflow: hidden;
        position: absolute;
      }

      .animated span:nth-child(2){
        animation-delay: 2.5s;
        -ms-animation-delay: 2.5s;
        -webkit-animation-delay: 2.5s;
      }

      .animated span:nth-child(3){
        animation-delay: 5s;
        -ms-animation-delay: 5s;
        -webkit-animation-delay: 5s;
      }

      .animated span:nth-child(4){
        animation-delay: 7.5s;
        -ms-animation-delay: 7.5s;
        -webkit-animation-delay: 7.5s;
      }

      .animated span:nth-child(5){
        animation-delay: 10s;
        -ms-animation-delay: 10s;
        -webkit-animation-delay: 10s;
      }

      @-moz-keyframes topToBottom{
        0% { opacity: 0; }
        5% { opacity: 0; -moz-transform: translateY(-50px); }
        10% { opacity: 1; -moz-transform: translateY(0px); }
        25% { opacity: 1; -moz-transform: translateY(0px); }
        30% { opacity: 0; -moz-transform: translateY(50px); }
        80% { opacity: 0; }
        100% { opacity: 0; }
      }
      @-webkit-keyframes topToBottom{
        0% { opacity: 0; }
        5% { opacity: 0; -webkit-transform: translateY(-50px); }
        10% { opacity: 1; -webkit-transform: translateY(0px); }
        25% { opacity: 1; -webkit-transform: translateY(0px); }
        30% { opacity: 0; -webkit-transform: translateY(50px); }
        80% { opacity: 0; }
        100% { opacity: 0; }
      }
      @-ms-keyframes topToBottom{
        0% { opacity: 0; }
        5% { opacity: 0; -ms-transform: translateY(-50px); }
        10% { opacity: 1; -ms-transform: translateY(0px); }
        25% { opacity: 1; -ms-transform: translateY(0px); }
        30% { opacity: 0; -ms-transform: translateY(50px); }
        80% { opacity: 0; }
        100% { opacity: 0; }
      }
    </style>
  </head>

  <body>
    <h2>CSS Animations are
      <div class="animated">
        <span>cool.</span>
        <span>neat.</span>
        <span>awesome.</span>
        <span>groovy.</span>
        <span>magic.</span>
      </div>
    </h2>
  </body>
</html>

.动画{
显示:内联;
文本缩进:8px;
}
.动画跨度{
动画:topToBottom 12.5s线性无限0s;
-ms动画:topToBottom 12.5s线性无限0s;
-webkit动画:topToBottom 12.5s线性无限0s;
颜色:红色;
不透明度:0;
溢出:隐藏;
位置:绝对位置;
}
.动画跨度:第n个孩子(2){
动画延迟:2.5s;
-ms动画延迟:2.5s;
-webkit动画延迟:2.5s;
}
.动画跨度:第n个孩子(3){
动画延迟:5s;
-ms动画延迟:5s;
-webkit动画延迟:5s;
}
.动画跨度:第n个孩子(4){
动画延迟:7.5s;
-ms动画延迟:7.5s;
-webkit动画延迟:7.5s;
}
.动画跨度:第n个孩子(5){
动画延迟:10秒;
-毫秒动画延迟:10秒;
-webkit动画延迟:10秒;
}
@-moz关键帧topToBottom{
0%{不透明度:0;}
5%{opacity:0;-moz变换:translateY(-50px);}
10%{opacity:1;-moz变换:translateY(0px);}
25%{opacity:1;-moz变换:translateY(0px);}
30%{opacity:0;-moz变换:translateY(50px);}
80%{不透明度:0;}
100%{不透明度:0;}
}
@-webkit关键帧topToBottom{
0%{不透明度:0;}
5%{opacity:0;-webkit transform:translateY(-50px);}
10%{opacity:1;-webkit transform:translateY(0px);}
25%{opacity:1;-webkit transform:translateY(0px);}
30%{opacity:0;-webkit transform:translateY(50px);}
80%{不透明度:0;}
100%{不透明度:0;}
}
@-ms关键帧topToBottom{
0%{不透明度:0;}
5%{opacity:0;-ms transform:translateY(-50px);}
10%{不透明度:1;-ms变换:translateY(0px);}
25%{不透明度:1;-ms变换:translateY(0px);}
30%{不透明度:0;-ms变换:translateY(50px);}
80%{不透明度:0;}
100%{不透明度:0;}
}
CSS动画是
酷。
整洁的
令人惊叹的。
棒极了。
魔术

请注意,这只是一个垂直滑动的示例。CSS在动画/过渡方面基本上有无限的可能性。

这个问题已经很老了,但在谷歌搜索中我发现了这个问题。在2018年,您可以使用CSS动画轻松实现此行为,而无需任何额外的JavaScript代码

以下内容应能满足您的需求:

<!DOCTYPE html>
<html>
  <head>
    <style>
      .animated{
        display: inline;
        text-indent: 8px;
      }

      .animated span{
        animation: topToBottom 12.5s linear infinite 0s;
        -ms-animation: topToBottom 12.5s linear infinite 0s;
        -webkit-animation: topToBottom 12.5s linear infinite 0s;
        color: red;
        opacity: 0;
        overflow: hidden;
        position: absolute;
      }

      .animated span:nth-child(2){
        animation-delay: 2.5s;
        -ms-animation-delay: 2.5s;
        -webkit-animation-delay: 2.5s;
      }

      .animated span:nth-child(3){
        animation-delay: 5s;
        -ms-animation-delay: 5s;
        -webkit-animation-delay: 5s;
      }

      .animated span:nth-child(4){
        animation-delay: 7.5s;
        -ms-animation-delay: 7.5s;
        -webkit-animation-delay: 7.5s;
      }

      .animated span:nth-child(5){
        animation-delay: 10s;
        -ms-animation-delay: 10s;
        -webkit-animation-delay: 10s;
      }

      @-moz-keyframes topToBottom{
        0% { opacity: 0; }
        5% { opacity: 0; -moz-transform: translateY(-50px); }
        10% { opacity: 1; -moz-transform: translateY(0px); }
        25% { opacity: 1; -moz-transform: translateY(0px); }
        30% { opacity: 0; -moz-transform: translateY(50px); }
        80% { opacity: 0; }
        100% { opacity: 0; }
      }
      @-webkit-keyframes topToBottom{
        0% { opacity: 0; }
        5% { opacity: 0; -webkit-transform: translateY(-50px); }
        10% { opacity: 1; -webkit-transform: translateY(0px); }
        25% { opacity: 1; -webkit-transform: translateY(0px); }
        30% { opacity: 0; -webkit-transform: translateY(50px); }
        80% { opacity: 0; }
        100% { opacity: 0; }
      }
      @-ms-keyframes topToBottom{
        0% { opacity: 0; }
        5% { opacity: 0; -ms-transform: translateY(-50px); }
        10% { opacity: 1; -ms-transform: translateY(0px); }
        25% { opacity: 1; -ms-transform: translateY(0px); }
        30% { opacity: 0; -ms-transform: translateY(50px); }
        80% { opacity: 0; }
        100% { opacity: 0; }
      }
    </style>
  </head>

  <body>
    <h2>CSS Animations are
      <div class="animated">
        <span>cool.</span>
        <span>neat.</span>
        <span>awesome.</span>
        <span>groovy.</span>
        <span>magic.</span>
      </div>
    </h2>
  </body>
</html>

.动画{
显示:内联;
文本缩进:8px;
}
.动画跨度{
动画:topToBottom 12.5s线性无限0s;
-ms动画:topToBottom 12.5s线性无限0s;
-webkit动画:topToBottom 12.5s线性无限0s;
颜色:红色;
不透明度:0;
溢出:隐藏;
位置:绝对位置;
}
.动画跨度:第n个孩子(2){
动画延迟:2.5s;
-ms动画延迟:2.5s;
-webkit动画延迟:2.5s;
}
.动画跨度:第n个孩子(3){
动画延迟
<!DOCTYPE html>
<html>
  <head>
    <style>
      .animated{
        display: inline;
        text-indent: 8px;
      }

      .animated span{
        animation: topToBottom 12.5s linear infinite 0s;
        -ms-animation: topToBottom 12.5s linear infinite 0s;
        -webkit-animation: topToBottom 12.5s linear infinite 0s;
        color: red;
        opacity: 0;
        overflow: hidden;
        position: absolute;
      }

      .animated span:nth-child(2){
        animation-delay: 2.5s;
        -ms-animation-delay: 2.5s;
        -webkit-animation-delay: 2.5s;
      }

      .animated span:nth-child(3){
        animation-delay: 5s;
        -ms-animation-delay: 5s;
        -webkit-animation-delay: 5s;
      }

      .animated span:nth-child(4){
        animation-delay: 7.5s;
        -ms-animation-delay: 7.5s;
        -webkit-animation-delay: 7.5s;
      }

      .animated span:nth-child(5){
        animation-delay: 10s;
        -ms-animation-delay: 10s;
        -webkit-animation-delay: 10s;
      }

      @-moz-keyframes topToBottom{
        0% { opacity: 0; }
        5% { opacity: 0; -moz-transform: translateY(-50px); }
        10% { opacity: 1; -moz-transform: translateY(0px); }
        25% { opacity: 1; -moz-transform: translateY(0px); }
        30% { opacity: 0; -moz-transform: translateY(50px); }
        80% { opacity: 0; }
        100% { opacity: 0; }
      }
      @-webkit-keyframes topToBottom{
        0% { opacity: 0; }
        5% { opacity: 0; -webkit-transform: translateY(-50px); }
        10% { opacity: 1; -webkit-transform: translateY(0px); }
        25% { opacity: 1; -webkit-transform: translateY(0px); }
        30% { opacity: 0; -webkit-transform: translateY(50px); }
        80% { opacity: 0; }
        100% { opacity: 0; }
      }
      @-ms-keyframes topToBottom{
        0% { opacity: 0; }
        5% { opacity: 0; -ms-transform: translateY(-50px); }
        10% { opacity: 1; -ms-transform: translateY(0px); }
        25% { opacity: 1; -ms-transform: translateY(0px); }
        30% { opacity: 0; -ms-transform: translateY(50px); }
        80% { opacity: 0; }
        100% { opacity: 0; }
      }
    </style>
  </head>

  <body>
    <h2>CSS Animations are
      <div class="animated">
        <span>cool.</span>
        <span>neat.</span>
        <span>awesome.</span>
        <span>groovy.</span>
        <span>magic.</span>
      </div>
    </h2>
  </body>
</html>