编程语言
首页 > 编程语言> > javascript-如何遍历h1元素并使用jquery使每个单词淡入?

javascript-如何遍历h1元素并使用jquery使每个单词淡入?

作者:互联网

我想知道如何遍历h1元素,并使每个单词在另一个单词中逐渐消失.

我完成了,但是代码并不干.有人可以显示并解释如何使用循环吗?

$('document').ready(function() {
  $('#H').fadeIn(3000).removeClass("hidden").addClass("hColor1");
  $('#e').fadeIn(5000).removeClass("hidden").addClass("hColor2");
  $('#l').fadeIn(6000).removeClass("hidden").addClass("hColor3");
  $('#secondL').fadeIn(7000).removeClass("hidden").addClass("hColor4");
  $('#o').fadeIn(7300).removeClass("hidden").addClass("hColor5");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="container">
  <h1 class="hidden"><span id="H">Hello</span> <span id="e">everyone</span> <span id="l">lets</span> <span id="secondL">learn</span> <span id="o">javascript</span></h1>
</div>

解决方法:

// get your text string
var hello = $('.hello').text();
// empty text container so we can refill it later
$('.hello').empty();
// split string by each word and save to array
var helloArray = hello.split(' ');
// adjust these values to customize how slow/fast your words appear
var delays = [400, 500, 1500, 1600, 1900];
// for each word in the array..
$(helloArray).each(function(i) {
  // cant use this inside the setTimeout function so we save this as a variable
  var pseudoThis = this;
  // begin the setTimeout function to stagger/delay the word appearance
  setTimeout(function() {
    // the html to wrap each word with for customizing css
    wordAndWrapper = '<span class="hColor n'+i+'">'+pseudoThis+'</span> ';
    // append html with variables inserted to text container
    $('.hello').append(wordAndWrapper);
  // i is used here to get the position in the delays array
  }, delays[i]);
  // if its the last word (or any word you want to emphasize as we've done with 'javascript' here)
  if (i === 4) {
    setTimeout(function() {
      // custom css styling for last word using a class
      $('.n4').addClass('grow');
    // had to make the delay 50ms more than delay of appearance so that transition in css applies
    }, 1950);
  };
})
html, body {
  margin: 0;
  background-color: hsla(0, 0%, 90%, 1);
}
.wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100vw;
}
.hello {
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  flex-wrap: wrap;
  margin: 0;
  text-align: center;
  font-size: 4vw;
  font-weight: bold;
}
.hColor { 
  display: flex;
  justify-content: center;
  width: 25%;
  padding: 0 30px;
  box-sizing: border-box;
  transition: all 0.2s linear;
}
.hColor.n0 { color: hsl(0, 51.2%, 49.8%); }
.hColor.n1 { color: hsl(190.7, 93.7%, 43.5%); }
.hColor.n2 { color: hsl(36, 70.9%, 51.6%); }
.hColor.n3 { color: hsl(286, 71.8%, 44.5%); }
.hColor.n4 { width: 100%; font-variant: small-caps; color: hsl(107.9, 83.6%, 45.5%); }
.hColor.n4.grow { font-size: 11vw; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  <h1 class="hello">Hello Everyone Let's Learn javascript</h1>
</div>

小提琴

https://jsfiddle.net/Hastig/vorj57gs/

信用

> Get text, put each word into an array .split
> Using setTimeout in the .each loop

标签:fadein,loops,javascript,jquery
来源: https://codeday.me/bug/20191109/2013020.html