Абзац случайного слова в верхнем регистре после указателя мыши <p> - PullRequest
0 голосов
/ 30 июня 2018

У меня есть 3 параграфа. Мне нужен код JS, чтобы случайные слова в этом фиктивном тексте были заглавными после того, как я использовал событие наведения мыши на тег P. Он будет меняться случайным образом каждые 3 секунды в каждом абзаце, который я наведу.

HTML:

<head>
    <style>
     p {
         margin-top: 50px;
         font-size: 24px;
     }
    </style>
</head>
<body>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. </p>
  <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old</p>
  <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</body>

1 Ответ

0 голосов
/ 30 июня 2018

[...document.getElementsByTagName('p')].forEach(elem => {
  let oldText = elem.innerHTML;
  elem.onmouseenter = () => {
    elem.onmouseenter = '';
    setInterval(() => elem.innerHTML = oldText.split('').map(word => Math.random() > 0.15 ? word.toLowerCase() : word.toUpperCase()).join(''), 3000);
  }
})
<head>
  <style>
    p {
      margin-top: 50px;
      font-size: 24px;
    }
  </style>
</head>

<body>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. </p>
  <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old</p>
  <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</body>
...